Infrastructure as Code: CloudFormation & CDK
Infrastructure as code (IaC) means describing your cloud resources, such as buckets, databases, functions and networks, in text files instead of clicking them together in the console. AWS's own tools are CloudFormation, which reads templates and builds the resources, and the AWS CDK, which lets you write those definitions in a programming language.
Why bother?
Clicking through the console is fine for learning, but imagine rebuilding a school portal's 40 resources by hand for a test environment, and getting every setting identical. IaC gives you:
- Repeatability: create identical dev, test and production environments from the same files.
- Version control: templates live in Git, so changes are reviewed in pull requests and can be rolled back.
- Documentation: the template is the description of your system.
- Clean deletion: deleting a stack removes everything it created, which is perfect for practice projects and cost control.
CloudFormation concepts
- Template: a YAML or JSON file listing resources and their properties.
- Stack: the set of real resources created from one template. You create, update and delete the stack as a unit.
- Change set: a preview of what an update will add, modify or replace, before it happens.
- Rollback: if creating or updating fails, CloudFormation returns the stack to its last working state.
- Drift detection: finds resources someone changed manually outside the template.
A CloudFormation template
storage.yaml creates a private, versioned bucket and an on-demand DynamoDB table for a notes app:
AWSTemplateFormatVersion: "2010-09-09"
Description: Storage for the Kuza notes app
Parameters:
Env:
Type: String
AllowedValues: [dev, prod]
Default: dev
Resources:
NotesBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
Tags:
- Key: env
Value: !Ref Env
NotesTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: noteId
AttributeType: S
KeySchema:
- AttributeName: noteId
KeyType: HASH
Outputs:
BucketName:
Value: !Ref NotesBucket
TableArn:
Value: !GetAtt NotesTable.Arn
AWSTemplateFormatVersionis always"2010-09-09".Parametersare inputs supplied at deploy time.Resourcesis the only required section. Each resource has a logical name (NotesBucket), aTypeandProperties.!Refreturns a parameter's value or a resource's main identifier (the bucket name);!GetAttreturns another attribute, such as the table's ARN.- No bucket name is given, so CloudFormation generates a unique one, avoiding name clashes.
Deploy it, read the outputs and, when finished, delete it:
aws cloudformation deploy --template-file storage.yaml \
--stack-name kuza-notes-dev --parameter-overrides Env=dev
aws cloudformation describe-stacks --stack-name kuza-notes-dev \
--query "Stacks[0].Outputs" --output table
aws cloudformation delete-stack --stack-name kuza-notes-dev
A bucket can only be deleted when it is empty, so empty it first if you uploaded files. If a template creates IAM roles, add --capabilities CAPABILITY_IAM (or CAPABILITY_NAMED_IAM) to acknowledge that.
AWS SAM
The AWS Serverless Application Model (SAM) is an extension of CloudFormation with short resource types for serverless apps, such as AWS::Serverless::Function, and a CLI (sam build, sam deploy --guided, sam delete). The serverless API project uses it.
The AWS CDK
The Cloud Development Kit lets you define infrastructure with real code (TypeScript, Python, Java, C#, Go), using loops, functions and sensible defaults. The CDK synthesises a CloudFormation template and deploys it, so you get CloudFormation's safety with less typing. The same storage in CDK v2, Python:
from aws_cdk import App, RemovalPolicy, Stack
from aws_cdk import aws_dynamodb as dynamodb
from aws_cdk import aws_s3 as s3
from constructs import Construct
class NotesStorageStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
s3.Bucket(
self, "NotesBucket",
versioned=True,
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
removal_policy=RemovalPolicy.DESTROY, # fine for practice, not production
auto_delete_objects=True,
)
dynamodb.Table(
self, "NotesTable",
partition_key=dynamodb.Attribute(name="noteId", type=dynamodb.AttributeType.STRING),
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST,
removal_policy=RemovalPolicy.DESTROY,
)
app = App()
NotesStorageStack(app, "NotesStorageStack")
app.synth()
The CDK CLI is installed with Node.js and npm, even when you write Python:
npm install -g aws-cdk mkdir notes-cdk && cd notes-cdk cdk init app --language python source .venv/bin/activate # Windows: .venv\Scripts\activate python -m pip install -r requirements.txt cdk bootstrap # once per account and Region cdk synth # print the generated CloudFormation template cdk diff # preview changes cdk deploy cdk destroy # delete everything when finished
Fixing things by hand in the console after deploying with IaC. The template and reality drift apart, and the next deploy may undo your fix or fail. Change the template and redeploy instead. For production data stores, keep the default "retain" behaviour (or DeletionPolicy: Retain in CloudFormation) so deleting a stack cannot delete your database by accident.
Terraform, from HashiCorp, is a popular third-party IaC tool that works across many cloud providers; the ideas on this page carry over.
Key points
- IaC makes infrastructure repeatable, reviewable and easy to delete.
- CloudFormation turns a template into a stack; change sets preview updates and failed changes roll back.
- Templates have Parameters, Resources (required) and Outputs; use
!Refand!GetAttto connect them. - SAM simplifies serverless templates; the CDK generates templates from Python or TypeScript code.
- Change the code, not the console, once a resource is managed by IaC.
Tip: use the ← and → keys to move between pages.
