LibrarySAM Template Structure

SAM Template Structure

Learn about SAM Template Structure as part of Serverless Architecture with AWS Lambda

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

code
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

code
AWS::Serverless::Function
,
code
AWS::Serverless::Api
,
code
AWS::Serverless::SimpleTable
, etc.

Outputs: Defines values that you can reference from other templates or use to retrieve information about the deployed stack.

What is the primary purpose of the 'Transform' section in a SAM template?

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.,
    code
    app.lambda_handler
    ).
  • Runtime: The programming language and version for your function (e.g.,
    code
    python3.9
    ,
    code
    nodejs18.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.,
    code
    prod
    ,
    code
    dev
    ).
  • DefinitionBody: Specifies the OpenAPI (Swagger) definition for your API, including paths, methods, and request/response models.
  • Auth: Configures authorization for your API endpoints.
What is the purpose of the 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

code
AWS::DynamoDB::Table
resource.

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:

yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A simple Hello World API
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: HelloWorldFunction
Handler: app.lambda_handler
Runtime: python3.9
CodeUri: hello_world/
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /hello
Method: get
Outputs:
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"
What AWS service is typically used in conjunction with AWS::Serverless::Api?

Amazon API Gateway.

Learning Resources

AWS Serverless Application Model (SAM) Developer Guide(documentation)

The official AWS documentation providing a comprehensive overview of SAM, its concepts, and how to use it.

AWS SAM Template Structure(documentation)

Detailed explanation of the structure and syntax of SAM templates, including all resource types and properties.

AWS SAM CLI Documentation(documentation)

Guides on installing and using the AWS SAM Command Line Interface (CLI) for building, testing, and deploying serverless applications.

AWS Lambda Developer Guide(documentation)

Essential information about AWS Lambda, including how to write, deploy, and manage Lambda functions.

AWS API Gateway Developer Guide(documentation)

Learn how to create, publish, maintain, monitor, and secure APIs with API Gateway.

AWS DynamoDB Developer Guide(documentation)

Comprehensive guide to Amazon DynamoDB, a fast and flexible NoSQL database service for all applications that need seamless scalability.

Building Serverless Applications with AWS SAM - AWS Online Tech Talk(video)

A video explaining the benefits and usage of AWS SAM for building serverless applications, often featuring template examples.

Serverless Framework vs AWS SAM: A Comparison(blog)

A comparative analysis of AWS SAM and the Serverless Framework, highlighting their differences and use cases.

Understanding CloudFormation Transforms(documentation)

Explains the concept of CloudFormation transforms, which are essential for understanding how SAM templates are processed.

OpenAPI Specification(documentation)

The official resource for the OpenAPI Specification, which is used to define the structure of APIs within SAM templates.