Understanding the AWS SAM Template Structure
The AWS Serverless Application Model (SAM) provides a simplified way to define serverless applications on AWS. At its core is the SAM template, which is an AWS CloudFormation template with a simplified syntax for defining serverless resources like Lambda functions, API Gateway APIs, and DynamoDB tables.
Key Components of a SAM Template
A SAM template is a YAML or JSON file that describes your serverless application's resources, their configurations, and their relationships. It extends AWS CloudFormation, adding specific resource types and properties tailored for serverless development.
The SAM template acts as the blueprint for your serverless application.
It defines all the AWS resources needed, such as Lambda functions, API endpoints, and databases, along with their configurations and how they interact.
Think of the SAM template as the architectural plan for your serverless application. It specifies every component, from the compute (Lambda functions) to the event sources (API Gateway, SQS queues) and data stores (DynamoDB). This declarative approach ensures consistency and repeatability in deploying your serverless infrastructure.
Core Sections of a SAM Template
A typical SAM template includes several key sections:
AWSTemplateFormatVersion: Specifies the CloudFormation template version. Usually '2010-09-09'.
Transform: Declares the SAM transform, which tells CloudFormation to process SAM-specific resource types. This is typically
AWS::Serverless-2016-10-31
Description: An optional field to provide a human-readable description of the template.
Parameters: Defines input values that you can provide when deploying the stack, allowing for customization.
Resources: The most critical section, where you define your serverless application's AWS resources. This includes
AWS::Serverless::Function
AWS::Serverless::Api
AWS::Serverless::SimpleTable
Outputs: Defines values that you can reference from other templates or use to retrieve information about the deployed stack.
It enables CloudFormation to process SAM-specific resource types.
The `AWS::Serverless::Function` Resource
This is the cornerstone of most serverless applications. It defines an AWS Lambda function and its associated event triggers.
Key properties include:
- Handler: The entry point for your Lambda function (e.g., ).codeapp.lambda_handler
- Runtime: The programming language and version for your function (e.g., ,codepython3.9).codenodejs18.x
- CodeUri: The location of your function's code (e.g., a local directory or S3 URI).
- Events: Defines the triggers that invoke your Lambda function (e.g., an API Gateway HTTP event, an SQS queue event).
- Policies: Grants necessary permissions to your Lambda function to access other AWS services.
A SAM template defines a serverless application as a collection of resources. The AWS::Serverless::Function
resource is central, specifying the code, runtime, and event triggers. For example, an API Gateway event can trigger a Lambda function, which might then interact with a DynamoDB table defined as AWS::Serverless::SimpleTable
. The Transform
section is crucial for SAM to interpret these simplified resource types.
Text-based content
Library pages focus on text content
The `AWS::Serverless::Api` Resource
This resource simplifies the creation of Amazon API Gateway REST APIs. It allows you to define endpoints, methods, and integrations with Lambda functions.
Key properties include:
- StageName: The deployment stage for your API (e.g., ,codeprod).codedev
- DefinitionBody: Specifies the OpenAPI (Swagger) definition for your API, including paths, methods, and request/response models.
- Auth: Configures authorization for your API endpoints.
CodeUri
property in an AWS::Serverless::Function
?It specifies the location of the Lambda function's code.
The `AWS::Serverless::SimpleTable` Resource
This resource provides a simplified way to define Amazon DynamoDB tables. It abstracts away much of the complexity of CloudFormation's
AWS::DynamoDB::Table
Key properties include:
- PrimaryKey: Defines the primary key for the table, including its name and type.
- ProvisionedThroughput: Configures read and write capacity units (for provisioned mode).
- SSESpecification: Configures server-side encryption for the table.
The SAM CLI is your essential tool for working with SAM templates. It allows you to build, test, and deploy your serverless applications locally and to AWS.
Putting It All Together: A Simple Example
Consider a simple API that returns 'Hello, World!' using a Lambda function. A SAM template might look like this:
AWSTemplateFormatVersion: '2010-09-09'Transform: AWS::Serverless-2016-10-31Description: A simple Hello World APIResources:HelloWorldFunction:Type: AWS::Serverless::FunctionProperties:FunctionName: HelloWorldFunctionHandler: app.lambda_handlerRuntime: python3.9CodeUri: hello_world/Events:HelloWorldApi:Type: ApiProperties:Path: /helloMethod: getOutputs:HelloWorldApiUrl:Description: "API Gateway endpoint URL for Prod stage for Hello World API"Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello"
AWS::Serverless::Api
?Amazon API Gateway.
Learning Resources
The official AWS documentation providing a comprehensive overview of SAM, its concepts, and how to use it.
Detailed explanation of the structure and syntax of SAM templates, including all resource types and properties.
Guides on installing and using the AWS SAM Command Line Interface (CLI) for building, testing, and deploying serverless applications.
Essential information about AWS Lambda, including how to write, deploy, and manage Lambda functions.
Learn how to create, publish, maintain, monitor, and secure APIs with API Gateway.
Comprehensive guide to Amazon DynamoDB, a fast and flexible NoSQL database service for all applications that need seamless scalability.
A video explaining the benefits and usage of AWS SAM for building serverless applications, often featuring template examples.
A comparative analysis of AWS SAM and the Serverless Framework, highlighting their differences and use cases.
Explains the concept of CloudFormation transforms, which are essential for understanding how SAM templates are processed.
The official resource for the OpenAPI Specification, which is used to define the structure of APIs within SAM templates.