Contact Us

AWS SAM - Build, Test and Deploy Serveless Applications.


What is SAM?

Serverless Application Model (SAM) is a tool that enables you to build, test, and deploy serverless applications.

Why SAM?

With SAM, you can define the resources you need in a YAML or JSON file, and AWS CloudFormation takes care of creating those resources for you. SAM extends AWS CloudFormation to provide a simplified way of defining and utilizing AWS serverless application resources.

Setting up your environment

In order to use SAM locally on your computer, you need to have the following installed on your machine:

You can proceed to the next step after completing the installations.

Configure your AWS CLI

To authenticate with your AWS account:

Run the following command:

aws configure

Provide the requested information. Example:

awsconfigure

You can continue to the next step if you have successfully completed the previous steps.

Create a SAM Project

Run the following command to create a new SAM project:

sam init

Enter 1 for the template.

choosetemplate

Enter 1 to select the Hello World Example.

selecttemplate

Enter N to select NO.

selectno

Select the package you would like to use, in my case, I will be using Python 3.8.

selectpython

Select Zip as the required package.

selectzip

Ignore the following.

ignoreno

Enter a project name. A default name is already provided, you can overwrite it with yours or click Enter to continue.

completedsam

Once completed, you can open the created folder in your favorite code editor.

projectstructure

The project Structure

What's in the template?

Globals

The Globals contain all configurations for every created function using this template.

In this case, we have a timeout and memory size of 128MB maximum.

global

Resources

The resources contain all created functions and function resources.

resources

In this Resource, we have a function called HelloWorldFunction.

Let's break down some properties:

The Code

In the app.py file, the default code should look something like this:

code

We can see that there is a function called lambda_handler, which basically just returns a JSON.

So when we test this function, we expect to see "hello world" in the output!

Test

test

Run the code

sam local start-api --port 8080

AWS SAM: Build, Test, and Deploy Serverless Applications - A Comprehensive Guide

Open the URL in your browser and you will see "hello world" on your browser screen.

AWS SAM: Build, Test, and Deploy Serverless Applications - A Comprehensive Guide

You can update the message and code without restarting the server; it will automatically update.

Run Tests

The tests are created with pytest, so you need to install it before running the tests.

pip3 install pytest pytest-mock python3 -m pytest tests/unit

Don't forget to update the test if you have made any changes to the code.

Deploy

In order to deploy our lambda function to AWS, we need to first build it. This will tweak some things and compress the project, including external dependencies and packages, into a zip file.

To build your project, run:

sam build

You can now deploy your function to AWS with:

sam deploy --guided

Fill out the questions like the example in the picture, and press Enter to skip.

deployguide

Once completed, the output will look something like this:

deploy-output

You can visit the URL provided in the HelloWorldFunction

or use curl

curl

There you go! You have successfully built, tested, and deployed a serverless application using AWS SAM.

You can inspect your build in CloudFormation in your AWS console.

Clean up

Run

sam delete

This will delete all resources and functions created on AWS for this project.