This repo contains the full code and templates needed to create a the Serverless SamFarm demo as shown in the What's New with AWS Lambda presentation at Re:Invent 2016.
There are three separate parts to this application: the api, the pipeline which detects, builds, and deploys changes, and the website. Lets start by getting that website up and running.
In the website directory there are four files:
- index.html: This is the index file that our S3 bucket will be displaying.
- app.js: The heart of our website. We will be making some changes to this in a bit, but for now we can leave it alone.
- squirrel.png: Our friend! SAM the squirrel.
- website.yaml: The CloudFormation template used to create the Amazon S3 bucket for the website.
To create the website stack.
Once the stack is complete, we will need to keep track of the S3 bucket name and the URL for the website.
Now we have all the seperate parts of our website, but lets get SAM up and running:
sh upload_website.sh <s3-bucket-name>
And visit the url you saved before. You should see something like this:
Ta-da, a working website! SAM the squirrel may be all alone right now, but we'll fix that in a bit.
The Serverless API we are building! The api directory contains five files.
- beta.json: The CloudFormation staging file. This will be used by CloudFormation to pass parameters to our CloudFormation template.
- buildspec.yml: This is used by CodeBuild in the build step of our pipeline. We will get to that later.
- index.js: The Lambda function code!
- package.json: The package.json that defines what packages we need for our Lambda function.
- saml.yaml: SAML my YAML! This is the SAM template file that will be used to create our API gateway resource and Lambda function, hook them up together
Create a new github repo from the api directory and place these files in there. This repo will be used for your automated CI/CD pipeline we build below.
The pipeline is a full CI/CD serverless pipeline for building and deploying your api. In this example we are using CloudFormation to create the pipeline, all resources, and any permissions needed.
The following resources are created:
- An S3 bucket to store deployment artifacts.
- An AWS CodeBuild stage to build any changes checked into the repo.
- The AWS CodePipeline that will watch for changes on your repo, and push these changes through to build and deployment steps.
- All IAM roles and policies required.
The CloudFormation templates being used to create these resources can be found in pipeline directory.
To create the pipeline stack, click the launch stack button below.
If you have looked at the code in the website (or if you haven't, now is your chance!), you may have noticed that the website makes http requests to an api periodically to update the number of SAMs on the screen. Right now it is pointing at nothing, so lets update it to point it to brand spanking new API. In the app.js, look for the line
var GET_SAM_COUNT_URL = 'INSERT API GATEWAY URL HERE';
and update that to your API Gateway endpoint. It should be in the format:
https://<api-id>.execute-api.us-east-1.amazonaws.com/Prod/sam
Now lets update your S3 static website with this change:
sh upload_website.sh <s3-bucket-name>
Now that we have our website, our code repository with our lambda function and our pipeline configured, lets see it in action. We are going to make two changes to our repository, first were going to setup our API for CORS, and second were going to update our Lambda function. Both changes will be made in the repo you created in Step 2.
Go to the beta.json file and update the following line:
"OriginUrl": "*"
to the url for the S3 static site we created. Something like:
"OriginUrl": "http://<s3-bucket-name>.s3-website-us-east-1.amazonaws.com"
Go to index.js file the repo you made in Step 2 and update the line:
var samCount = 1;
to
var samCount = 15;
Commit and push the changes.
Go back to the pipeline we generated in Step 3, you will see AWS CodePipeline automatically pick up your change, and start the build and deploy process. Voila! A completely version controlled, serverless, CI/CD solution to give a squirrel a few friends. Technology!