A Simple Starter Kit for AWS Serverless Application Model
Define your first SAM template. Deploy API Gateway and Lambda functions in a few minutes.
I’m still new to Amazon Web Services (AWS). As a beginner, the breadth of AWS services was overwhelming — I didn’t know where to start. The documentation was difficult to search. I couldn’t find deployable sample code on the web.
So, I wrote the starter kit I wish I had when I was starting out. In particular, this tutorial is for the AWS Serverless Application Model (AWS SAM). To get started, simply download the code and deploy your first serverless service.
AWS is the leading provider of cloud services. This includes virtual servers, software containers, databases, file systems, among many others. IoT, deep learning, augmented reality—Name it, they have it.
AWS also supports the serverless execution model. That is, they manage the environment for running scripts. No need to rent a dedicated server nor installing software packages.
There are drawbacks in relying on a third-party for the execution environment. However, going serverless enables developers to test ideas and release services quickly.
To get started with SAM, I made a minimal sample to show how to write a template file (template.yaml). All codes and deployment instructions are on GitHub.
The template file reads a separate Swagger file (swagger.yaml). This file defines two API methods for post and get. These two methods trigger lambda functions (lambda/). The lambda functions then return the parameters passed to it.
Using Swagger, API Gateway, and Lambda
The SAM template integrates the Swagger file as follows.
RestApiId: !Ref "PetStoreApiGateway"
# Replace AWS_REGION and ACCOUNT_ID in uri
To deploy, use the deploy.sh shell script. There are three steps: create the deployment bucket, create the CloudFormation template, and finally, create the resources specified in the template.
# IMPORTANT: Bucket names must be unique for all AWS users.
# Creates your deployment bucket if it doesn't exist yet.
aws s3 mb s3://$BUCKET
# Uploads files to S3 bucket and creates CloudFormation template
sam package \
--s3-bucket $BUCKET \
# Deploys your stack
sam deploy \
--template-file package.yaml \
--stack-name pet-store-stack \
The lambda function can now be programmed to do something more interesting. For example, lambda can write to DynamoDB (AWS-managed NoSQL database). For SAM-supported AWS resources, checkout the AWS guide here. The cloud is the limit!