AWS-SAM

AWS SAM (Serverless Application Model) — What I Learned

Editorial Cover

Introduction

AWS SAM (Serverless Application Model) is an open-source framework from AWS that helps developers build, test, and deploy serverless applications easily.

It simplifies working with AWS serverless services like:

  • AWS Lambda
  • API Gateway
  • DynamoDB
  • Step Functions
  • EventBridge

It provides a way to define infrastructure as code (IaC) and manage deployments through the AWS SAM CLI.

AWS SAM mainly has two important parts:

  1. SAM Transform Templates
  2. AWS SAM CLI

1. SAM Transform Templates

AWS SAM templates are infrastructure-as-code templates used to define serverless applications.

SAM templates are built on top of AWS CloudFormation, which means SAM extends CloudFormation by adding serverless-specific resources and simplifying deployment.

Instead of manually creating:

  • Lambda functions
  • API Gateway endpoints
  • Permissions
  • Event triggers

we can define everything inside one SAM template.


Lambda and Events

A Lambda function cannot work by itself. It needs an event source to trigger it.

Common Lambda triggers:

  • API Gateway requests
  • S3 uploads
  • SNS messages
  • SQS messages
  • EventBridge events

Inside a SAM template, we define:

  • Lambda function
  • Event source that triggers the Lambda function

This makes managing serverless applications easier.


AWS SAM Serverless Resource Types

AWS SAM provides 8 main serverless resource types:

AWS::Serverless::Function

Creates Lambda functions.

AWS::Serverless::API

Creates REST APIs using API Gateway.

AWS::Serverless::HttpApi

Creates HTTP APIs using API Gateway.

AWS::Serverless::SimpleTable

Creates DynamoDB tables.

AWS::Serverless::LayerVersion

Creates Lambda layers.

AWS::Serverless::Application

Allows reuse of serverless applications.

AWS::Serverless::StateMachine

Creates AWS Step Functions workflows.

AWS::Serverless::Connector

Creates permissions and connections between resources.


Lambda Event Sources Supported by SAM

Lambda functions can be triggered by:

  • Amazon S3
  • Amazon SNS
  • Amazon Kinesis
  • Amazon DynamoDB Streams
  • Amazon SQS
  • API Gateway
  • HTTP API
  • Scheduled Events
  • Amazon Cognito
  • Amazon EventBridge

SAM allows these integrations to be defined directly inside the template.


2. AWS SAM CLI

AWS SAM CLI is a command-line tool used to:

  • Build
  • Test
  • Debug
  • Deploy

serverless applications.


AWS SAM Local

SAM Local allows developers to test serverless applications locally before deploying.

It supports:

  • Unit testing
  • Functional testing
  • Debugging

Run APIs Locally

Command:

sam local start-api

or

aws sam start-api

This runs API Gateway and Lambda locally.


Invoke Lambda Locally

Command:

sam local invoke

This invokes Lambda locally by simulating:

  1. A service sending an event
  2. Lambda receiving and processing the event

Generate Mock Events

Command:

sam local generate-event

Generates sample events from AWS services.

Examples:

  • S3 event
  • SQS event
  • API Gateway event

AWS SAM Accelerate

SAM Accelerate improves the development workflow by reducing deployment time.

It includes:


SAM Build

Command:

sam build

Features:

  • Parallel builds
  • Incremental builds
  • Temporary layers

SAM build only rebuilds changed resources instead of rebuilding everything.


SAM Sync

Command:

sam sync

SAM Sync updates changes quickly without a full deployment.

It is mainly used for development.

It watches changed files and updates AWS resources faster.


SAM Logs

Command:

sam logs

Allows developers to view CloudWatch logs from the terminal.

Features:

  • Aggregated logs
  • Log filtering
  • Trace information

SAM Deploy Flow

The deployment process:

flowchart LR

    A[Template YAML File<br/>SAM Template] --> B[S3<br/>Upload Lambda Code<br/>and Artifacts]

    A --> C[ECR<br/>Amazon Elastic Container Registry<br/>Upload Container Images]

    B --> D[Create CloudFormation<br/>Change Set]

    C --> D

    D --> E[Deploy to CloudFormation Stack]

    E --> F[AWS Resources Created<br/>Lambda<br/>API Gateway<br/>DynamoDB<br/>Other Serverless Resources]

SAM Deploy

Command:

sam deploy

Deploys the serverless application to AWS.

Before deploying:

sam build

SAM packages application artifacts and uploads them to:

  • Amazon S3
  • Amazon ECR

depending on the application type.


AWS SAM Pipeline

AWS SAM provides tools to create CI/CD pipelines for serverless applications.

It automatically generates:

  • AWS resources
  • Permissions
  • Pipeline configuration

Pipeline Bootstrap

Command:

sam pipeline bootstrap

Creates AWS environment resources and permissions required for the pipeline.


Pipeline Init

Command:

sam pipeline init

Generates pipeline configuration.

Flow:

sam pipeline bootstrap
          |
          ↓
sam pipeline init

Serverless Application Repository Patterns

AWS provides reusable serverless patterns.

There are more than 186 AWS SAM patterns available.

These patterns help developers quickly create common architectures.


SAM Config File

AWS SAM supports:

samconfig.toml

This file stores default values for SAM commands.

Example:

Instead of repeatedly passing deployment parameters, store them inside the config file.


Best Practice: Let SAM Name Your Resources

Avoid manually hardcoding resource names.

Example:

Avoid:

FunctionName: MyApplicationLambda

Allow SAM and CloudFormation to generate resource names.

Benefits:

  • Avoid naming conflicts
  • Easier deployments
  • Better environment management
  • Works better with multiple stages

Feedbacks

0 feedbacks