AWS SAM (Serverless Application Model) is an open-source framework to define serverless applications using YAML.
Features
- SAM helps you to create serverless applications using simple YAML configuration.
- In SAM, you can use all CloudFormation features.
- SAM can use CodeDeploy to deploy Lambda functions.
- Traffic Shift with aliases
- PreTraffic and PostTraffic hooks
How it Works
- You can provide the simplified instruction to define your application
- Lambda function: AWS::Serverless::Function
- API: AWS::Serverless::Api
- DynamoDB: AWS::Serverless::SimpleTable
- AWS SAM transforms the instructions into the fully detailed CloudFormation template.
- AWS CloudFormation uses the transformed template to build the stack.
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2021-07-11
Description: A hello world application.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: main.handler
Runtime: python3.9
CodeUri: ./src
AutoPublishAlias: live
DeploymentPreferences:
Type: Canary10Percent10Minutes # Canary, Linear, AllAtOnce
Hooks:
PreTraffic: !Ref PreTrafficLambdaFunc
PostTraffic: !Ref PostTrafficLambdaFunc
AWS SAM CLI
You can use the AWS SAM CLI utility to test and deploy your application.
- SAM CLI launches a Docker container.
- You can use Cloud9 Environment – Linux 2 instance – to use the pre-installed SAM CLI.
cat /etc/os-release
sam --version
sam validate
Lambda Deployment – SAM & CodeDeploy
When you deploy Lamdba functions, SAM uses CodeDeploy under the hood. So you can leverage CodeDeploy functionalities using SAM when deploying Lambda functions.
- Traffic Shifting
- PreTraffic and PostTraffic hooks
- Automatic rollbacks using CloudWatch alarms
AutoPublishAlias
- Creates and publish a new version of a function when the new code is uploaded
- Update the alias to point the updated version of the Lambda function
DeploymentPreference
- Type
- AllAtOnce, Linear, and Canary
- Alarms
- alarms that can trigger rollback
- Hooks
- PreTraffic
- PostTraffic
