Contact Us

AWS SAM - Build, Test and Deploy Serveless Applications.


By abdulmumin yaqeen

on May 15, 2023



img

What is SAM?

Serverless Application Model (SAM) is a tool that enable 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 other to use sam locally on your computer, you need to have the following installed on your machine.

You can follow to the next step after you've completed the installations. Visit here for further instruction.

Configure your AWS CLI

To authenticate with your AWS account,

Run the following command

aws configure

Provide the requested information. Example

image1

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

image2

Enter 1 to select the Hello World Example

image3

Enter N to select NO

image4

Select the package you would like to use, in my case , i will be using python3.8

image5

select Zip as the required package

image6

Ignore the following

image7

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

image8

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

image9

The project Structure

What is 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 128mb max.

image10

Resources

The resources contain all created functions and function resources

image11

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.

image12

we can see, 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

image13

Run the code

sam local start-api --port 8080

image14

Open the url in your browser and you see hello world in your browser screen

image15

you can update the message, code and no need to restart the server, it automatically update.

Run Tests

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

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

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

Deploy

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

in other to build your project, Run

sam build

you can now deploy your function to AWS with

sam deploy --guided

fill out the question like the example in the picture, press enter to skip

image16

Once completed, the output will look something like this

image17

You can visit the url provided in the HelloWorldFunction

or use curl

image18

There you go! You have successfully build, test and deploy a serveless 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.