Skip to Content

AWS CloudFormation

AWS CloudFormation as infrastructure as code, its benefits, the visual Infrastructure Composer editor, the CloudFormation service role and iam:PassRole, and StackSets for many accounts and Regions.

What it is

AWS CloudFormation is a declarative way of outlining AWS infrastructure, for almost any resource.

For example, a CloudFormation template states:

  • I want a security group;
  • I want two EC2 instances using this security group;
  • I want an S3 bucket;
  • I want a load balancer (ELB) in front of these machines.

Then CloudFormation creates them, in the right order, with the exact configuration specified. The resources created from one template form a stack; delete the stack and its resources are deleted too.

Resources:
  NotesBucket:
    Type: AWS::S3::Bucket
    Properties:
      VersioningConfiguration:
        Status: Enabled

Benefits

  • Infrastructure as code:
    • no resources are created manually, which is excellent for control;
    • changes to the infrastructure are reviewed through code.
  • Cost:
    • each resource in the stack is tagged with an identifier, so the cost of a stack is visible;
    • the costs of the resources can be estimated from the template;
    • savings strategy: in dev, delete stacks automatically at 5 PM and re-create them at 8 AM, safely.
  • Productivity:
    • destroy and re-create an infrastructure on the cloud on the fly;
    • automated diagrams of the templates;
    • declarative programming: no need to work out ordering and orchestration.
  • Don't re-invent the wheel: use existing templates from the web and the documentation.
  • Supports (almost) all AWS resources; "custom resources" cover the rest.

Infrastructure Composer

Infrastructure Composer shows a CloudFormation stack visually: all the resources and the relations between them (for example a WordPress stack's load balancer, launch configuration and database). A template can also be designed by dragging components.

Updated since the slides

The standalone Infrastructure Composer console closes on 7 December 2026. The same visual editor stays on the CloudFormation console pages and in the AWS Toolkit for VS Code.

CloudFormation service role

CloudFormation service role: a user with only cloudformation permissions and iam:PassRole submits a template; CloudFormation uses a service role with s3 permissions to create the stack's S3 bucket, so the user never needs those permissions directly CloudFormation service role: a user with only cloudformation permissions and iam:PassRole submits a template; CloudFormation uses a service role with s3 permissions to create the stack's S3 bucket, so the user never needs those permissions directly

CloudFormation acts with its own role, not the user's.
  • A service role is an IAM role that allows CloudFormation to create, update and delete stack resources on the user's behalf.
  • It lets users create, update and delete stack resources even if they don't have permissions to work with those resources directly.
  • Use cases:
    • the goal is the least privilege principle;
    • but the user should not get all the permissions needed to create the stack resources.
  • The user must have iam:PassRole permission (to hand the role to CloudFormation).

StackSets

Not in the course slides

StackSets isn't in the slides, but multi-account deployment comes up in exam questions. This section is written from the AWS documentation.

CloudFormation StackSets create, update or delete stacks across multiple accounts and Regions with a single operation. An administrator account manages the template; with AWS Organizations, new accounts can get the stack automatically.

Real-world examples

  • A start-up keeps its whole environment (VPC, ALB, Auto Scaling group, RDS) in one template, and creates an identical staging copy in 20 minutes.
  • A security team uses StackSets to deploy the same IAM roles and Config rules to all 120 accounts of its organization.

Exam tips

  • "Infrastructure as code", "repeatable environments", "re-create the same infrastructure" → CloudFormation.
  • "Let users deploy stacks without giving them permissions on the resources" → CloudFormation service role + iam:PassRole.
  • "Deploy the same stack to many accounts and Regions" → StackSets.

Common traps

  • "The service role lets the user act on the resources directly." Only CloudFormation uses the role; the user only needs iam:PassRole and CloudFormation permissions.
  • "Delete a resource by hand from a stack." Change the template and update the stack, or the stack drifts.

Hands-on lab: a stack from a template

Goal: create and delete an S3 bucket with CloudFormation.

Costs money

CloudFormation is free for AWS resources, and an empty S3 bucket costs nothing. Any Region works.

Write the template

Save the YAML above as bucket.yaml on your computer.

Create the stack

CloudFormation → Create stack → With new resources → Upload a template file, choose bucket.yaml, stack name saa-bucket-stack, then create.

Look around

Open the stack's Events and Resources tabs. Open the bucket from the Resources tab: versioning is enabled, and it carries aws:cloudformation:stack-name tags.

Expected result: a bucket created by CloudFormation, with a name generated from the stack name.

Clean up: delete the saa-bucket-stack stack; CloudFormation deletes the bucket.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt