AWS CloudFormation is the native Infrastructure-as-Code (IaC) service for AWS. It provisions and manages AWS resources via declarative JSON or YAML templates, treats a deployed set of resources as a single unit (a stack), and tracks every change with rollback on failure. CloudFormation is the substrate for higher-level tools like AWS CDK and SAM, which compile down to CloudFormation templates.
AWS::CloudFormation::Stack) and exported outputs imported across stacks.aws cloudformation validate-template for syntax; cfn-lint for richer rules.
AWSTemplateFormatVersion: '2010-09-09'
Description: Single EC2 instance behind an SSH/HTTP security group.
Parameters:
KeyName:
Type: AWS::EC2::KeyPair::KeyName
AllowedSshCidr:
Type: String
Default: 203.0.113.0/24
InstanceType:
Type: String
Default: t3.micro
AllowedValues: [t3.micro, t3.small, t3.medium]
Resources:
AppSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SSH from office, HTTP from anywhere
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref AllowedSshCidr
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
AppInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
ImageId: '{{resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64}}'
KeyName: !Ref KeyName
SecurityGroups:
- !Ref AppSecurityGroup
Tags:
- {Key: Name, Value: !Sub '${AWS::StackName}-app'}
Outputs:
PublicDns:
Description: Public DNS of the instance
Value: !GetAtt AppInstance.PublicDnsName
Export:
Name: !Sub '${AWS::StackName}-PublicDns'
aws cloudformation deploy \
--stack-name app-prod \
--template-file app.yaml \
--parameter-overrides KeyName=my-keypair InstanceType=t3.small \
--capabilities CAPABILITY_NAMED_IAM \
--tags Project=demo Owner=platform
aws cloudformation describe-stacks --stack-name app-prod \
--query 'Stacks[0].Outputs'
# Preview changes safely
aws cloudformation deploy --no-execute-changeset ...
aws cloudformation describe-change-set --change-set-name
UPDATE_ROLLBACK_FAILED — resources keep running while you debug. Use change sets and --disable-rollback selectively to avoid orphaned partial deployments.cfn-init, cfn-signal, cfn-hup coordinate EC2 bootstrapping.CloudFormation: deepest AWS service coverage on day one of a new feature, no state file to manage, integrated with StackSets and Service Catalog. Terraform: multi-cloud, larger module ecosystem, explicit plan/apply UX, broader community. Many AWS-only teams use CDK (compiles to CloudFormation) to get a real programming language without losing native integration.
A preview of what CloudFormation would do if you applied a template change — additions, modifications, and (importantly) replacements. Reviewing change sets before execution catches accidental destroy-and-recreate of stateful resources like RDS instances.
Three options: nested stacks (AWS::CloudFormation::Stack), cross-stack references via Export/!ImportValue, or SSM Parameter Store as a loose-coupling registry. CDK encourages constructs as the abstraction over nested stacks.
Compares deployed resource state with template state and flags differences. Coverage isn't 100% — some properties (e.g., Lambda code SHA) aren't checked. Use it as one signal in compliance, not the sole one; AWS Config and SCPs prevent drift more reliably.
Never hardcode. Use NoEcho parameters for inputs, dynamic references like {{resolve:secretsmanager:...}} or {{resolve:ssm-secure:...}} to fetch from Secrets Manager / SSM at deploy time, and prefer IAM roles for runtime credentials over passing keys through templates.
A template plus an orchestration layer that deploys the same stack to many accounts and regions. The canonical use is rolling out org-wide baseline controls (CloudTrail, GuardDuty, IAM roles for cross-account access) from the management account.