AWS Serverless Application Model (SAM) Cheat Sheet
The AWS Serverless Application Model (SAM) is an open-source framework that simplifies the process of building and deploying serverless applications on AWS. It provides a shorthand syntax to declare serverless resources, making it easier and faster than using AWS CloudFormation directly.
Core Concepts
- What it is: An extension of AWS CloudFormation specifically designed for serverless applications. You define your application in a SAM template file, and SAM transforms it into a standard CloudFormation stack during deployment.
- Why use it: It reduces the amount of code needed to define common serverless resources like Lambda functions, API Gateway APIs, and DynamoDB tables. This leads to cleaner templates and a more streamlined development workflow.
- Key Components:
- SAM Template: A YAML or JSON file where you define your serverless application's resources.
- SAM CLI: A command-line interface (CLI) tool for creating, building, testing, packaging, and deploying your SAM applications.
The SAM Template
A SAM template is a CloudFormation template with special SAM-specific resource types. During deployment, SAM expands these resources into their more verbose CloudFormation equivalents.
Key SAM Resource Types
-
AWS::Serverless::Function
- Defines a Lambda function. It automatically creates the Lambda function itself, an IAM execution role with necessary permissions, and configures event sources.
- Example:
Resources: MyLambdaFunction: Type: AWS::Serverless::Function Properties: Handler: index.handler Runtime: nodejs20.x CodeUri: src/ Events: MyApiEvent: Type: Api Properties: Path: /hello Method: get
-
AWS::Serverless::Api
- Defines a collection of Amazon API Gateway resources and methods. It provides a simplified way to configure endpoints for your Lambda functions.
-
AWS::Serverless::SimpleTable
- Defines a DynamoDB table with a single primary key. It's a quick way to create a simple NoSQL database for your application.
-
AWS::Serverless::HttpApi
- Defines an Amazon API Gateway HTTP API, which is a lighter-weight, lower-latency, and more cost-effective alternative to REST APIs.
The SAM CLI
The SAM CLI is an essential tool for local development, testing, and deployment of serverless applications.
Common SAM CLI Commands
-
sam init
- Initializes a new serverless application with a pre-configured template and folder structure. You can choose from several runtimes and starter templates.
sam init --runtime nodejs20.x --name my-sam-app
-
sam build
- Builds your application by packaging your code, resolving dependencies, and preparing it for deployment. It processes your
template.yaml
file and places the build artifacts into a.aws-sam/build
directory. sam build
- Builds your application by packaging your code, resolving dependencies, and preparing it for deployment. It processes your
-
sam local invoke <FunctionLogicalId>
- Invokes a Lambda function locally in a Docker container that simulates the AWS Lambda environment. This is great for quick, iterative testing without deploying to the cloud.
sam local invoke MyLambdaFunction --event events/event.json
-
sam local start-api
- Starts a local API Gateway endpoint on your machine that serves your Lambda functions. This allows you to test your API endpoints using tools like
curl
or Postman. sam local start-api
- Starts a local API Gateway endpoint on your machine that serves your Lambda functions. This allows you to test your API endpoints using tools like
-
sam package
- Packages your application code and dependencies into a ZIP file, uploads it to an S3 bucket, and produces a new template file with the S3 location of your code. (Note:
sam deploy
now handles this implicitly).
- Packages your application code and dependencies into a ZIP file, uploads it to an S3 bucket, and produces a new template file with the S3 location of your code. (Note:
-
sam deploy --guided
- Deploys your serverless application to the AWS cloud by creating and managing a CloudFormation stack.
- The
--guided
flag walks you through a series of prompts to configure the deployment, such as stack name, region, and whether to confirm changes before deploying. These settings are then saved in asamconfig.toml
file for future deployments.
Typical Development Workflow
- Initialize: Create a new project with
sam init
. - Develop: Write your Lambda function code and define your resources in the
template.yaml
file. - Test Locally: Use
sam local invoke
andsam local start-api
to test your functions and API endpoints on your local machine. - Build: Run
sam build
to prepare your application for deployment. - Deploy: Deploy the application to your AWS account using
sam deploy --guided
. - Iterate: Make changes to your code, test locally, and redeploy with
sam deploy
.