In this article, we will be covering how to deploy AWS serverless applications using the SAM CLI. This article builds off of part 1 so I would recommend reading it before continuing with this tutorial. The SAM CLI has built-in support for packaging and deploying serverless projects by utilizing Amazons Infrastructure as Code (IAC) framework AWS CloudFormation. By using this framework it is simple to deploy the serverless app because SAM and CloudFormation take care of the provisioning, setup, and deployment of all the code, IAM users, and API gateways needed.
Note: Check out part 1 of the SAM CLI series if you have not already before continuing
SAM CLI Series
Prerequisites
- sam-app from part 1
- SAM CLI
- AWS CLI
- AWS Account
Setup
Since this tutorial builds off the part 1 tutorial there isn't much more setup needed. Make sure that you have the AWS CLI configured with your AWS account credentials that you want to deploy the project to.
Creating the S3 Bucket
Amazon S3 is an object storage service that provides the infrastructure to store files of nearly any type and retrieve them using a simple REST API. The SAM CLI will use an S3 bucket to store a configuration file which defines the resources needed for deploying the project, as well as defines the needed IAM users and permissions for executing the Lambda functions. Using the AWS CLI execute the following command to create an S3 bucket.
aws s3 mb s3://samexamplebucket --region us-east-1
This will create a bucket called "samexamplebucket" in the us-east-1 region.
Packaging the application
Now that we have our bucket for holding our configuration files its time to package our application and generate said config file. Using the SAM CLI execute the following command from the root of the sam-app project directory.
sam package --s3-bucket samexamplebucket --output-template-file serverless-output.yaml
Note: If you named your bucket differently in the above step then change the name of the --s3-bucket parameter accordingly
You should now have a serverless-ouput.yaml file that looks something like the following
AWSTemplateFormatVersion: '2010-09-09'
Description: 'sam-app
Sample SAM Template for sam-app
'
Globals:
Function:
Timeout: 3
Outputs:
HelloWorldApi:
Description: API Gateway endpoint URL for Prod stage for Hello World function
Value:
Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/
HelloWorldFunction:
Description: Hello World Lambda Function ARN
Value:
Fn::GetAtt:
- HelloWorldFunction
- Arn
HelloWorldFunctionIamRole:
Description: Implicit IAM Role created for Hello World function
Value:
Fn::GetAtt:
- HelloWorldFunctionRole
- Arn
Resources:
HelloWorldFunction:
Properties:
CodeUri: s3://samexamplebucket/97538d222384df4925c659c8b2642d8b
Environment:
Variables:
PARAM1: VALUE
Events:
HelloWorld:
Properties:
Method: get
Path: /hello
Type: Api
Handler: app.lambda_handler
Runtime: python3.7
Type: AWS::Serverless::Function
HelloWorldNameFunction:
Properties:
CodeUri: s3://samexamplebucket/69a01c03edf3f465be0a11f61232b0ff
Events:
HelloWorld:
Properties:
Method: get
Path: /hello/{name}
Type: Api
Handler: app.lambda_handler
Runtime: python3.7
Type: AWS::Serverless::Function
Transform: AWS::Serverless-2016-10-31
This file tells CloudFormation what resources are needed and how to deploy them.
Deploy the application
At this point, we have our s3 bucket and our config file so it is time to deploy our hello world serverless application to AWS. The following command will accomplish this.
sam deploy --template-file serverless-output.yaml --stack-name samexamplestack
And after that executes there should be a live version of your application on AWS. Open up the AWS console at http://console.aws.amazon.com and navigate to the CloudFormation resource.

After clicking on "Stacks" you should see something like the screenshot above. Clicking on the Stack name will open up that specific deployment. Click on the "Output" tab and there should be a row with "HelloWorldApi" on it with a link as the value. That link is where your application is now available on the created API Gateway which points to the Lambda resources! Both /hello and /hello/<name> should work to execute both of the hello world Lambda functions.
Summary
In part 2 of the SAM CLI series, we saw how to use the AWS and SAM CLIs to deploy a serverless application to the cloud using AWS CloudFormation. This simple deployment path takes a lot of the effort and complexity out of getting applications running in the cloud.