Sub topic 18
Deploying across accounts and Regions with StackSets
This chapter assumes you can write a template, use parameters and outputs, and run stack operations from the AWS CLI. It also assumes you know what a change set is.
What a StackSet is
A stack lives in one account and one Region. A StackSet is a template plus configuration that you manage from an administrator account. CloudFormation creates a stack instance, which is an ordinary stack, in each target account and Region.
StackSets suit baseline resources that every account needs, such as logging buckets, IAM roles, and security rules.
Permission models
- Service-managed uses AWS Organizations. You target organizational units (OUs). With auto-deployment on, new accounts that join an OU receive the stack automatically.
- Self-managed uses IAM roles that you create. The administrator account needs
AWSCloudFormationStackSetAdministrationRole. Each target account needsAWSCloudFormationStackSetExecutionRole. You list account IDs yourself.
Creating and deploying a StackSet
aws cloudformation create-stack-set \
--stack-set-name baseline \
--template-body file://template.yaml \
--permission-model SERVICE_MANAGED \
--auto-deployment Enabled=true,RetainStacksOnAccountRemoval=false
aws cloudformation create-stack-instances \
--stack-set-name baseline \
--deployment-targets OrganizationalUnitIds=ou-abcd-12345678 \
--regions us-east-1 eu-west-1 \
--operation-preferences FailureToleranceCount=1,MaxConcurrentCount=2The operation preferences control the rollout. MaxConcurrentCount limits how many accounts run at once. FailureToleranceCount sets how many failures are allowed before the operation stops. Start with small values so a bad template does not reach every account at once.
Some behavior differs from a normal stack:
- Parameters can be overridden per instance, which is why
parametersleads into StackSets. - Stack instances can drift. Run drift detection on the StackSet, not just on single stacks.
- Hard-coded names such as a bucket name can collide across Regions or accounts. Build names with
!Suband pseudo parameters like${AWS::AccountId}and${AWS::Region}.
Designing templates and stacks that last
- Group by lifecycle. Put resources that change together in one stack. Keep databases and networks apart from application code.
- Keep stacks small. Smaller stacks have a smaller blast radius and update faster. Use nested stacks to reuse a pattern and exports to share values.
- Preview every update. Create a change set, read the Replacement column, and only then execute it.
- Protect stateful resources. Set
DeletionPolicyandUpdateReplacePolicytoRetainorSnapshot. Turn on termination protection for production. - Let CloudFormation name things. Fixed physical names block replacement updates, because the new resource cannot share the old name.
- Manage changes in one place. Use version control and avoid console edits. Run drift detection regularly to catch edits made outside CloudFormation.
- Avoid fragile exports. An export that another stack imports cannot be changed or deleted. Use parameters for loosely coupled values.
Exercise
Your StackSet creates an S3 bucket with the fixed name company-logs. Deployment succeeds in one Region and fails in the others. Why, and how do you fix it?
Show answer
S3 bucket names are globally unique, so only the first instance can create company-logs. Remove the fixed name so CloudFormation generates one, or build it with !Sub "company-logs-${AWS::AccountId}-${AWS::Region}".