AWS CloudFormation for Developers: Templates, Parameters, Intrinsic Functions, Change Sets, Stacks and Drift

AWS Developer Associate 55 min min read Updated: Nov 17, 2026 Intermediate
AWS CloudFormation for Developers: Templates, Parameters, Intrinsic Functions, Change Sets, Stacks and Drift
Intermediate Topic 1 of 4

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

SectionRequired?Purpose
AWSTemplateFormatVersionNoVersion, 2010-09-09
DescriptionNoText about the template
ParametersNoInputs at deploy time
MappingsNoStatic lookup tables (for example AMI by Region)
ConditionsNoCreate resources only in some cases (IsProd)
TransformNoMacros such as AWS::Serverless-2016-10-31 (SAM)
ResourcesYesThe AWS resources
OutputsNoValues 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

FunctionUseExample
!RefValue of a parameter, or the ID/name of a resource!Ref OrdersTable
!GetAttAn attribute of a resource (ARN, DNS name)!GetAtt OrdersTable.Arn
!SubString substitution!Sub 'arn:aws:s3:::${Bucket}/*'
!JoinJoin values!Join ['-', [app, !Ref Env]]
!Select, !SplitPick an item, split a string!Select [0, !GetAZs '']
!FindInMapLook up a mapping!FindInMap [EnvSettings, !Ref Env, Memory]
!If, !Equals, !And, !Or, !NotConditions!If [IsProd, 3, 1]
!ImportValueUse another stack's export!ImportValue network-VpcId
Fn::TransformRun a macro
Pseudo parametersAWS::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

SituationResult
Stack creation failsDefault: 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 failsAutomatically rolls back to the previous working state (UPDATE_ROLLBACK_COMPLETE)
Rollback failsUPDATE_ROLLBACK_FAILED: fix the cause (for example a manually deleted resource) and continue update rollback
Delete failsDELETE_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

ValueEffect when the resource is deleted or replaced
DeleteDefault. Remove it
RetainKeep the resource, remove it only from the stack
SnapshotTake a snapshot first (EBS, RDS, ElastiCache, Redshift, Neptune, DocDB)

Organizing Large Systems

TechniqueMeaning
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
StackSetsDeploy one template to many accounts and Regions (works with Organizations)
Modules and registryReusable resource packages
Macros / TransformsSAM 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

  1. Deploy the sample template and read the stack events
  2. Change Env to prod using a change set and note what is modified or replaced
  3. Break the template on purpose and watch the rollback
  4. Export a table name from one stack and import it in another
  5. 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators