CloudFormation (IaC)

CloudFormation lets you define your entire AWS infrastructure as a template, so it can be created and destroyed reliably.

AWS CloudFormation is AWS's infrastructure as code service. You describe the resources you want in a template (YAML or JSON), and CloudFormation creates, updates and deletes them as a single unit called a stack.

Why infrastructure as code?

  • Repeatable — spin up identical dev, test and prod environments.
  • Version-controlled — your infrastructure lives in Git alongside your app.
  • Safe — CloudFormation rolls back automatically if a deployment fails.

Template anatomy

  • Parameters — inputs you pass in at deploy time.
  • Resources — the AWS objects to create (the only required section).
  • Outputs — values to export, like a bucket name or URL.

Example

Example · yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: A single S3 bucket managed as code

Parameters:
  BucketName:
    Type: String
    Description: Globally unique bucket name

Resources:
  AssetsBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName
      VersioningConfiguration:
        Status: Enabled

Outputs:
  BucketArn:
    Description: ARN of the created bucket
    Value: !GetAtt AssetsBucket.Arn

When to use it

  • A DevOps team defines an entire three-tier web application stack in a CloudFormation template so any developer can recreate the exact environment in minutes.
  • A company uses CloudFormation StackSets to deploy identical security guardrails (CloudTrail, Config, GuardDuty) across all 20 AWS accounts in their organization simultaneously.
  • After a disaster recovery event, an operations team runs a CloudFormation template to rebuild the entire VPC, subnets, and application infrastructure from scratch.

More examples

Minimal CloudFormation Template

A minimal CloudFormation template that accepts a bucket name parameter and creates a versioned S3 bucket with an output ARN.

Example · yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple S3 bucket

Parameters:
  BucketName:
    Type: String

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName
      VersioningConfiguration:
        Status: Enabled

Outputs:
  BucketArn:
    Value: !GetAtt MyBucket.Arn

Deploy a Stack from CLI

Deploys or updates a CloudFormation stack from a local template file, passing parameter values on the command line.

Example · bash
aws cloudformation deploy \
  --template-file stack.yaml \
  --stack-name my-s3-stack \
  --parameter-overrides BucketName=my-unique-bucket-2024 \
  --capabilities CAPABILITY_IAM

Check Stack Events on Failure

Filters stack events to show only failed resources and their reasons, the fastest way to diagnose a deployment failure.

Example · bash
aws cloudformation describe-stack-events \
  --stack-name my-s3-stack \
  --query 'StackEvents[?ResourceStatus==`CREATE_FAILED`].{Resource:LogicalResourceId,Reason:ResourceStatusReason}' \
  --output table

Discussion

  • Be the first to comment on this lesson.