CloudFormation templates and stacks
Declaring AWS resources as templates and stacks: parameters, references and outputs, dependency order, rollback, deletion policies and validation.
The AWS CLI, SDKs and credential profilesIAM users, groups, roles and policiesSQS queues
CloudFormation templates and stacks
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A template declares resources. A stack is one deployed instance of a template, created, updated and deleted as a unit.
- Ref and GetAtt connect resources, and CloudFormation derives the creation order from those references.
- Parameters, conditions and Sub let one template serve several environments. Outputs publish values to people and other stacks.
- A failed create rolls back and leaves a stack in ROLLBACK_COMPLETE, which can only be deleted.
- Deleting a stack deletes its resources unless a DeletionPolicy says otherwise. Some database types snapshot by default.
- Lint templates before deploying. cfn-lint catches misspelled properties and broken references in seconds.
Why describe it instead of clicking it
You built a queue, a function and a role by hand. Now build the same in production, in a second Region, and again after someone deletes one. Nobody remembers every checkbox. The general case for declarative infrastructure applies here, and you may already know Terraform. CloudFormation is AWS’s built-in engine: you send it a description, it makes the API calls, and it keeps the record of what it made. There is no state file for you to store or lock.
Two words matter. A template is the description. A stack is one live instance of it. Think of a building plan and the building: one plan, many buildings.
A template
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
Stage:
Type: String
AllowedValues: [dev, prod]
Conditions:
IsProd: !Equals [!Ref Stage, prod]
Resources:
DeadLetters:
Type: AWS::SQS::Queue
Properties:
MessageRetentionPeriod: 1209600
Orders:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub "orders-${Stage}"
VisibilityTimeout: !If [IsProd, 180, 30]
RedrivePolicy:
deadLetterTargetArn: !GetAtt DeadLetters.Arn
maxReceiveCount: 5
Outputs:
OrdersUrl:
Value: !Ref Orders
Only Resources is required. Each resource has a logical ID you choose (Orders), a type and properties; AWS gives it a physical ID. The functions do the wiring:
!Refreturns a parameter’s value or a resource’s main identifier (for a queue, its URL).!GetAttreturns another attribute, such as an ARN.!Subsubstitutes into a string.!If, withConditions, varies resources or properties by environment.Mappingsact as lookup tables.
Because Orders refers to DeadLetters, CloudFormation creates the dead-letter queue first. Independent resources are created in parallel.
What happened when I ran it
cfn-lint, a free checker, passed the template. Then I broke it in two ways, a misspelled property and a reference to a resource that doesn’t exist:
E3002 Additional properties are not allowed ('VisiblityTimeout' was unexpected.
Did you mean 'VisibilityTimeout'?)
E1010 'DeadLetter' is not one of ['DeadLetters', 'Orders']
Both would otherwise surface minutes into a deployment. Put the linter in CI.
I then created the stack on moto, a local emulator:
DeadLetters .../orders-dev-DeadLetters-IK3SBSWZJ4I4
Orders .../orders-dev
The queue I named got exactly that name. The other got a generated one built from the stack name, logical ID and a random suffix. Prefer generated names: they let the same template deploy twice in one account, and they let CloudFormation create a replacement next to the original. moto reported only stack-level events; real CloudFormation lists every resource’s progress, and that list is where you find the first failure and its reason.
When things fail
A deployment is a transaction of sorts. If a resource fails during an update, CloudFormation rolls the stack back to the previous state. If a create fails, it removes what it made and parks the stack in ROLLBACK_COMPLETE, where the only allowed operation is delete. While debugging you can choose to preserve successfully provisioned resources instead.
Deleting, and keeping
Deleting a stack deletes its resources by default. Data deserves an explicit DeletionPolicy: Retain keeps the resource, Snapshot saves a copy first where the type supports it. Some defaults differ: an AWS::RDS::DBCluster is snapshotted by default, as is a standalone DB instance. A bucket that still holds objects cannot be deleted at all.
Sharing and limits
An output with an Export name can be pulled into another stack with Fn::ImportValue, which also stops you deleting the export while it’s in use. Writing to Parameter Store is a looser coupling. Dynamic references such as {{resolve:secretsmanager:...}} fetch secrets at deploy time.
A template holds up to 500 resources, and can be 51,200 bytes inline or 1 MB from S3. Large systems split into several stacks by lifetime: network, data, application.
By default CloudFormation acts with your permissions. A service role makes it act with its own, so a deployer needs to pass that role instead of holding every permission.
About the video
The embed is Stephane Maarek’s “AWS CloudFormation Template Tutorial”, June 2019, 15 minutes. Its chapters create a first stack from an example template, watch it build, update it, verify the changed resources and delete it. The lifecycle is unchanged, though the console looks different now and the video predates newer functions. I judged it from chapters and description only, since transcripts couldn’t be fetched. English was judged from the “English (US) original” audio track. A conceptual overview is in deeper.
What was run: cfn-lint 1.41.0 output is real. The stack ran on moto 5.1.22, an emulator, so it shows API shape and naming, not AWS provisioning. Rollback states, deletion policy and quotas are from the AWS docs, checked 2026-11-29.
Where this fits
Creating is the safe part. Updating something live is where outages come from: changing stacks safely.
Check yourself
You never wrote DependsOn, yet CloudFormation created the dead-letter queue before the main queue. How did it know?
The main queue's redrive policy uses GetAtt on the dead-letter queue. A reference implies a dependency. DependsOn is only for ordering that no reference expresses.
Your first deployment of a new stack failed and the next deploy is refused. What state is it in and what do you do?
ROLLBACK_COMPLETE. The resources were cleaned up, and the only operation allowed is delete. Delete the stack, fix the cause found in the events, and create it again.
You set a fixed QueueName. Later a change requires replacing the queue. Why might that update fail?
CloudFormation creates the replacement before deleting the old resource, and two resources cannot share the name. Let CloudFormation generate names unless something outside needs a fixed one.
Where should a database password come from in a template?
Not from the template text or a plain parameter default. Use a dynamic reference to Secrets Manager or Parameter Store, or let the service manage the secret, so the value is resolved at deployment and not stored in your code.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.