Serverless Application Model (SAM)
Mascot

About
Transform header indicates it's SAM model.
Framework for developing and deploying serverless applications.
All the configuration is
YAMLcode.Generate complex CloudFormation from simple SAM
YAMLfile.Supports anything from CloudFormation such as
Outputs,Mappings,Parameters,Resourcesetc.SAM can use
CodeDeployto deploy Lambda functions.SAM can help you to run Lambda, API Gateway, DynamoDB locally.
SAM Recipe
Transform Header indicates it's SAM template.
Write code using SAM constructs
Once SAM template YAML file is created, use
sam deploycommand ( optionally can usesam packagecommand).SAM commands helps to build and deploy sam template.
Can use
sam sync --watchcommand to quickly sync local changes to AWS Lambda usingSAM Accelerate.
Deep Dive
SAML Template YAML is first build using
sam buildcommand.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 deploycommand to deploy or execute the changeset.CloudFormation stack is made up of
Lambda,API GatewayandDynamoDB.
SAM Accelerate
It is a set of features to reduce latency while deploying resources to AWS.
sam syncsynchronizes 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 command
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
SAM policy templates
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.
S3ReadPolicySQSPollerPolicyDynamoDBCrudPolicy
Local Capability
Lambda
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 invokecommand.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
--profileoption.
Generate Event
Generate events for lambda functions using command
sam local generate-event.
API Gateway
Locally start an API Gateway endpoint using the command
sam local start-api.Starts a local
HTTPserver that hosts all your functions.Changes to functions are automatically reloaded.
Multiple environments
TOML file can be used to configure environment specific parameters.
This configuration file can be passed while deploying resources using
SAMcommand line.Command can be given as follows,
Integrations
SAM and CodeDeploy
SAM Framework natively uses CodeDeploy to update and LambdaFunctions.
Traffic Shifting features using Alias is used for update.
AutoPublishAliasParameter inSAMtemplate 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
CloudWatchAlarms.Also has capability to provide Deployment Preference like
Canary,Linear,AllAtOnce.Alarms can trigger a rollback.
Last updated