SAM
Links: 102 AWS DVA Index
SAM¶
- It stands for Serverless Application 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: 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 local start-lambda
- Starts a local endpoint that emulates AWS Lambda.
- We can do automated tests against this endpoint.
sam local invoke
- Invoke Lambda function with payload once and quit after invocation completes.
- Helpful for generating test cases
-
SAM Deployment Process
The template you write in SAM is converted to a CloudFormation template. Any time you see the keyword Transform in a template then it means it is a SAM template
Recipe¶
- Transform Header indicates it's SAM template:
- Transform:
AWS::Serverless-2016-10-31
- Transform:
- Write Code
AWS::Serverless::Function
: LambdaAWS::Serverless::Api
: API GatewayAWS::Serverless::SimpleTable
: DynamoDBAWS::Serverless::Application
: SAR
- Package & Deploy:
aws cloudformation package
/sam package
- The command uploads local artifacts to an S3 bucket.
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
- AWS Toolkits: IDE plugins which allows you to build, test, debug, deploy, and invoke Lambda functions built using AWS SAM
SAM Policy Templates¶
- List of templates to apply permissions to your Lambda Functions
- Important examples:
S3ReadPolicy
: Gives read only permissions to objects in S3SQSPollerPolicy
: Allows to poll an SQS queueDynamoDBCrudPolicy
: CRUD
- The idea is to focus on what you want to do rather than focusing on how to do it (like worrying about policies)
SAM & CodeDeploy¶
- SAM framework natively uses CodeDeploy to update Lambda functions
- Pre and Post traffic hooks features to validate deployment (before the traffic shift starts and after it ends)
- Easy & automated rollback using CloudWatch Alarms
SAR (Serverless Application Repository)¶
- Managed repository for serverless applications
- The applications are packaged using SAM
- Build and publish applications that can be re-used by organisations
- 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 customised using Environment variables
SAM Exam Summary¶
- SAM is built on CloudFormation
- SAM requires the Transform and Resources section in its template.
- Commands to know:
sam init
: initialises a serverless application with an AWS SAM templatesam build
: fetch dependencies and create local deployment artifactssam package
: package and upload to Amazon S3, generate CF templatesam deploy
: deploy to CloudFormation
- SAM Policy templates for easy lAM policy definition
- SAM is integrated with CodeDeploy to do deploy to Lambda aliases
Last updated: 2023-03-01