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:
Docker - It simulates the running environment of Lambda on 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 - Enables you to create, test, and deploy serverless applications.
Visit here to download.
Git - For versioning and tracking.
Visit here to download.
You can proceed to the next step after completing the installations.
Configure your AWS CLI
To authenticate with your AWS account:
Navigate to your IAM management console.
Select your USER.
In the Security tab, locate Access keys.
Generate a new Access Key and download the provided CSV.
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 Python 3.8.
Select Zip as the required package.
Ignore the following.
Enter a project name. A default name is already provided, you can overwrite it with yours or click Enter to continue.
Once completed, you can open the created folder in your favorite code editor.
The project Structure
events folder - Contains event templates that you can use to invoke the function.
hello_world folder - Contains code for the lambda function.
tests folder - Contains tests for the application code.
template.yml - Template that defines the application resources, functions, and others.
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.
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:
The Code
In the app.py file, the default code should look something like this:
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
Run the code
sam local start-api --port 8080
Open the URL in your browser and you will see "hello world" on your browser screen.
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.
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 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.