SAM CLI: What It Is and When to Use It

Definition

The AWS Serverless Application Model (SAM) Command Line Interface (CLI) is an open-source developer tool that enables you to build, test, debug, and deploy serverless applications on AWS. It acts as a local frontend to the AWS SAM framework, which provides a simplified, shorthand syntax for defining serverless resources, and then transforms these definitions into standard AWS CloudFormation templates for deployment.

How It Works

The SAM CLI streamlines the entire development lifecycle for serverless applications. It uses a YAML or JSON template file (e.g., template.yaml) where you define your application's resources—such as AWS Lambda functions, Amazon API Gateway APIs, and Amazon DynamoDB tables—using the SAM specification.

The typical workflow involves these core commands:

  1. sam init: Initializes a new serverless application by creating a directory structure with a template file and sample application code in your chosen runtime (e.g., Python, Node.js, Java).
  2. sam build: This command processes your SAM template, builds your application code and any dependencies, and prepares it for local testing or deployment. It creates a .aws-sam directory containing the build artifacts.
  3. sam local: This suite of commands allows you to test your application locally without deploying it to the AWS cloud. It uses Docker containers to simulate the Lambda execution environment.
    • sam local invoke: Invokes a single Lambda function once.
    • sam local start-api: Starts a local HTTP server that emulates API Gateway, allowing you to test your API-triggered functions.
    • sam local start-lambda: Emulates the Lambda invocation service locally.
  4. sam deploy: This command packages your application artifacts (code and dependencies), uploads them to an Amazon S3 bucket, and deploys the application to the AWS cloud by creating or updating a CloudFormation stack. The --guided flag provides an interactive experience for the first deployment, saving your settings to a samconfig.toml file for future use.
  5. sam sync: For rapid iteration during development, this command quickly syncs local code and configuration changes to your deployed application in the cloud, often without needing a full CloudFormation deployment.
  6. sam logs: Fetches logs generated by your deployed Lambda functions from Amazon CloudWatch Logs, which is useful for debugging.

Key Features and Limits

  • Local Emulation: Test Lambda functions and API Gateway locally in a Docker-based environment.
  • Interactive Debugging: Step-through debugging of your Lambda function code by connecting your IDE to the local environment.
  • Infrastructure as Code (IaC): Uses the simplified SAM syntax which is an extension of AWS CloudFormation, providing reliable and repeatable deployments.
  • Built-in Best Practices: Comes with starter templates that incorporate best practices for serverless development.
  • CI/CD Integration: Provides commands like sam pipeline init to generate starter CI/CD pipelines for popular systems like AWS CodePipeline, Jenkins, GitLab CI/CD, and GitHub Actions.
  • Terraform and AWS CDK Support: While native to SAM templates, the CLI also supports local testing and debugging for applications defined using Terraform and the AWS Cloud Development Kit (CDK).
  • SAM Accelerate: A feature that speeds up cloud-based testing by syncing changes directly to the AWS environment, bypassing a full deployment for faster feedback cycles.
  • CloudFormation Language Extensions Support: As of May 2026, SAM CLI supports CloudFormation Language Extensions like Fn::ForEach, allowing you to reduce duplication in your IaC templates while still using the full local development workflow.

There are no specific service limits for the SAM CLI itself, as it is a client-side tool. However, the resources it deploys are subject to the standard quotas of the respective AWS services (e.g., AWS Lambda, API Gateway).

Common Use Cases

  • Rapid Prototyping of Serverless APIs: Quickly create and iterate on REST APIs using Lambda and API Gateway, with the ability to test the entire request/response cycle locally.
  • Event-Driven Architectures: Build applications that respond to events from services like Amazon S3, Amazon SNS, Amazon SQS, and Amazon EventBridge. The sam local generate-event command can create mock event payloads for local testing.
  • Automated CI/CD Pipelines for Serverless: Use sam pipeline init to bootstrap a continuous integration and delivery pipeline, enabling automated builds, tests, and deployments for your serverless applications.
  • Local Development for AWS CDK and Terraform: Developers using AWS CDK or Terraform for infrastructure can leverage sam local commands to test and debug their Lambda functions without leaving their local environment.
  • Data Processing Workflows: Develop and test Lambda functions that are part of a larger data processing pipeline, for example, functions triggered by new objects in an S3 bucket to perform transformations.

Pricing Model

The AWS SAM CLI itself is an open-source tool and is free to use. You are only billed for the AWS resources that you provision and consume when you deploy your application using the sam deploy or sam sync commands. This includes charges for services like AWS Lambda invocations, API Gateway requests, data stored in Amazon S3, and Amazon DynamoDB usage. There are no minimum fees or upfront commitments associated with using SAM CLI.

Pros and Cons

Pros:

  • Accelerated Development Cycle: Local testing and debugging features significantly shorten the feedback loop compared to deploying to the cloud for every change.
  • Simplified Syntax: SAM's shorthand syntax for defining serverless resources is more concise and easier to manage than raw CloudFormation.
  • First-Party AWS Support: As an official AWS tool, it has excellent integration with other AWS services and is kept up-to-date with the latest features.
  • Strong Tooling Integration: Integrates well with IDEs (like VS Code through the AWS Toolkit) and CI/CD systems.
  • Open Source: Being open-source allows for community contributions and transparency.

Cons:

  • Local Environment Discrepancies: The local emulation, while powerful, cannot perfectly replicate the cloud environment. IAM permissions, networking configurations, and some service integrations can only be fully tested once deployed.
  • Learning Curve: While simpler than CloudFormation, there is still a learning curve associated with the SAM syntax, CLI commands, and the underlying concepts.
  • Vendor Lock-in: SAM is designed specifically for the AWS ecosystem, which can be a limitation for multi-cloud strategies.
  • Docker Dependency: Local testing and building functionalities require Docker to be installed and running on the developer's machine.

Comparison with Alternatives

  • AWS CDK (Cloud Development Kit): The AWS CDK allows you to define your cloud infrastructure using familiar programming languages like TypeScript, Python, and Java. While SAM CLI uses a declarative YAML/JSON template, CDK uses an imperative approach. The SAM CLI can be used to locally test Lambda functions defined within a CDK application, making them complementary tools.
  • Serverless Framework: A popular third-party, open-source framework that supports multiple cloud providers (AWS, Azure, Google Cloud). It offers a similar feature set to SAM CLI, including local development, deployment, and plugin extensions. The choice often comes down to preference for a multi-cloud tool versus an AWS-native one.
  • Raw AWS CloudFormation / AWS CLI: Using the standard AWS CLI with CloudFormation is always an option. However, SAM CLI provides a higher-level abstraction specifically for serverless applications, simplifying the template syntax and automating packaging and deployment steps that would otherwise be manual.

Exam Relevance

The AWS SAM CLI is a key topic for developer-focused and serverless-related certifications. You can expect to see questions related to it on the following exams:

  • AWS Certified Developer - Associate (DVA-C02): Candidates should understand the core SAM CLI workflow: init, build, local, and deploy. Questions may focus on how to test and debug Lambda functions locally.
  • AWS Certified DevOps Engineer - Professional (DOP-C02): This exam may cover how SAM CLI integrates into CI/CD pipelines for automated serverless deployments.
  • AWS Certified Solutions Architect - Associate (SAA-C03): While less focused on the CLI commands themselves, understanding how SAM simplifies serverless application definition and deployment is beneficial.

Frequently Asked Questions

Q: Do I need to know AWS CloudFormation to use the AWS SAM CLI?

A: While AWS SAM abstracts away much of the complexity of AWS CloudFormation, a basic understanding is very helpful. Since SAM is an extension of CloudFormation, you can use any standard CloudFormation resource in your SAM template, and understanding CloudFormation outputs, parameters, and intrinsic functions will allow you to build more complex applications.

Q: Can I use the SAM CLI to test Lambda functions that interact with other AWS services like DynamoDB or S3?

A: The SAM CLI's local testing primarily focuses on emulating the Lambda runtime and API Gateway. For interactions with other services, you have two main options: connect to actual AWS resources in a development account from your local function (which will incur costs), or use local simulators for those services, such as DynamoDB Local.

Q: What is the difference between sam deploy and sam sync?

A: sam deploy is a comprehensive command that packages your entire application and deploys it using AWS CloudFormation. It's suitable for initial deployments and significant infrastructure changes. sam sync is designed for rapid development iterations; it detects changes to your code or Lambda configuration and attempts to update them directly in the cloud using service APIs, bypassing a full CloudFormation deployment. This makes the update process much faster but should only be used in development environments.


This article reflects AWS features and pricing as of 2026. AWS services evolve rapidly — always verify against the official AWS documentation before making production decisions.

Published: 6/15/2026 / Updated: 6/15/2026

This article is for informational purposes only. AWS services, pricing, and features change frequently — always verify details against the official AWS documentation before making production decisions.

More in DevOps