AWS SAM (Serverless Application Model) templates are an extension of AWS CloudFormation templates specifically designed to simplify the deployment of serverless applications. SAM provides a shorthand syntax for defining serverless resources, making it easier to create and manage Lambda functions, A…AWS SAM (Serverless Application Model) templates are an extension of AWS CloudFormation templates specifically designed to simplify the deployment of serverless applications. SAM provides a shorthand syntax for defining serverless resources, making it easier to create and manage Lambda functions, API Gateway endpoints, DynamoDB tables, and other serverless components.
A SAM template is a YAML or JSON file that begins with the declaration 'Transform: AWS::Serverless-2016-10-31', which tells CloudFormation to process the template using SAM transformations. This transform converts SAM-specific syntax into standard CloudFormation resources during deployment.
Key resource types in SAM templates include:
1. AWS::Serverless::Function - Defines Lambda functions with properties like runtime, handler, code location, memory, timeout, and event triggers.
2. AWS::Serverless::Api - Creates API Gateway REST APIs with simplified configuration for routes and methods.
3. AWS::Serverless::SimpleTable - Provides a quick way to create DynamoDB tables with basic configurations.
4. AWS::Serverless::LayerVersion - Defines Lambda layers for sharing code across functions.
SAM templates support the Globals section, allowing you to define common properties shared across multiple resources, reducing repetition in your template.
The SAM CLI tool works alongside templates to enable local testing of Lambda functions and APIs before deployment. Common SAM CLI commands include 'sam init' for creating new projects, 'sam build' for preparing deployment packages, 'sam local invoke' for testing functions locally, and 'sam deploy' for deploying to AWS.
During deployment, SAM templates are transformed into full CloudFormation templates, which then provision the actual AWS resources. SAM integrates with CodeDeploy for gradual deployments and supports traffic shifting strategies like Canary and Linear deployments for Lambda functions, enabling safe production releases with automatic rollback capabilities if errors occur.
AWS SAM Templates - Complete Guide for AWS Developer Associate Exam
What is AWS SAM?
AWS Serverless Application Model (SAM) is an open-source framework designed to build serverless applications on AWS. SAM templates are an extension of AWS CloudFormation templates that provide a simplified syntax for defining serverless resources such as Lambda functions, API Gateway APIs, and DynamoDB tables.
Why AWS SAM is Important
Understanding AWS SAM is crucial for several reasons:
• Simplified Syntax: SAM reduces the amount of code needed to define serverless resources compared to standard CloudFormation • Local Testing: SAM CLI allows developers to test Lambda functions locally before deployment • Built-in Best Practices: SAM templates automatically configure permissions and event sources following AWS best practices • Integration with CI/CD: SAM integrates seamlessly with AWS CodePipeline and CodeBuild for automated deployments • Cost Efficiency: Enables rapid development and deployment of serverless applications
How AWS SAM Works
SAM operates through a transformation process:
1. Template Creation: You write a SAM template (template.yaml) using simplified SAM syntax 2. Transform Declaration: The template includes Transform: AWS::Serverless-2016-10-31 at the top 3. SAM CLI Commands: Use sam build to prepare the application and sam deploy to deploy 4. CloudFormation Conversion: AWS transforms SAM templates into full CloudFormation templates during deployment 5. Resource Provisioning: CloudFormation creates and manages all specified resources
Key SAM Resource Types
• AWS::Serverless::Function - Defines Lambda functions with event sources • AWS::Serverless::Api - Creates API Gateway REST APIs • AWS::Serverless::HttpApi - Creates API Gateway HTTP APIs • AWS::Serverless::SimpleTable - Creates DynamoDB tables with basic configuration • AWS::Serverless::LayerVersion - Defines Lambda layers • AWS::Serverless::Application - Embeds applications from AWS Serverless Application Repository
Essential SAM Template Structure
A basic SAM template includes:
• AWSTemplateFormatVersion - Template format version • Transform - Must be AWS::Serverless-2016-10-31 • Globals - Optional section for common configuration across resources • Resources - Required section defining serverless resources • Parameters, Mappings, Outputs - Optional CloudFormation sections
SAM CLI Key Commands
• sam init - Initialize a new SAM project • sam build - Build the application and prepare deployment artifacts • sam local invoke - Test Lambda functions locally • sam local start-api - Run API Gateway locally for testing • sam deploy - Deploy the application to AWS • sam package - Package application and upload to S3
SAM Policy Templates
SAM includes predefined policy templates for common scenarios: • S3ReadPolicy - Read access to S3 bucket • DynamoDBCrudPolicy - CRUD operations on DynamoDB • SQSPollerPolicy - Poll messages from SQS queue • LambdaInvokePolicy - Invoke another Lambda function
Exam Tips: Answering Questions on AWS SAM Templates
Remember these key points for the exam:
1. Transform Header: Always remember that SAM templates require the Transform: AWS::Serverless-2016-10-31 declaration
2. SAM vs CloudFormation: Know that SAM is an extension of CloudFormation, not a replacement. SAM templates are converted to CloudFormation during deployment
3. Packaging Commands: Remember sam package uploads artifacts to S3 and outputs a packaged template. The command aws cloudformation package performs the same function
4. Deployment Commands: Know both sam deploy and aws cloudformation deploy can deploy SAM applications
5. Local Testing: Questions about testing Lambda locally should point to SAM CLI with sam local invoke or sam local start-api
6. CodeUri Property: Understand that CodeUri in AWS::Serverless::Function points to the location of function code
• When asked about the simplest way to deploy serverless applications, SAM is typically the answer • Questions about local Lambda testing point to SAM CLI • If a question mentions Transform: AWS::Serverless-2016-10-31, it is referring to SAM • Questions about reducing template complexity for serverless apps suggest using SAM over raw CloudFormation