Using Serverless for HTML/CSS Website

I’m interested in using Serverless to create a basic HTML/CSS Website. Why? (Partly because I want to see if it can be done), and second because the cost for a website created this way would essentially be 0 since I doubt I’ll use the 3 million seconds per month available to me in execution time via Lambda. Not to mention Free HTTPS (Certificates!).

I created a simple test in which I finally got my domain name to return some text.

Homepage.js

'use strict';

module.exports.render = (event, context, callback) => {
  let dynamicHtml = '<p>Hey Unknown!</p>';

  // Check for GET params and use if available
  if (event.queryStringParameters && event.queryStringParameters.name) {
    dynamicHtml = `<p>Hey ${event.queryStringParameters.name}!</p>`;
  }

  const html = `
  <html>
    <style>
      h1 { color: #73757d; }
    </style>
    <body>
      <h1>Landing Page</h1>
      ${dynamicHtml}
    </body>
  </html>`;

  const response = {
    statusCode: 200,
    headers: {
      'Content-Type': 'text/html',
    },
    body: html,
  };

  callback(null, response);
};

My serverless file looks like this.

service: my-awesome-service # NOTE: update this with your service name

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: us-east-1
  environment:
    GOOGLE_MAPS_API_KEY: key1234

plugins:
  - serverless-domain-manager

custom:
  customDomain:
    domainName: www.myawesomedomain.com
    basePath: ""
    stage: ${self:provider.stage}
    certificateName: "myawesomedomain.com"
    createRoute53Record: true

functions:
  homepage:
    handler: homepage.render
    events:
      - http:
          path: /
          method: get

resources:
  Resources:
    uploadBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:service}-${self:provider.stage}-uploads
    userTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:service}-${self:provider.stage}-users
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
        ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1

Note for following along, to use a custom domain name you need to create the certificate in amazon and what through me off was the ‘tag’ property at the bottom of the interface needs to be what you will reference in certificateName in serverless.yml file for the customDomain. Also, it only works with sub domains, and of course you need to register your name with Route53

My question is what is a good organization structure for creating a website this way. To my knowledge there is no framework designed to work with this. If this is true, I was thinking of creating a project to be ‘said’ framework. Anyone interested in doing this with me? I just don’t want to re-design the wheel if its already there somewhere.

Basically, I’m looking for something that makes it easy to do HTML/Templating like Twig, but setup using this style. It’s nice using serverless.yml to take care of the routing.

My other question is using serverless like this with Lambda even good practice to create a website? Do the limitations of Lambda get in the way?

Hi,
I checked your solution. You would be using a bazooka to kill a bee if u implement ur whole website like this.

For simple website hosting following is what i suggest

Web site hosting :- use S3 bucket to store your static webiste files S3 bucket with static website hosting will make your S3 bucket a webserver but you can serve only static content. For dynamic stuff you can use jquery or angular js and get dynamic data from apis. Apis you write in serverless framework.

Note : S3 bucket supports only http url for SSL you will need to connect S3 bucket to cloudfront distribution.

SSL certifcates : aws provides free ssl certificates, you can use aws cerrtificate manager to issue an ssl certificate to ur domain. This certificate you can attach to ur cloudfront distribution.

Above all things can be done without using serverless framework by going in the aws console. You would be using serverless technologies but not serverless framework.

Now as far as serverless framework is concerned, ideally it should be used to get dynamic data in the webpage. Making apis.

Let me know if this answers your question.

Hi there, this sounds about right. I was curious how to use it correctly. So I was thinking using ReactJS Library (maybe with Redux) as my HTML/CSS generating code base. I believe reactJS can load the dynamic content via AJAX perhaps using the serverless framework to generate just the parts I need, which I do like this design pattern. My only worry is the SEO side of it. For search engines to find my site, its important the server side renders the results so that the SEO bot can find the text needed. That text might be dynamic. How do I solve this issue? React JS uses javascript to render the text after the fact which the search engine bots fail to read from my understanding.

Essentially it will see an empty page without the content in it, since most of the content is dynamic for everything. Maybe I use server-side for the important SEO parts on the front-end side of the website, and backend parts where you need a login I just do all client side rendering?

1 Like

@x11joe I just came across the same thoughts.

I’m basically gonna build a react app, which display stuff based on settings. Those settings are gonna be stored in a DB.

I need a server endpoint (node lambda) which retrieves such settings and displays the HTML page with contains the settings retrieved from the DB and the react app.

I could do what @dhavalyours is saying, store my static HTML file in S3 bucket, and serve it. Then, use ajax to load such settings through an endpoint (lambda). But it would kill my SEO.

Hence the fact that I’m looking for a solution using Serverless. I’m also thinking about serving some kind of template html file, maybe handlebars.

@x11joe I just did a boilerplate for Serverless + Handlebars => SSR

Feedback welcome!

I’ve been implementing the same pattern (s3/React->apigw->lambda) for the past month or so. The fact that you are serverless doesn’t really impact the SEO - it would look like a normal React page which is popular enough that I would imagine people have given SEO quite a bit of thought. (It wasn’t relevant in my case.) A quick Google search does turn up this: https://medium.freecodecamp.org/seo-vs-react-is-it-neccessary-to-render-react-pages-in-the-backend-74ce5015c0c9

Beware that serverless may impact SEO.

Indeed, SEO needs fast responses, if a SEO request doesn’t get answered fast enough, you may lose the preview mode. (preview on facebook, slack, etc.)

And since serverless functions have a startup time (if cold) you may run into this issue.
It’s probably not an issue if the lambdas replies fast, but for those that need 6sec+ to get warm, it’s likely gonna be an issue.

I don’t have much feedback about this though, mostly assuming behaviour here.