Serverless Application Model (AWS SAM):
The AWS Serverless Application Model (AWS SAM) is an open-source framework that you can use to build serverless applications on AWS.
A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda functionโit can include additional resources such as APIs, databases, and event source mappings.
Benefits:
Single-deployment configuration
Extension of AWS CloudFormation
Built-in best practices
Local debugging and testing
Deep integration with development tools
Prerequisites:
Install the AWS CLI - Click here
Install the AWS SAM CLI - Click here
Architecture Overview:
Download stage:
sam init
Choose AWS Quick Start Templates
Choose Hello World Example
Give y - we are going with python and zip package
Give y - This will enable X-Ray tracing for function
Give project name as firstserverlessapp
This command creates a directory with the name that you provided as the project name. The contents of the project directory are similar to the following:
firstserverlessapp/
โโโ README.md
โโโ events/
โ โโโ event.json
โโโ hello_world/
โ โโโ __init__.py
โ โโโ app.py #Contains your AWS Lambda handler logic.
โ โโโ requirements.txt #Contains any Python dependencies the application requires, used for sam build
โโโ template.yaml #Contains the AWS SAM template defining your application's AWS resources.
โโโ tests/
โโโ unit/
โโโ __init__.py
โโโ test_handler.py
There are three especially important files:
template.yaml: Contains the AWS SAM template that defines your application's AWS resources.
hello_world/app.py: Contains your actual Lambda handler logic.
hello_world/requirements.txt: Contains any Python dependencies that the application requires, and is used for sam build.
Build stage:
sam build
You can see the following top-level tree under .aws-sam:
Deploy stage:
sam deploy --guided
curl https://<restapiid>.execute-api.us-east-1.amazonaws.com/Prod/hello/
{"message": "hello world"}
Test locally:
sam local start-api
curl http://127.0.0.1:3000/hello
Invoke Lambda function:
sam local invoke "HelloWorldFunction" -e events/event.json
Clean up:
aws cloudformation delete-stack --stack-name **sam-app** --region **region**
Replace sam-app with stack name and region with your aws region
Top comments (0)