AWS AWSAWS SAMCloudFormationInfrastructure as CodeServerless

Article & Tutorial

AWS CloudFormation vs AWS SAM: Complete Guide with Examples, Diagrams, Deployment & CI/CD

Learn AWS CloudFormation and AWS SAM from the ground up. Understand the difference between them, when to use each, how SAM builds on CloudFormation, and deploy real infrastructure with practical YAML examples, diagrams, CLI commands, and CI/CD guidance.

AWS CloudFormation vs AWS SAM: Complete Guide with Examples, Diagrams, Deployment & CI/CD technical illustration
AWS CloudFormation vs AWS SAM: Complete Guide with Examples, Diagrams, Deployment & CI/CD technical illustration
Contents

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.

yaml
Developer | v YAML / JSON Template | v Version Control | v CI/CD Pipeline | v AWS Deployment Service | v AWS Resources

This 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:

yaml
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 AppBucket

Deploy the CloudFormation Template

yaml
aws cloudformation validate-template \ --template-body file://template.yaml aws cloudformation deploy \ --template-file template.yaml \ --stack-name demo-infrastructure \ --parameter-overrides Environment=dev

You can inspect the stack after deployment:

yaml
aws cloudformation describe-stacks \ --stack-name demo-infrastructure

What 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:

yaml
Transform: AWS::Serverless-2016-10-31

SAM 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

yaml
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 + Events

This 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:

yaml
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:

yaml
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

yaml
sam validate sam build sam local invoke HelloFunction sam local start-api

Then open:

yaml
http://127.0.0.1:3000/hello

Deploy the SAM Application

yaml
sam deploy --guided

After the first guided deployment, SAM can store deployment settings in samconfig.toml , allowing subsequent deployments to be simpler:

yaml
sam build sam deploy

CloudFormation 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?

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.