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.

The SAM Setup: API Gateway triggers lambda functions. See GitHub.

Serverless?

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.

After setting up and deploying the sample code, check if the API is working through the AWS Console. The post method returns the parameters passed to it. This demonstrates that the lambda function can access the parameters from API Gateway.

Prerequisites

This tutorial requires the following:
1. An AWS Account
2. AWS CLI Commands
3. Local Workspace Configuration
4. AWS SAM Commands

Using Swagger, API Gateway, and Lambda

The SAM template integrates the Swagger file as follows.

In template.yaml:

Resources:
PetStoreApiGateway:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionUri: ./swagger.yaml
PostPetFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PostPetFunction
Runtime: python3.6
Handler: function.handler
CodeUri: ./lambda/postPet/
Events:
PostPetApi:
Type: Api
Properties:
RestApiId: !Ref "PetStoreApiGateway"
Path: /pet
Method: POST

In swagger.yaml:

x-amazon-apigateway-request-validators:
basic:
validateRequestParameters: true
x-amazon-apigateway-integration:
type: "aws"
httpMethod: "POST"
# Replace AWS_REGION and ACCOUNT_ID in uri
uri: "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:AWS_REGION:ACCOUNT_ID:function:PostPetFunction/invocations"

Deployment

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.

#!/bin/sh
# IMPORTANT: Bucket names must be unique for all AWS users.
BUCKET="pet-store-api-deployment-workspace-<UNIQUE_NAME>"
# 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 \
--template-file template.yaml
--s3-bucket $BUCKET \
--output-template-file package.yaml
# Deploys your stack
sam deploy \
--template-file package.yaml \
--stack-name pet-store-stack \
--capabilities CAPABILITY_IAM
Created resources are on the AWS CloudFormation console.

What’s Next?

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!