15.SAM (Server-less Application Model)
Index
- Introduction
- SAM-Recipe
- CLI Debugging
- Policy template
- SAM and Code deploy
- Exam Summary
- SAR(Server-less Application Repository)
Introduction
- 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: Outputs, Mappings, Parameters, Resources…
- Only two commands to deploy to AWS
- SAM can use CodeDeploy to deploy Lambda functions
- SAM can help you to run Lambda, API Gateway, DynamoDB locally
SAM-Recipe
- Transform Header indicates it’s SAM template:
- Transform: ‘AWS::Serverless-2016-10-31’
- Write Code
- AWS::Serverless::Function
- AWS::Serverless::Api
- AWS::Serverless::SimpleTable
- Package & Deploy
- aws cloudformation package / sam package
- aws cloudformation deploy / sam deploy
CLI Debugging
- Locally build, test, and debug your serverless applications that are defined using AWS SAM templates
- Provides a lambda-like execution environment locally
- SAM CLI + AWS Toolkits => step-through and debug your code
- Supported IDEs: AWS Cloud9, Visual Studio Code, JetBrains, PyCharm, IntelliJ, …
- AWS Toolkits: IDE plugins which allows you to build, test, debug, deploy, and invoke Lambda functions built using AWS SAM
Policy template
- List of templates to apply permissions to your Lambda Functions
- Important examples:
- S3ReadPolicy: Gives read only permissions to objects in S3
- SQSPollerPolicy: Allows to poll an SQS queue
- DynamoDBCrudPolicy: CRUD = create read update delete

SAM and Code deploy
- SAM framework natively uses CodeDeploy to update Lambda functions
- Traffic Shifting feature
- Pre and Post traffic hooks features to validate deployment (before the traffic shift starts and after it ends)
- Easy & automated rollback using CloudWatch Alarms
Exam Summary
- SAM is built on CloudFormation
- SAM requires the Transform and Resources sections
- Commands to know
- sam build: fetch dependencies and create local deployment artifacts
- sam package: package and upload to Amazon S3, generate CF template
- sam deploy: deploy to CloudFormation
- SAM Policy templates for easy IAM policy definition
- SAM is integrated with CodeDeploy to do deploy to Lambda aliases
SAR(Server-less Application Repository)
- Managed repository for serverless applications
- The applications are packaged using SAM
- Build and publish applications that can be re-used by organizations
- Can share publicly
- Can share with specific AWS accounts
- This prevents duplicate work, and just go straight to publishing
- Application settings and behaviour can be customized using Environment variables