Loading...


Updated 5 Oct 2026 • 7 mins read

templates are CloudFormation templates with serverless shorthand and a CLI for local testing. This guide compares them on scope, syntax, tooling, and cost in US dollars, with a comparison table, a worked example, and a decision framework.
Most “SAM vs CloudFormation” comparisons start from a false premise, that you are choosing between two rival tools. You are not. AWS SAM is built on CloudFormation, deploys through CloudFormation, and produces a CloudFormation stack every time you run it. The real question is narrower and more useful: for the serverless parts of your infrastructure, is SAM's extra layer worth adopting, and what does the answer mean for how much you build, how fast you ship, and what you pay?
This guide answers that properly. It explains exactly how AWS SAM relates to AWS CloudFormation, compares them on scope, syntax, tooling, and cost in US dollars, works through a realistic serverless example, and ends with a decision framework. It also covers the part most comparisons skip: how each one shapes the AWS bill of the resources it deploys.
The short answer: AWS SAM is an extension of CloudFormation, not an alternative to it. CloudFormation is AWS's general-purpose infrastructure-as-code service that can define almost any AWS resource. A SAM template is a CloudFormation template that adds a Transform declaration and compact serverless resource types (such as AWS::Serverless::Function) which CloudFormation expands into standard resources at deploy time, plus the SAM CLI for building, testing locally, and deploying. Both are free; you pay only for the resources they create, with CloudFormation charging a small fee only for third-party registry resources. Use SAM for Lambda, API Gateway, DynamoDB, and Step Functions applications; use plain CloudFormation for everything else, and mix the two in one template when a stack has both.
CloudFormation is the engine. You describe resources, VPCs, IAM roles, EC2 instances, Lambda functions, databases, in a declarative YAML or JSON template, and CloudFormation creates, updates, and deletes them as a unit called a stack, tracking state and handling dependencies and rollbacks. It can define nearly every AWS resource type and is the substrate that other AWS tooling, including the CDK, compiles down to.
SAM is a layer on that engine, delivered as a CloudFormation transform, which is a macro that CloudFormation runs on your template before deploying it. Adding the line Transform: AWS::Serverless-2016-10-31 to a CloudFormation template turns it into a SAM template and unlocks a handful of serverless resource types: AWS::Serverless::Function, Api, HttpApi, SimpleTable, StateMachine, and, as of 2026, CapacityProvider. When you deploy, CloudFormation expands each of these into the standard resources it represents. A five-line Serverless::Function becomes a Lambda function, an execution role with appropriate policies, event source mappings, often an API Gateway route, and typically a log group. The SAM developer guide documents the expansion; the practical upshot is that SAM is specialized CloudFormation that knows a great deal about Lambda.
The second half of SAM is the SAM CLI, a separate open-source tool that builds function code and dependencies, invokes functions and emulates API Gateway locally, packages artifacts to S3, deploys through CloudFormation, and, with sam sync, pushes code changes to the cloud in seconds without a full stack update. None of this exists in CloudFormation itself, and for many teams the CLI rather than the template shorthand is the reason to adopt SAM.
| Dimension | AWS CloudFormation | AWS SAM |
|---|---|---|
| What it is | General-purpose IaC service for AWS | Serverless extension of CloudFormation plus a CLI |
| Scope | Nearly every AWS resource type | Serverless resources, plus any CloudFormation resource in the same template |
| Template syntax | Verbose, explicit, one resource per logical unit | Compact shorthand that expands to CloudFormation |
| Deployment engine | CloudFormation | CloudFormation (via transform) |
| Local testing | None built in | sam local invoke and sam local start-api |
| Fast iteration | Change sets and full stack updates | sam sync pushes code in seconds |
| Nested composition | Nested stacks | Nested applications via Serverless Application Repository |
| Cost to use | Free for AWS resources; ~$0.0009 per third-party registry operation beyond 1,000 free per month | Free; same CloudFormation rules apply underneath |
| Learning curve | Steeper, more to write | Gentler for serverless; you still need CloudFormation knowledge to debug |
| Best for | Networking, compute, databases, and mixed infrastructure | Lambda, API Gateway, DynamoDB, and Step Functions applications |
Neither tool has a price tag of its own for ordinary use. CloudFormation is free for managing AWS resource types, and its only charge, listed on the official CloudFormation pricing page, is for third-party and private registry resource types: roughly $0.0009 per handler operation after 1,000 free operations a month, which is negligible for almost everyone. SAM inherits those rules because it deploys through CloudFormation, and the SAM CLI is free and open source. The cost of either is the cost of what they deploy.
For a serverless stack, that means Lambda invocations and duration, API Gateway requests, DynamoDB reads and writes, and, easily forgotten, CloudWatch Logs ingestion and storage. The Lambda pricing page lists the compute side; the pattern for controlling it is the same as for any AWS service and is covered in our AWS cost optimization guide. Where SAM and CloudFormation differ on cost is not in their own fees but in how each shapes the deployed footprint, which the next section covers.
SAM's convenience is also its cost risk. When one line expands into five resources, you inherit the defaults of resources you did not explicitly write. Three matter for the bill.
Raw CloudFormation has the same defaults but forces you to write each resource, so you are more likely to notice them. That is not an argument against SAM; it is an argument for treating the template as the place cost is decided, which is the principle behind shifting cost left into code review that we describe in our guide to infrastructure as code. A SAM template with explicit retention, memory, and throttling is both shorter and cheaper to run than the equivalent raw CloudFormation with defaults left alone.
Consider a small HTTP API: three Lambda functions behind an HTTP API, writing to a DynamoDB table, with IAM roles and log groups. In raw CloudFormation, that is typically 250 to 350 lines: each function, its role and policy, each route and integration, the table, and each log group written out. The same stack in SAM is often 60 to 90 lines, because each AWS::Serverless::Function declares its handler, runtime, and an Events block, and SAM generates the role, permissions, route, and integration. Both deploy to identical resources and identical monthly cost, roughly a few dollars at low traffic, since Lambda and HTTP API bill per use and the DynamoDB table can sit in on-demand mode.
The difference is developer time and iteration speed. The SAM version can be invoked locally, tested against an emulated API, and pushed to the cloud in seconds with sam sync during development, where the CloudFormation version requires a full stack update each time. For a team shipping serverless features daily, that difference in cycle time is the real cost saving, and it dwarfs the negligible fees of either tool.
SAM's shorthand only helps where serverless resources dominate. For a stack that is mostly a VPC, subnets, security groups, EC2 instances, RDS, and load balancers, SAM adds a transform and a CLI you will not use, and plain CloudFormation is cleaner. It is also the better choice when a team standardizes on one tool for everything and wants no layer to debug through, since a SAM deployment error surfaces as a CloudFormation error on an expanded resource you did not write. And organizations that have chosen the AWS CDK or Terraform for their infrastructure will usually find SAM's CLI useful for local Lambda testing but SAM's template layer redundant.
multi-cloud deployment tools. SAM's CLI works alongside either for local Lambda testing.
SAM versus CloudFormation is not a contest, because SAM is CloudFormation with a serverless dialect and a better developer workflow attached. Neither costs anything to use; both cost whatever they deploy. Reach for SAM when Lambda is at the center of what you are building, because its shorthand and its CLI genuinely shorten templates and development cycles. Reach for plain CloudFormation when it is not, and combine them whenever a stack has both. Then treat the template, in either dialect, as the place where cost is set: explicit log retention, right-sized memory, and sensible throttling written into the code cost nothing to add and are the difference between a serverless bill that stays small and one that quietly grows. Watching that bill by function and by team afterward is the job of Opslyft's cost visibility, and the practices around it are in our guide to DevOps practices.
CloudFormation is AWS's general-purpose infrastructure-as-code service, able to define almost any AWS resource in a declarative template. AWS SAM is an extension of CloudFormation for serverless applications: a SAM template is a CloudFormation template with a Transform declaration that lets you use compact serverless resource types, plus the SAM CLI for local testing and deployment. At deploy time, CloudFormation expands SAM's shorthand into standard resources.
No. SAM runs on CloudFormation; every SAM template becomes a CloudFormation stack, and SAM templates can mix serverless shorthand with ordinary CloudFormation resources. SAM adds a developer-friendly layer for Lambda, API Gateway, DynamoDB, and Step Functions, while CloudFormation remains the engine underneath and the right tool for everything that is not serverless.
Both are free to use for AWS resources. You pay only for the resources they deploy, such as Lambda invocations, API Gateway requests, and DynamoDB capacity. CloudFormation charges a small fee, about $0.0009 per handler operation beyond 1,000 free per month, only when it manages third-party or private registry resource types. The SAM CLI is free and open source.
Yes, and most real projects do. A SAM template can contain both AWS::Serverless resource types and standard CloudFormation resources such as SNS topics, CloudWatch alarms, and IAM policies, and the two can reference each other freely. Teams commonly use SAM for the Lambda and API layer and plain CloudFormation for supporting infrastructure in the same stack.
Use SAM when your application is built on Lambda, API Gateway, DynamoDB, or Step Functions and you want shorter templates, local invocation and API emulation, and fast iterative deployment with sam sync. Use plain CloudFormation when your stack is mostly non-serverless infrastructure such as VPCs, EC2, RDS, and networking, where SAM's shorthand adds nothing.
SAM does not add cost itself, but its convenience can create cost through defaults. A single AWS::Serverless::Function expands into a Lambda function, an IAM role, and often an API Gateway integration and log group, and CloudWatch Logs retention defaults to never expire unless you set it. Explicitly setting log retention, memory sizes, and API throttling in the template keeps the deployed footprint, and the bill, under control.
Yes. AWS continues to maintain the SAM transform and the SAM CLI, with documentation updated through 2026 and new resource types such as AWS::Serverless::CapacityProvider added. SAM also integrates with the AWS CDK and Terraform, so its local-testing tooling can be used alongside those frameworks.