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.