AWS CloudFormation and AWS SAM (Serverless Application Model) are both Infrastructure as Code (IaC) technologies used to define and deploy AWS resources. They are closely related, but they solve slightly different problems. CloudFormation is the general-purpose infrastructure provisioning engine, while AWS SAM adds serverless-focused shorthand and developer tooling on top of CloudFormation.
Quick answer: Use CloudFormation when you are primarily provisioning general AWS infrastructure such as VPCs, EC2, RDS/Aurora, load balancers, IAM, S3, and networking. Use AWS SAM when your application is mainly serverless—Lambda, API Gateway, event sources, DynamoDB, Step Functions, and related services. A SAM template can also contain normal CloudFormation resources.
What Is Infrastructure as Code?
Infrastructure as Code means defining infrastructure in files instead of manually clicking through the AWS console. The template becomes a repeatable blueprint that can be version controlled, reviewed, tested, and deployed through CI/CD.
Developer | v YAML / JSON Template | v Version Control | v CI/CD Pipeline | v AWS Deployment Service | v AWS ResourcesThis approach helps teams create consistent development, test, staging, and production environments while reducing manual configuration drift.
What Is AWS CloudFormation?
AWS CloudFormation is AWS's native Infrastructure as Code service. You describe the AWS resources you want in a YAML or JSON template, then CloudFormation provisions and manages those resources as a stack .
A CloudFormation stack can contain resources such as:
- Amazon VPC, subnets, route tables, NAT gateways and security groups
- Amazon EC2 instances and Auto Scaling groups
- Application and Network Load Balancers
- Amazon RDS and Amazon Aurora databases
- Amazon S3 buckets
- AWS IAM roles and policies
- AWS Lambda functions
- Amazon API Gateway
- Amazon ECS and EKS resources
- CloudWatch alarms and logging resources
CloudFormation Core Concepts
Concept Meaning Template YAML or JSON file describing the desired infrastructure. Stack A deployed collection of resources managed as one unit. Parameters Input values supplied at deployment time. Resources The AWS resources CloudFormation creates and manages. Outputs Values returned after deployment, such as an endpoint or resource ID. Mappings Static lookup values used by the template. Conditions Rules that control whether resources or properties are created. Change Set A preview of proposed stack changes before execution. Drift Detection Detects supported resources that have been changed outside CloudFormation.
Simple CloudFormation Example
The following template creates an encrypted S3 bucket with versioning enabled:
AWSTemplateFormatVersion: '2010-09-09' Description: Simple S3 bucket example Parameters: Environment: Type: String Default: dev AllowedValues: - dev - test - prod Resources: AppBucket: Type: AWS::S3::Bucket Properties: BucketEncryption: ServerSideEncryptionConfiguration: - ServerSideEncryptionByDefault: SSEAlgorithm: AES256 VersioningConfiguration: Status: Enabled Tags: - Key: Environment Value: !Ref Environment Outputs: BucketName: Description: Created S3 bucket name Value: !Ref AppBucketDeploy the CloudFormation Template
aws cloudformation validate-template \ --template-body file://template.yaml aws cloudformation deploy \ --template-file template.yaml \ --stack-name demo-infrastructure \ --parameter-overrides Environment=devYou can inspect the stack after deployment:
aws cloudformation describe-stacks \ --stack-name demo-infrastructureWhat Is AWS SAM?
AWS SAM stands for AWS Serverless Application Model . It is an open-source framework for building serverless applications using Infrastructure as Code. SAM templates extend CloudFormation templates with a simplified serverless syntax.
The key line that identifies a SAM template is:
Transform: AWS::Serverless-2016-10-31SAM provides special resource types such as:
- AWS::Serverless::Function
- AWS::Serverless::Api
- AWS::Serverless::HttpApi
- AWS::Serverless::SimpleTable
- AWS::Serverless::StateMachine
- AWS::Serverless::LayerVersion
During deployment, SAM transforms these simplified declarations into the CloudFormation resources needed to provision the application.
How AWS SAM Works
Developer | v template.yaml + application code | v SAM CLI |-- sam build |-- sam local invoke |-- sam local start-api |-- sam validate |-- sam deploy | v SAM Transform | v CloudFormation | v Lambda + API Gateway + IAM + DynamoDB + EventsThis is one of the most important concepts to understand: SAM does not replace CloudFormation. SAM uses CloudFormation as the underlying deployment mechanism.
Simple AWS SAM Example
The following SAM template creates a Lambda function exposed through an HTTP API:
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Simple SAM API example Globals: Function: Timeout: 10 MemorySize: 256 Resources: HelloFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/ Handler: app.handler Runtime: python3.12 Events: HelloApi: Type: HttpApi Properties: Path: /hello Method: GET Outputs: ApiUrl: Description: HTTP API endpoint Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/hello"Example Python function:
import json def handler(event, context): return { "statusCode": 200, "headers": {"content-type": "application/json"}, "body": json.dumps({"message": "Hello from AWS SAM"}) }Build and Test Locally
sam validate sam build sam local invoke HelloFunction sam local start-apiThen open:
http://127.0.0.1:3000/helloDeploy the SAM Application
sam deploy --guidedAfter the first guided deployment, SAM can store deployment settings in samconfig.toml , allowing subsequent deployments to be simpler:
sam build sam deployCloudFormation vs AWS SAM
Feature CloudFormation AWS SAM Main purpose General AWS infrastructure Serverless application development Template format YAML or JSON Mostly YAML/JSON CloudFormation-compatible templates with SAM syntax Underlying deployment CloudFormation CloudFormation Lambda support Yes Yes, with simplified syntax API Gateway support Yes Yes, with serverless-focused shorthand EC2/VPC/RDS/Aurora Excellent fit Can use normal CloudFormation resources inside a SAM template Local Lambda testing Not its primary developer workflow Supported through SAM CLI Local API emulation Not its primary developer workflow sam local start-api Change sets Yes Deployment ultimately uses CloudFormation stacks/change-set workflows Best for Infrastructure platforms and complete AWS environments Lambda/API/event-driven applications
Important Question: Should Aurora PostgreSQL Be Created with SAM or CloudFormation?
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Sep 17, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Sep 17, 2026Related articles
Learn Linux from the ground up with simple explanations of the kernel, distributions, filesystem, commands, pe...
4 min read · Updated Oct 1, 2026
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
7 min read · Sep 19, 2026
Troubleshoot Kubernetes issues including Pending Pods, CrashLoopBackOff, ImagePullBackOff, service routing, in...
7 min read · Sep 17, 2026
Learn Docker environment variables and secrets, including docker run -e, env files, Compose environment, secre...
2 min read · Sep 17, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.