Serverless Application Model (SAM)
Last updated
Last updated
Transform header indicates it's SAM model.
Framework for developing and deploying serverless applications.
All the configuration is YAML
code.
Generate complex CloudFormation from simple SAM YAML
file.
Supports anything from CloudFormation such as Outputs
, Mappings
, Parameters
, Resources
etc.
SAM can use CodeDeploy
to deploy Lambda functions.
SAM can help you to run Lambda, API Gateway, DynamoDB locally.
Transform Header indicates it's SAM template.
Write code using SAM constructs
Once SAM template YAML file is created, use sam deploy
command ( optionally can use sam package
command).
SAM commands helps to build and deploy sam template.
Can use sam sync --watch
command to quickly sync local changes to AWS Lambda using SAM Accelerate
.
SAML Template YAML is first build using sam build
command.
After build phase, cloud formation template (YAML) file is generated out of it.
Zip and upload S3 the artifacts generated to S3 bucket.
Use sam deploy
command to deploy or execute the changeset.
CloudFormation stack is made up of Lambda
, API Gateway
and DynamoDB
.
It is a set of features to reduce latency while deploying resources to AWS.
sam sync
synchronizes the project declared in SAM templates to AWS.
It can synchronize code changes to AWS without updating infrastructure (internally it uses service APIs and bypass CloudFormation).
sync
commandsam sync
Synchronizes code and infrastructure
sam sync --code
Synchronizes code changes without updating infrastructure (bypass CloudFormation, update in seconds)
sam sync --code --resource <resource>
Synchronize only a specific resource and their dependencies
sam sync --code --resource-id <resource-id>
Synchronize only resource with specific resource id and their dependencies
sam sync --watch
Monitor for file changes and automatically synchronize when changes are detected.
If includes configuration changes, it uses sam sync
It is a list of templates to apply permission to your Lambda functions.
Policy templates list can be found here.
From the above policy list following are important.
S3ReadPolicy
SQSPollerPolicy
DynamoDBCrudPolicy
Start Lambda
By using SAM framework, you can start AWS Lambda locally.
This will start a local endpoint that emulates AWS Lambda.
This can be used to run automated test against local endpoint.
Command : sam local start-lambda
.
Invoke Lambda
Local invocation can be done using sam local invoke
command.
Local invocation will invoke lambda once and quit after invocation completes.
Helpful for generating test cases.
If lambda tries to access some other AWS resource make sure to pass the relevant profile using the --profile
option.
Generate Event
Generate events for lambda functions using command sam local generate-event
.
Locally start an API Gateway endpoint using the command sam local start-api
.
Starts a local HTTP
server that hosts all your functions.
Changes to functions are automatically reloaded.
TOML file can be used to configure environment specific parameters.
This configuration file can be passed while deploying resources using SAM
command line.
Command can be given as follows,
SAM Framework natively uses CodeDeploy to update and LambdaFunctions.
Traffic Shifting features using Alias is used for update.
AutoPublishAlias
Parameter in SAM
template file takes care of this.
Pre and Post traffic hooks feature to validate deployment (before traffic shift starts and after it ends).
Easy and automated rollbacks using CloudWatch
Alarms.
Also has capability to provide Deployment Preference like Canary
, Linear
, AllAtOnce
.
Alarms can trigger a rollback.