Infrastructure as Code with AWS CloudFormation
Infrastructure as Code (IaC) describes your cloud resources in files that are versioned, reviewed and deployed like application code. AWS CloudFormation reads a template and creates, updates or deletes all its resources together as a stack.
Why IaC
| Clicking in the console | Infrastructure as code |
|---|---|
| Hard to repeat exactly | Same template → identical dev, test, prod |
| No history | Git history, pull-request reviews |
| Manual clean-up | Delete the stack → everything removed |
| Drift between environments | Drift detection shows manual changes |
Anatomy of a template
AWSTemplateFormatVersion: '2010-09-09'
Description: Website bucket and a DynamoDB table
Parameters:
Env:
Type: String
AllowedValues: [dev, prod]
Default: dev
Resources:
SiteBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'notes-site-${Env}-${AWS::AccountId}'
VersioningConfiguration: { Status: Enabled }
NotesTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
AttributeDefinitions: [{ AttributeName: id, AttributeType: S }]
KeySchema: [{ AttributeName: id, KeyType: HASH }]
Outputs:
BucketArn:
Value: !GetAtt SiteBucket.Arn
TableName:
Value: !Ref NotesTable
| Section | Purpose |
|---|---|
| Parameters | Inputs at deploy time (environment, instance size) |
| Mappings / Conditions | Look-ups and conditional resources |
| Resources (required) | The AWS resources – logical ID, Type, Properties |
| Outputs | Values to show or export to other stacks |
Intrinsic functions
| Function | Returns |
|---|---|
!Ref X |
Resource’s main ID (bucket name, table name) or a parameter value |
!GetAtt X.Arn |
An attribute of a resource |
!Sub '...${Var}...' |
String with substitutions |
!Join, !Select, !If, !FindInMap, !ImportValue |
Build values, conditions, cross-stack references |
Stack lifecycle
flowchart LR
T[Template] -->|create-stack| C["CREATE_IN_PROGRESS → CREATE_COMPLETE"]
C -->|edit template| CS["Change set – preview Add / Modify / Replace"]
CS -->|execute| U[UPDATE_COMPLETE]
C -->|error| R["ROLLBACK_COMPLETE (all changes undone)"]
U -->|delete-stack| D[DELETE_COMPLETE]
- CloudFormation works out the dependency order (a subnet before an instance).
- On failure it rolls back automatically.
- Change sets preview updates – watch for Replacement: True (the resource will be recreated, e.g. renamed bucket or table).
- Drift detection reports resources changed outside CloudFormation.
- DeletionPolicy: Retain/Snapshot protects data when a stack is deleted.
Scaling up
- Nested stacks and cross-stack exports for large systems.
- StackSets deploy to many accounts and Regions.
- AWS CDK – write infrastructure in TypeScript/Python, synthesised to CloudFormation.
- AWS SAM – shorthand for serverless apps (
AWS::Serverless::Function).
Treat templates like code: keep them in git, deploy through a pipeline, preview with change sets, and never make manual changes to stack resources.
Practise it
Virtual lab · Amazon Web Services · 40 min
Infrastructure as code with AWS CloudFormation: stacks, change sets, rollback and delete
Deploy a data layer (S3, DynamoDB, SQS) from a YAML template with parameters, intrinsic functions and outputs, preview an update with a change set, watch a failed stack roll back automatically, and delete a stack while retaining critical data.
Quick revision
- A template declares resources; a stack is a deployed template.
- Parameters in, outputs out;
!Ref,!GetAtt,!Sub. - Automatic dependency ordering and rollback.
- Change sets preview updates; drift detection finds manual edits.
- CDK and SAM generate CloudFormation.
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.