AWS Serverless Application Model (SAM) is an open-source framework that simplifies building and deploying serverless applications on AWS. This beginner-friendly guide will walk you through everything you need to know to start building with AWS SAM, from installation to deployment.

Explaining AWS SAM to a 6-Year-Old

Imagine building with LEGO blocks. AWS SAM is like having special LEGO instructions and tools that help you build amazing structures faster. Instead of figuring out each piece individually, SAM gives you pre-designed sections (templates) and a helper robot (CLI) that builds everything for you when you’re ready!

What is AWS SAM?

AWS SAM is an extension of AWS CloudFormation that provides simplified syntax specifically for serverless applications. It allows you to define serverless resources like:

  • AWS Lambda functions
  • API Gateway APIs
  • Amazon DynamoDB tables
  • Amazon S3 buckets
  • Event sources (SQS, SNS, etc.)

Why Use AWS SAM?

FeatureTraditional ApproachWith AWS SAM
Development SpeedManual configurationAccelerated with templates
Local TestingDifficult to simulateEasy with SAM CLI
DeploymentComplex scriptsSingle command
Best PracticesManual implementationBuilt-in patterns
Learning CurveSteep for beginnersSimplified for new users

Getting Started with AWS SAM

Install AWS SAM CLI

The SAM CLI is the primary tool for working with AWS SAM applications. Install it based on your operating system:


# For macOS using Homebrew
brew tap aws/tap
brew install aws-sam-cli

# For Windows using Chocolatey
choco install aws-sam-cli

# For Linux
curl -L https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip -o aws-sam-cli.zip
unzip aws-sam-cli.zip -d sam-installation
sudo ./sam-installation/install

Initialize a New SAM Project

Create your first serverless application using SAM’s initialization command:


sam init

# Follow the prompts to choose:
# 1. AWS Quick Start Templates
# 2. Hello World Example
# 3. Python 3.12 (or your preferred runtime)

Explore the Project Structure

A basic SAM project includes these key files:


my-sam-app/
├── template.yaml # SAM configuration
├── hello_world/ # Lambda function code
│ ├── app.py
│ └── requirements.txt
├── tests/ # Unit tests
│ └── unit/...
└── events/ # Test event payloads
└── event.json

Understanding template.yaml

The template.yaml file is the heart of your SAM application. Here’s a simplified version:


# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Simple SAM application

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.12
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /hello
Method: get

Building Your First SAM Application

Test Locally

AWS SAM CLI lets you test your functions locally before deploying:


sam local start-api

# Output:
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions

Visit http://localhost:3000/hello to test your function

Build Your Application

Build your application to prepare it for deployment:


sam build

# Output:
Building codeuri: hello_world runtime: python3.12 metadata: {} architecture: x86_64
Running PythonPipBuilder:ResolveDependencies
Build Succeeded

Deploy to AWS

Deploy your application to AWS with a single command:


sam deploy --guided

# Follow the interactive prompts to:
# - Enter a stack name
# - Select AWS region
# - Confirm changes
# - Allow SAM CLI IAM role creation

AWS SAM CLI Essential Commands

Master these commands to become productive with SAM:

sam build

Builds your application and prepares it for deployment:

  • Installs dependencies
  • Creates deployment artifacts
  • Use --use-container for consistent builds

sam deploy

Deploys your application to AWS:

  • Packages and uploads code
  • Creates/updates CloudFormation stack
  • Use --guided for first-time setup

sam local

Test your functions locally:

  • start-api: Simulate API Gateway
  • invoke: Directly invoke a function
  • generate-event: Create sample events

sam logs

Fetch logs for your Lambda functions:

  • -n FunctionName: Specify function
  • --tail: Continuously stream logs
  • --filter: Search for specific terms

Best Practices for AWS SAM Beginners

Project Organization

  • Group related functions in separate directories
  • Use a consistent naming convention
  • Store environment variables in parameter store
  • Separate dev/test/prod environments

Security Fundamentals

  • Apply the principle of least privilege to IAM roles
  • Use AWS Secrets Manager for sensitive data
  • Enable AWS CloudTrail for auditing
  • Regularly update dependencies

Performance Tips

  • Keep Lambda functions focused and small
  • Use Lambda layers for shared code
  • Optimize package size to reduce cold starts
  • Set appropriate memory and timeout values

Real-World Example: API Backend

Here’s a more advanced template for a CRUD API with DynamoDB:


Resources:
ItemsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
BillingMode: PAY_PER_REQUEST

GetItemFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: get_function/
Handler: app.handler
Runtime: nodejs18.x
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ItemsTable
Environment:
Variables:
TABLE_NAME: !Ref ItemsTable
Events:
Api:
Type: Api
Properties:
Path: /items/{id}
Method: get

Download Complete Guide

Want this AWS SAM guide as a reference? Download the complete HTML version for offline access.



Download Full HTML Guide

Next Steps in Your SAM Journey

Now that you’ve learned the basics of AWS SAM, here’s how to continue your serverless journey:

Testing Serverless Applications

Learn how to thoroughly test your SAM applications locally and in the cloud.

Read Guide →

SAM vs. CloudFormation

Understand when to use SAM and when to use raw CloudFormation.

Compare Approaches →

CI/CD Pipelines with SAM

Set up automated deployment pipelines for your serverless applications.

Learn Integration →

Conclusion

AWS SAM dramatically simplifies serverless development on AWS. By providing a streamlined syntax and powerful CLI, it enables developers to focus on writing code rather than managing infrastructure. As you continue your serverless journey, remember that:

  • AWS SAM templates are extensions of CloudFormation
  • The SAM CLI is your primary development tool
  • Local testing accelerates development cycles
  • Infrastructure-as-code ensures reproducible environments

Ready to build your first serverless application? Check out our step-by-step project tutorial to apply what you’ve learned!