In this blog, we’ll learn how to create, build, and test a serverless application model using the CLI. So, let’s get started.
The AWS Serverless Application Model (SAM) is an open-source framework for developing serverless applications on Amazon Web Services.
It allows us to express functions, APIs, databases, and event source mappings using shorthand syntax. We can define and model our application using YAML with just a few lines per resource. SAM converts and expands SAM syntax into AWS CloudFormation syntax during deployment, allowing us to build serverless applications faster.
- AWS Account
- Python 3.9
- Docker, for testing the application locally
- Homebrew, for Linux and macOS
Step 1: Install the SAM CLI
Download AWS SAM CLI zip file zip file.
Using the following command, generate a hash value to verify the integrity and authenticity of the downloaded installer files:
Unzip the installation files.
unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
Install the AWS SAM CLI.
Verify the installation.
Step 2: Create a SAM Project
For initializing the SAM application run,
There will be some on-screen prompts, so for this example, I’ll choose AWS Quick Start Templates, the runtime and zip package type, and a Hello World Example.
Give a name to the project and it will start cloning and generate the application.
This will create a directory with the same name as the project name we specified. The following is a list of the contents of the project directory:
There are three files that are important:
template.yaml: Contains the AWS SAM template that defines the AWS resources used by our application.
hello_world/app.py: Contains the logic for our Lambda handler.
hello_world/requirements.txt: Used for sam build and contains any Python dependencies that the application requires.
Step 3: Build SAM Application
Move to the project directory, where the sample application’s template.yaml file is located and run this command.
Here, to construct our dependencies, the AWS SAM CLI comes with abstractions for a number of Lambda runtimes and transfers the source code into staging folders so that everything is ready to be packaged and deployed. The
sam build command compiles our application’s dependencies and moves our source code to folders under
.aws-sam/build, where it will be packaged and published to Lambda.
Step 4: Test SAM Application locally
We may find it handy to test locally when developing our application. The sam local command in the AWS SAM CLI allows us to run our application in Docker containers that simulate Lambda’s execution environment.
There are two ways to do it:
- Host our API locally
- Invoke our Lambda function directly
Host our API locally
Run this command.
sam local start-api
The Docker image may take some time to load. We can browse a request to our application after it has loaded.
start-api command creates a local endpoint that serves as a replica of our REST API endpoint. It downloads a local execution container in which we can run our function. The end result is the same as when we used the AWS Cloud to invoke our function.
Invoke our Lambda function directly
Run this command.
sam local invoke "HelloWorldFunction" -e events/event.json
The invoke command calls our Lambda functions directly and accepts input event payloads from us.
In this blog, we have learned how to create, build and test our SAM Application. If you have any doubt you can reach out to me.
To read more about DevOps.