CloudFormation Templates

CloudFormation is AWS's native IaC — declare resources in YAML and deploy them as a stack.

CloudFormation is AWS's built-in IaC service. You write a template describing resources, and CloudFormation creates, updates, or deletes them as a single unit called a stack.

Template sections

  • Parameters — inputs you pass in at deploy time (e.g. environment name).
  • Resources — the AWS things to create (required).
  • Outputs — values to export, like a load balancer URL.

Safe changes

Before applying, generate a change set to preview exactly what will be added, modified, or deleted. CloudFormation also rolls back automatically if a create or update fails partway.

Example

Example · yaml
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  EnvName:
    Type: String
    Default: prod
Resources:
  AppBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub 'myapp-${EnvName}-web-2026'
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        RestrictPublicBuckets: true
Outputs:
  BucketName:
    Value: !Ref AppBucket

When to use it

  • A team defines an S3 bucket, CloudFront distribution, and Route 53 record in one CloudFormation template and deploys them together as a stack.
  • An ops engineer uses CloudFormation change sets to preview what will change before updating a production stack.
  • A company uses CloudFormation StackSets to deploy the same security baseline stack to all AWS accounts in the organisation.

More examples

Minimal CloudFormation template

Declares an S3 static website bucket using CloudFormation YAML and exports the bucket name as a stack output.

Example · yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: S3 bucket for static hosting

Resources:
  SiteBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub '${AWS::StackName}-site'
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: 404.html

Outputs:
  BucketName:
    Value: !Ref SiteBucket

Deploy stack with AWS CLI

Deploys or updates the CloudFormation stack from a local template file, passing a parameter override for the environment.

Example · bash
aws cloudformation deploy \
  --template-file stack.yaml \
  --stack-name my-site \
  --capabilities CAPABILITY_IAM \
  --parameter-overrides Env=prod

Create and review a change set

Creates a change set for an existing stack and describes what resources will be added, modified, or removed.

Example · bash
aws cloudformation create-change-set \
  --stack-name my-site \
  --template-body file://stack.yaml \
  --change-set-name preview-$(date +%s)

aws cloudformation describe-change-set \
  --stack-name my-site \
  --change-set-name preview-$(date +%s)

Discussion

  • Be the first to comment on this lesson.
CloudFormation Templates — AWS Deployment | SoundsCode