AWS SAM - Build, Test and Deploy Serveless Applications.
By abdulmumin yaqeen
on May 15, 2023

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.
- Docket - It simulates the running environment of Lambda in your local machine
visit here to download - AWS CLI - Allows you to authenticate and communicate with your AWS account.
visit here to download - SAM CLI - Enable you to create, test and deploy serverless applications.
visit here to download - Git - For versioning and tracking.
visit here to download
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,
- Navigate to yout IAM management console
- Select your USER
- in the Security tab, locate Access keys.
- Generate a new Access Key and Download the CSV Provided.
Run the following command
aws configure
Provide the requested information. Example
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
Enter 1 to select the Hello World Example
Enter N to select NO
Select the package you would like to use, in my case , i will be using python3.8
select Zip as the required package
Ignore the following
Enter project name, a default name is already provided , you can overwrite with yours or click Enter to continue.
Once completed, you can open the created folder in your favourite code editor
The project Structure
- events folder - Contain events template that you can use to invoke the function.
- hello_world folder - Contains code for the lambda function.
- tests folder - Contain tests for the application code.
- template.yml - Template that defines the application resources, functions and others
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.
Resources
The resources contain all created functions and function resources
In this Resource, we have a function called HelloWorldFunction.
Let's break down some properties.
- CodeUri - here you specify folder where you store the code
- Handler - Here you specify the entry point to this function
- Runtime - The runtime of this function
The Code
in the app.py file, the default code should look something like this.
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
Run the code
sam local start-api --port 8080
Open the url in your browser and you see hello world in your browser screen
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
Once completed, the output will look something like this
You can visit the url provided in the HelloWorldFunction
or use curl
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.