Creating a static website is one of the simplest cases for website development. Even with a static site, serverless hosting will provide the same scalability and cost benefits as with more complex apps.

This blog post will walk you though the static serverless site example available on GitHub.

Handler

The site will be served using a Node.js express handler:

const express = require('express');
const shim = require('@ffleet/shim');

const app = express();
app.use(express.static('./public'));
exports.static = shim.http(app);

Taking this code one section at a time, we start by importing the express and @ffleet/shim libraries:

const express = require('express');
const shim = require('@ffleet/shim');

The next two lines create a standard express server and plugin the static handler middleware provided by express:

const app = express();
app.use(express.static('./public'));

The final line exports the express app for AWS Lambda to import. The shim library is what allows us to use express, as AWS Lambda does not support it natively.

exports.static = shim.http(app);

Config

The Functional Fleet configuration below creates a catch-all route (/**) that sends all HTTP requests to the static function handler that we created above. The root route / is necessary to capture requests to the root resource.

version: 1.0
runtime: node8
role: arn:aws:iam::962961885225:role/lambda
functions:
  static:
    handler: index.static
http:
  routes:
    /:
      get:
        function: static
    /**:
      get:
        function: static

And that's it! Using the GitHub code, you can clone and deploy this example immediately. Make sure that you update the role in the configuration to the IAM role that you created.