Sub topic 23
Deploying across accounts and Regions with StackSets
This chapter assumes you can create, update and delete a single stack from the CLI. It also assumes you know how template parameters and change sets work. A stack lives in one account and one Region. A StackSet removes that limit.
What a StackSet is
A StackSet is a template plus parameters, managed from one administrator account. You tell CloudFormation which accounts and Regions to deploy to. In each account and Region pair, CloudFormation creates an ordinary stack. These stacks are called stack instances.
The StackSet is the single source of truth. When you update it, CloudFormation pushes the change to every instance. Typical uses are baseline logging, IAM roles for security tooling, and AWS Config rules in every account.
Permission models
The permission model decides how CloudFormation gets into the target accounts. You choose it when you create the StackSet, and you can't change it afterward.
| Model | How it works | Best for |
|---|---|---|
| Self-managed | You create an administration role in the administrator account and an execution role in each target account. | A few accounts, or accounts outside AWS Organizations. |
| Service-managed | CloudFormation uses AWS Organizations and creates the roles for you. You target organizational units (OUs). | Many accounts, and automatic coverage of new accounts. |
For self-managed, the administration role is usually named AWSCloudFormationStackSetAdministrationRole. Each target account needs AWSCloudFormationStackSetExecutionRole, which trusts the administrator account. For service-managed, enable trusted access for StackSets in Organizations first. Run the commands from the management account, or from a delegated administrator account with --call-as DELEGATED_ADMIN.
With service-managed permissions you can turn on automatic deployment. Accounts that join a target OU later then receive the stack instances without any action from you.
A template suited to StackSets
The template is an ordinary one. Avoid hard-coded names for global resources. This bucket has no BucketName, so CloudFormation generates a unique name in each account and Region.
AWSTemplateFormatVersion: "2010-09-09"
Description: Baseline audit bucket deployed by a StackSet
Parameters:
EnvName:
Type: String
Default: dev
Resources:
AuditBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
Tags:
- Key: Environment
Value: !Ref EnvName
Outputs:
AuditBucketArn:
Value: !GetAtt AuditBucket.ArnCreating a StackSet and its instances
Creating the StackSet deploys nothing. You then create stack instances as a separate operation.
# Service-managed StackSet with automatic deployment to new accounts
aws cloudformation create-stack-set \
--stack-set-name audit-baseline \
--template-body file://template.yaml \
--permission-model SERVICE_MANAGED \
--auto-deployment Enabled=true,RetainStacksOnAccountRemoval=true \
--parameters ParameterKey=EnvName,ParameterValue=prod
# Deploy to every account in an OU, in two Regions
aws cloudformation create-stack-instances \
--stack-set-name audit-baseline \
--deployment-targets OrganizationalUnitIds=ou-abcd-12345678 \
--regions us-east-1 eu-west-1 \
--operation-preferences FailureToleranceCount=1,MaxConcurrentCount=2
# Self-managed variant: list account IDs instead of OUs
aws cloudformation create-stack-instances \
--stack-set-name audit-baseline \
--accounts 111111111111 222222222222 \
--regions us-east-1Each command returns an operation ID. Poll it with describe-stack-set-operation, and list per-instance results with list-stack-instances or list-stack-set-operation-results.
Controlling the rollout
Operation preferences limit the damage a bad template can do:
MaxConcurrentCountorMaxConcurrentPercentagesets how many accounts are processed at once within a Region.FailureToleranceCountorFailureTolerancePercentagesets how many instance failures are allowed before CloudFormation stops the operation in that Region.RegionConcurrencyTypeisSEQUENTIAL(the default) orPARALLEL.RegionOrdersets the order for sequential deployment.
Use a small failure tolerance and sequential Regions for risky changes. A failure then stops the rollout before it reaches every account. Instances that were already updated stay updated. StackSets do not roll back successful instances.
Updating, drift and deletion
Run update-stack-set to change the template or default parameters. Use --parameter-overrides on update-stack-instances to give specific instances different values. An instance with overrides keeps them when you later change the StackSet's defaults.
Instance status shows where each instance stands:
CURRENTmeans the instance matches the StackSet.OUTDATEDmeans the last operation on the instance failed or was cancelled.INOPERABLEmeans CloudFormation can't operate on it, often because a role is missing or deleted.
detect-stack-set-drift runs drift detection across all instances. It reports drift against the StackSet's template, which catches manual edits in member accounts.
To remove a StackSet, delete its instances first with delete-stack-instances. Then call delete-stack-set, which fails while any instance remains. Pass --retain-stacks to remove instances from the StackSet but keep the stacks and their resources.
Choosing StackSets
Use StackSets when the same template must exist in many accounts or Regions. Use nested stacks to split one large deployment into pieces within one account and Region. Use cross-stack exports to share values between related stacks. You can combine them. A StackSet template can itself contain nested stacks, as long as the child templates are reachable from every target account.
Exercise
You deploy a StackSet to 40 accounts with FailureToleranceCount=0 and MaxConcurrentCount=5. The template has an error that fails in the first account. What happens to the other accounts, and what do you do next?
Show answer
With a failure tolerance of 0, the operation stops at the first failure, so only the first batch of up to 5 accounts is attempted. Accounts that succeeded stay updated, because StackSets do not roll back successful instances. The failed instance shows OUTDATED. Read the instance's status reason, fix the template, and run update-stack-set again, which retries the outdated instances and continues to the remaining accounts.