A Simple Starter Kit for AWS Serverless Application Model

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.


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.


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:

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

In swagger.yaml:

validateRequestParameters: true
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"


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.
# 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!

Marc Ericson Santos, PhD

Written by

Bridging research to practice, one article at a time. HCI researcher turned software engineer based in Tokyo. UX insights and personal essays.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade