Serverless Application Model (SAM)

Sam is both an AWS CLI tool and a CloudFormation Macro which makes it effortless to define and deploy serverless applications.

CloudFormation allows you to specify macros through the Transform attribute. This is how SAM is used.

Transform: 'AWS::Serverless-2016-10-31'

Sam gives you new Resource types:

  • AWS::Serverless::Function
  • AWS::Serverless::API
  • AWS::Serverless::SimpleTable

SAM vs CloudFormation

You write at least 50% less code with Sam

SAM CLI

sam build Prepare the lambda source code to be deployed by packaging it for upload

sam deploy Upload the lambda package code (artifact) and then deploy

sam init Initialize a new serverless project, creating all the folders an files to get started

sam local generate-event Generates sample payloads from different event sources, such as Amazon $3, Amazon API Gateway, and Amazon SNS

sam local invoke Run a single lambda locally

sam local start-api run your serverless application locally for quick development and testing

sam local start-lambda Run a single lambda locally (much easier)

sam logs Fetches logs that are generated by your Lambda function.

sam package Packages an AWS SAM application. Creates a zip, and uploads it to S3

sam publish Publishes your packaged SAM application to AWS Serverless Application Repository

sam validate Validates an AWS SAM template (great for finding syntax hugs)