AWS CloudFormation for Developers: Templates, Parameters, Intrinsic Functions, Change Sets, Stacks and Drift
What and Why
CloudFormation creates and manages AWS resources from a template (JSON or YAML). The set of resources created from one template is a stack. Benefits: repeatable, reviewable (in Git), consistent across environments, and easy to delete cleanly.
Template Anatomy
| Section | Required? | Purpose |
AWSTemplateFormatVersion | No | Version, 2010-09-09 |
Description | No | Text about the template |
Parameters | No | Inputs at deploy time |
Mappings | No | Static lookup tables (for example AMI by Region) |
Conditions | No | Create resources only in some cases (IsProd) |
Transform | No | Macros such as AWS::Serverless-2016-10-31 (SAM) |
Resources | Yes | The AWS resources |
Outputs | No | Values to show or export to other stacks |
yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Orders API storage and function
Parameters:
Env:
Type: String
AllowedValues: [dev, prod]
Default: dev
TableBillingMode:
Type: String
Default: PAY_PER_REQUEST
LatestAmiId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64
Mappings:
EnvSettings:
dev: {Memory: 256, LogDays: 7}
prod: {Memory: 1024, LogDays: 90}
Conditions:
IsProd: !Equals [!Ref Env, prod]
Resources:
OrdersTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain # keep data if the stack is deleted
UpdateReplacePolicy: Snapshot
Properties:
TableName: !Sub orders-${Env}
BillingMode: !Ref TableBillingMode
AttributeDefinitions: [{AttributeName: orderId, AttributeType: S}]
KeySchema: [{AttributeName: orderId, KeyType: HASH}]
PointInTimeRecoverySpecification: {PointInTimeRecoveryEnabled: !If [IsProd, true, false]}
FunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement: [{Effect: Allow, Principal: {Service: lambda.amazonaws.com}, Action: 'sts:AssumeRole'}]
ManagedPolicyArns: [arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole]
Policies:
- PolicyName: table
PolicyDocument:
Version: '2012-10-17'
Statement: [{Effect: Allow, Action: [dynamodb:GetItem, dynamodb:PutItem], Resource: !GetAtt OrdersTable.Arn}]
OrdersFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub orders-${Env}
Runtime: python3.12
Handler: index.handler
MemorySize: !FindInMap [EnvSettings, !Ref Env, Memory]
Role: !GetAtt FunctionRole.Arn
Environment: {Variables: {TABLE: !Ref OrdersTable}}
Code:
ZipFile: |
def handler(event, context):
return "ok"
Outputs:
TableName:
Value: !Ref OrdersTable
Export: {Name: !Sub '${AWS::StackName}-TableName'}
Intrinsic Functions
| Function | Use | Example |
!Ref | Value of a parameter, or the ID/name of a resource | !Ref OrdersTable |
!GetAtt | An attribute of a resource (ARN, DNS name) | !GetAtt OrdersTable.Arn |
!Sub | String substitution | !Sub 'arn:aws:s3:::${Bucket}/*' |
!Join | Join values | !Join ['-', [app, !Ref Env]] |
!Select, !Split | Pick an item, split a string | !Select [0, !GetAZs ''] |
!FindInMap | Look up a mapping | !FindInMap [EnvSettings, !Ref Env, Memory] |
!If, !Equals, !And, !Or, !Not | Conditions | !If [IsProd, 3, 1] |
!ImportValue | Use another stack's export | !ImportValue network-VpcId |
Fn::Transform | Run a macro | |
| Pseudo parameters | AWS::Region, AWS::AccountId, AWS::StackName, AWS::NoValue | !Sub '${AWS::AccountId}' |
Deploy and Update
bash
aws cloudformation validate-template --template-body file://template.yaml
aws cloudformation deploy --template-file template.yaml --stack-name orders-dev \
--parameter-overrides Env=dev --capabilities CAPABILITY_IAM --no-execute-changeset # creates a change set only
aws cloudformation describe-change-set --stack-name orders-dev --change-set-name <name>
aws cloudformation deploy --template-file template.yaml --stack-name orders-dev --parameter-overrides Env=dev --capabilities CAPABILITY_IAM
aws cloudformation describe-stack-events --stack-name orders-dev
aws cloudformation delete-stack --stack-name orders-dev
CAPABILITY_IAM / CAPABILITY_NAMED_IAM is a required acknowledgement when a template creates IAM resources. CAPABILITY_AUTO_EXPAND is required for macros and SAM transforms
- Change sets preview what will be added, modified, replaced or deleted before you apply them. Look for
Replacement: True, which deletes and recreates a resource (data loss risk)
- Updates behave as No interruption, Some interruption or Replacement depending on the property (check the docs for each property)
Rollback and Failure Behavior
| Situation | Result |
| Stack creation fails | Default: rolls back (deletes what it created) and ends in ROLLBACK_COMPLETE (you must delete the stack before reusing the name). Use --disable-rollback or OnFailure=DO_NOTHING to keep resources for debugging |
| Stack update fails | Automatically rolls back to the previous working state (UPDATE_ROLLBACK_COMPLETE) |
| Rollback fails | UPDATE_ROLLBACK_FAILED: fix the cause (for example a manually deleted resource) and continue update rollback |
| Delete fails | DELETE_FAILED, for example a non-empty S3 bucket. Empty it and retry, or retain the resource |
- Rollback triggers can use CloudWatch alarms to roll back an update automatically
- Stack policies protect resources from updates (for example block replacing the database)
- Termination protection blocks stack deletion
DeletionPolicy and UpdateReplacePolicy
| Value | Effect when the resource is deleted or replaced |
Delete | Default. Remove it |
Retain | Keep the resource, remove it only from the stack |
Snapshot | Take a snapshot first (EBS, RDS, ElastiCache, Redshift, Neptune, DocDB) |
Organizing Large Systems
| Technique | Meaning |
Nested stacks (AWS::CloudFormation::Stack) | A parent stack includes child stacks (reusable parts). Update through the parent |
| Cross-stack references (Export / ImportValue) | Loosely share values between independent stacks. An exported value cannot be changed or deleted while another stack imports it |
| StackSets | Deploy one template to many accounts and Regions (works with Organizations) |
| Modules and registry | Reusable resource packages |
| Macros / Transforms | SAM and others expand shorthand into full templates |
Drift Detection
People sometimes change resources by hand. Drift detection compares the actual configuration with the template and lists differences. Fix the drift by updating the template or reverting the change.
bash
aws cloudformation detect-stack-drift --stack-name orders-dev
aws cloudformation describe-stack-resource-drifts --stack-name orders-dev --stack-resource-drift-status-filters MODIFIED DELETED
Custom Resources and Helpers
- Custom resources run your Lambda code during create, update and delete (for example call an external API). Your function must send a response (
cfn-response or SDK) or the stack waits about an hour and fails
- **
cfn-init / cfn-signal / CreationPolicy and WaitCondition** configure EC2 instances and make the stack wait for success
- **
DependsOn** forces order when CloudFormation cannot infer it. Most dependencies come from Ref and GetAtt
- cfn-lint checks templates before deployment. cfn-guard enforces rules as code
- IaC generator can create templates from existing resources, and resource import adopts existing resources into a stack
Best Practices
- Keep templates in Git and deploy through CI/CD
- Use parameters, mappings and conditions to reuse one template across environments
- Do not hard-code names that must be unique. Let CloudFormation generate them
- Never put secrets in templates or parameters in plain text. Use dynamic references
{{resolve:secretsmanager:name:SecretString:password}} or NoEcho
- Split stacks by lifecycle (network, data, app) so risky changes are small
- Always review change sets for production and set DeletionPolicy: Retain on data stores
Practice Tasks
- Deploy the sample template and read the stack events
- Change
Env to prod using a change set and note what is modified or replaced
- Break the template on purpose and watch the rollback
- Export a table name from one stack and import it in another
- Change a resource by hand and run drift detection
Conclusion
CloudFormation turns infrastructure into reviewable templates. Know the template sections, Ref/GetAtt/Sub and conditions, preview with change sets, understand rollback and DeletionPolicy, share values with exports or nested stacks, use StackSets for many accounts, detect drift, and keep secrets out of templates.
Quick Summary: what and why; anatomy + example; functions; deploy/change sets; rollback; DeletionPolicy; organization; drift; custom resources; best practices.