2. AWS SAM — Serverless Application Model

📅 Updated: Nov 14, 2025 ⏱️ 8 min read 👤 Tech Writing Team

AWS Serverless Application Model (SAM) is an open-source framework that simplifies building, testing, and deploying serverless applications on AWS. Built on top of AWS CloudFormation, SAM provides shorthand syntax specifically designed for serverless resources like Lambda functions, APIs, event mappings, and permissions.

ℹ️ Note

Sam is fully compatible with standard CloudFormation templates. You can mix SAM shorthand syntax with native CloudFormation resources in the same template.

Prerequisites

Before diving into AWS SAM, ensure you have the following installed and configured:

Terminal
sam --version
# Expected output: SAM CLI, version 1.105.0
aws --version

Initializing a Project

Start by scaffolding a new SAM project. The CLI provides several templates out of the box:

bash
sam init --runtime nodejs18.x --name aevum-serverless-api --app-template hello-world

This creates a standardized directory structure:

Directory Tree
aevum-serverless-api/
├── .aws-sam/
├── events/
│   └── event.json
├── hello_world/
│   ├── app.py
│   └── requirements.txt
├── samconfig.toml
├── template.yaml
└── tests/
    └── unit/
        └── test_handler.py

Understanding the Template

The template.yaml file is the heart of your SAM application. It defines resources, events, and permissions using simplified syntax:

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Aevum API Gateway + Lambda example

Resources:
  GetDataFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.9
      Handler: app.lambda_handler
      CodeUri: hello_world/
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /data/{id}
            Method: get
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref DataStore

  DataStore:
    Type: AWS::Serverless::SimpleTable
    Properties:
      PrimaryKey:
        Name: id
        Type: String
💡 Pro Tip

Use AWS::Serverless::SimpleTable instead of raw AWS::DynamoDB::Table to automatically get a standard string primary key and pay-per-request billing.

Building & Testing Locally

SAM leverages Docker to emulate the Lambda execution environment locally. This ensures near-identical behavior to production:

Terminal
# Build dependencies into container images
sam build

# Start local API Gateway emulator
sam local start-api

# Test endpoint
curl http://localhost:3000/data/test123

The local emulator supports API Gateway v1 & v2, event mapping, and VPC networking simulation. Use --warm-containers EAGER to avoid cold start delays during rapid iteration.

Deploying to AWS

Once validated, deploy your application with the guided CLI command. It automatically creates or updates a CloudFormation stack, provisions IAM roles, and outputs your endpoint URLs:

Terminal
sam deploy --guided
⚠️ Security Warning

Never hardcode AWS credentials or secrets in your template. Use AWS Secrets Manager, Parameter Store, or environment variables injected at deploy time via --parameter-overrides.

Best Practices

Next Steps

Now that you understand the core workflow of AWS SAM, proceed to the next module to learn how to containerize your functions and run advanced local simulations with Docker networking and VPC emulation.