1. What is SAM?

AWS SAM is an open-source framework for building serverless applications. It extends CloudFormation with simplified syntax for defining Lambda functions, API Gateway APIs, DynamoDB tables, and event sources.

Core Concept SAM = simplified CloudFormation for serverless. Instead of writing 50+ lines of CF to define a Lambda function + API Gateway + IAM role + log group, SAM does it in ~10 lines. SAM templates are CloudFormation templates with a Transform header that enables the shorthand syntax. SAM also provides a CLI for local testing.

2. SAM Template

# SAM Template (template.yaml)
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My Serverless API

Globals:
  Function:
    Timeout: 30
    Runtime: nodejs18.x
    MemorySize: 128

Resources:
  GetOrdersFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/orders.getAll
      CodeUri: ./src
      Events:
        GetOrders:
          Type: Api
          Properties:
            Path: /orders
            Method: get

  CreateOrderFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/orders.create
      CodeUri: ./src
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref OrdersTable
      Events:
        CreateOrder:
          Type: Api
          Properties:
            Path: /orders
            Method: post

  OrdersTable:
    Type: AWS::Serverless::SimpleTable
    Properties:
      PrimaryKey:
        Name: orderId
        Type: String

What SAM Auto-Creates

  1. The above template automatically creates: 2 Lambda functions, API Gateway REST API with /orders routes, IAM execution roles for each Lambda, CloudWatch Log Groups, DynamoDB table
  2. In raw CloudFormation, this would be 200+ lines. In SAM, it’s ~40 lines.

3. SAM Resource Types

4 SAM Event Sources

  1. Api: API Gateway REST API trigger
  2. HttpApi: API Gateway HTTP API trigger
  3. Schedule: EventBridge scheduled event (cron/rate)
  4. S3: S3 bucket event notification
  5. SQS: SQS queue polling (event source mapping)
  6. SNS: SNS topic subscription
  7. Kinesis: Kinesis stream polling
  8. DynamoDB: DynamoDB Streams polling
  9. CloudWatchEvent: EventBridge rule trigger
  10. IoTRule: IoT Core rule trigger

5 SAM Policy Templates

SAM provides pre-built IAM policy templates for common patterns, so you don’t write IAM policies manually.

6. SAM CLI

SAM Local Testing sam local invoke and sam local start-api use Docker to simulate Lambda and API Gateway on your local machine. This enables testing serverless applications without deploying to AWS. Extremely useful for rapid development and debugging.

7. CloudFormation vs CDK vs SAM

  1. CDK and SAM can be used together: CDK can include SAM constructs, and sam can work with CDK apps
  2. All three ultimately use CloudFormation for provisioning
Exam Tip SAM: "Simplified serverless IaC" = SAM. "Transform: AWS::Serverless" = SAM template. "Local Lambda testing" = sam local invoke. "Local API testing" = sam local start-api. SAM auto-creates: Lambda + IAM role + API GW + log groups. Policy templates = pre-built IAM. SAM is a CloudFormation extension (not a replacement). "Any AWS resource IaC" = CloudFormation or CDK. "Serverless-specific IaC" = SAM.