CloudFormation StackSets, registry and security
Clear study notes on AWS CloudFormation StackSets, the CloudFormation registry, and security/permissions models for provisioning resources across multiple AWS accounts and regions.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
CloudFormation StackSets and the CloudFormation registry let engineers provision and share infrastructure reliably across many AWS accounts and regions. Engineers use StackSets when they must deploy the same CloudFormation template (for example, an S3 bucket, IAM roles, or a VPC component) to many accounts/regions; they use the registry to share, version and reuse resource types and modules across teams.
Key ideas
Stack: the runtime instantiation of a template. A stack has a name, a set of resources, a state (CREATE_COMPLETE, UPDATE_IN_PROGRESS, etc.), and a stack policy optionally protecting resources from updates.
CloudFormation StackSet: a construct that deploys a single template as stack instances across multiple target accounts and regions. Formally: "A StackSet is an object that defines which template to use and which accounts and regions should have stack instances created from that template." StackSet manages operations (create/update/delete) across many accounts and regions as one logical operation.
Assumptions: StackSets require either
- service-managed permissions (the StackSet service uses AWS Organizations to deploy across accounts), or
- self-managed permissions (you create IAM roles in target accounts and the administrator uses them).
Administrator account (in StackSets): the AWS account where you create the StackSet and from which StackSet operations are initiated. This is sometimes called the management or admin account. Target accounts are the accounts where stack instances are created.
Service-managed permissions: a StackSet permission model where CloudFormation assumes a service role and uses AWS Organizations to access target accounts. This is simpler when your accounts are in an Organization. Requires that the accounts be part of the organization and that you enable trusted access. Use when you want the service to manage cross-account deployment roles. You must enable trusted access for StackSets in AWS Organizations and, optionally, designate a delegated administrator account; StackSets uses the AWSServiceRoleForCloudFormationStackSets service-linked role for operations.
Self-managed permissions: a StackSet permission model where you create an IAM role (the StackSet execution role) in each target account and give the administrator account permission to assume that role. Use when accounts are not in a single AWS Organization or you want manual control.
CloudFormation registry: a registry for resource types and modules. It allows teams to publish reusable resource types (custom resource types backed by handlers) and modules (grouped logical declarations) for reuse. The registry holds versioning metadata and supports public and private (account/organization scoped) registration. It also supports Hooks (type HOOK) that run before resource operations to validate and enforce guardrails; hooks can be registered privately (account/organization) and are important for security controls.
Resource type: a defined type with a name like
MyCompany::Category::Resourcethat can be used in the Resources section of a template. Resource types can be Amazon-managed (built-in), public third-party, or private (your account or organization).Module: a reusable template fragment packaged as a module and referenced in other templates (like a function or library of resources).
Change set: a preview of changes that CloudFormation will apply to a stack when you update it. Always review a change set to verify updates, replacements and deletions.
Drift and drift detection: Drift means the actual live resource configuration differs from the stack template's expected configuration. Drift detection is the process (API call) that compares stack resource properties to template declarations and reports differences.
Security principles:
- Follow least privilege when creating the IAM roles used by StackSets and registry operations.
- Use organization-level controls (Service Control Policies) and guardrails to limit what StackSets can provision.
- Record and audit StackSet operations using CloudTrail and CloudFormation operation logs. Also consider CloudFormation Hooks to enforce preventive controls (for example, deny unencrypted S3 buckets) before stack operations.
Diagrams to draw (for an exam):
- Draw a rectangle labelled "Administrator account (StackSet)" at top. Below it draw three rectangles labelled "Target Account A", "Target Account B", "Target Account C". Draw arrow from Administrator to each Target labelled "assume execution role" for self-managed, and a separate diagram showing a single arrow to AWS Organizations (service-managed) which fans out to targets. Label roles: "AWSCloudFormationStackSetAdministrationRole" in the administrator account and "AWSCloudFormationStackSetExecutionRole" in each target for self-managed; for service-managed, note the service-linked role "AWSServiceRoleForCloudFormationStackSets".
Formulas
(For this programming / infra topic, memorize these syntaxes and patterns.)
CloudFormation template skeleton (YAML):
AWSTemplateFormatVersion: '2010-09-09'Description: Brief descriptionParameters:ParamName: { Type: String, Default: '...' }Resources:MyResource: { Type: 'AWS::S3::Bucket', Properties: { BucketName: !Ref ParamName } }Outputs:BucketName: { Value: !Ref MyResource }Create a StackSet (AWS CLI example):
aws cloudformation create-stack-set --stack-set-name MySet --template-body file://template.yaml --capabilities CAPABILITY_NAMED_IAMCreate stack instances in accounts/regions (CLI):
aws cloudformation create-stack-instances --stack-set-name MySet --accounts 111122223333 444455556666 --regions us-east-1 ap-south-1Register a private resource type (CLI basic form):
aws cloudformation register-type --type RESOURCE --type-name MyCompany::Network::Subnet --schema-handler-package s3://bucket/handler.zipDetect drift on a stack (CLI):
aws cloudformation detect-stack-drift --stack-name MyStack
(Verify exact flags and required parameters before copy-paste; see verify list.)
Worked examples
- Create a StackSet to deploy an S3 bucket across two accounts in two regions (step-by-step): Given: Admin account A and target accounts B (111122223333) and C (444455556666). Regions: ap-south-1 and us-east-1. Template: minimal S3.
Steps:
- Prepare template.yaml with
AWS::S3::Bucketand parameterBucketPrefix. - Decide permissions: accounts are in an AWS Organization so choose service-managed permissions.
- From admin account run:
aws cloudformation create-stack-set --stack-set-name OrgS3Set --template-body file://template.yaml --capabilities CAPABILITY_NAMED_IAM --permission-model SERVICE_MANAGED --auto-deployment Enabled=true,RetainStacksOnAccountRemoval=true
- Create stack instances for the organization (example using OUs or explicit accounts):
aws cloudformation create-stack-instances --stack-set-name OrgS3Set --accounts 111122223333 444455556666 --regions ap-south-1 us-east-1aws cloudformation create-stack-instances --stack-set-name OrgS3Set --deployment-targets OrganizationalUnitIds=ou-xxxx-xxxxxxxx --regions ap-south-1 us-east-1 --operation-preferences FailureTolerancePercentage=10,MaxConcurrentPercentage=50 --parameter-overrides ParameterKey=BucketPrefix,ParameterValue=org
- CloudFormation will start an operation that creates 4 stack instances (2 accounts × 2 regions). Monitor using
describe-stack-set-operation.
Notes: If using self-managed permissions, you must pre-create an execution role in each target account that allows the administrator to assume it; then supply --administration-role-arn and --execution-role-name when creating the StackSet.
- Registering a private resource type in the registry (conceptual trace):
Goal: publish a reusable
MyCompany::DB::ReadReplicatype backed by a handler in Lambda or a container.
Steps:
- Package the type: create a handler package (zip) and upload to S3:
s3://my-bucket/handlers/readreplica.zip. - From the account where you want the private type, run registration (example):
aws cloudformation register-type --type RESOURCE --type-name MyCompany::DB::ReadReplica --schema-handler-package s3://my-bucket/handlers/readreplica.zip
- Wait for registration to complete. Then set a default version if there are many versions.
- Use the new type in templates:
Type: MyCompany::DB::ReadReplicain Resources.
Security: restrict who can register and invoke handlers using IAM. Use resource policies on S3 and IAM policies for registry APIs.
- Detecting drift and remediation (step-by-step model answer): Question: "How do you detect and fix drift for a StackSet-created stack instance?"
Model answer steps:
- Use the CloudFormation drift detection API for stacks:
detect-stack-drift --stack-name <name>or use Console -> Stack -> Detect drift. For StackSets, useaws cloudformation detect-stack-set-drift --stack-set-name <StackSetName>(ordetect-stack-instance-driftfor a single instance) and thendescribe-stack-set-operationto review results. - CloudFormation reports which resources are drifted and which properties differ.
- For a single instance in a StackSet, you may remove drift by performing a stack update through the StackSet (update StackSet -> stack instances) or by manually updating the resource in the target account to match the template.
- Best practice: do not make out-of-band changes; use drift detection regularly and enforce via automation.
Common mistakes
- Not passing the right capabilities: use CAPABILITY_IAM or CAPABILITY_NAMED_IAM when templates create/modify IAM resources, and CAPABILITY_AUTO_EXPAND when using macros; CloudFormation will reject the operation.
- Ignoring change sets: updating a StackSet without reviewing the operation may replace resources unexpectedly across many accounts.
- Expecting immediate global consistency: StackSet operations are eventually consistent and run asynchronously; monitor operation status.
- Mis-scoped registry sharing: registering a private type only in a single account (or without enabling trusted access and sharing from the management/delegated admin account) leads to availability problems for other accounts in the organization.
- Not auditing handler code for registered types: custom resource handlers run code with permissions — treat them as code with privileged access.
For your exam
Typical question formats:
Definitions: Define a StackSet, difference between service-managed and self-managed permissions. Full-marks answer: concise definition, typical use-case, diagram (admin + targets) and list of key steps.
Short notes: Explain the CloudFormation registry and how modules differ from types. Full-marks answer: definitions, examples of use, versioning and visibility, and security considerations.
Derivations/diagrams: Draw and explain architecture for deploying a VPC template to 10 accounts across 3 regions with self-managed permissions. Full-marks answer: the diagram, roles to create, assume-role flow, commands or APIs used, and explanation of failure/retry behavior.
Programs/commands: Give AWS CLI commands to create a StackSet and to register a type. Full-marks answer: correct CLI syntax (include required flags like --capabilities when IAM is created), describe outputs to watch.
Case-study/long answer: Plan a migration where a new IAM baseline must be enforced via StackSets. Include roles, SCPs, rollback strategy and auditing. Full-marks answer: architecture, security controls (SCP, IAM least privilege), stepwise operations, testing and rollback plan.
Quick check
- What does a StackSet manage?
- Name the two permission models for StackSets.
- What is drift in CloudFormation?
Answers: A StackSet manages deployment of a template as stack instances across multiple accounts and regions.; Service-managed permissions and self-managed permissions.; Drift is when a live resource's configuration differs from the resource's configuration declared in the stack template.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?