3.5.1.5. Policy as Code and Multi-Account Infrastructure Deployment (CloudFormation Hooks, CDK Aspects, StackSets)
💡 First Principle: A rule is only as strong as the earliest point at which it is enforced and the paths that can bypass it. Layer the controls: catch violations while the code is written, block them before provisioning, deny them at the API, and detect and remediate whatever still slips through.
Scenario: A platform team runs 120 accounts. Developers deploy with "AWS CDK" and raw "CloudFormation", a few teams still click in the console, and auditors want proof that no unencrypted bucket or public security group can be created, plus automatic correction of anything that drifts.
The four enforcement points:
| Layer | Mechanism | Blocks? | Bypassed by |
|---|---|---|---|
| Authoring (synthesis) | "CDK aspects" (and rule packs such as cdk-nag), "CloudFormation Guard" checks in the pipeline | Fails cdk synth or the build | Anyone not using that CDK app or pipeline |
| Provisioning | "CloudFormation Hooks" | Yes, in FAIL mode | Console, CLI and SDK calls that do not go through CloudFormation or Cloud Control API |
| API authorization | "SCPs" (and RCPs for resource-side limits) | Yes, for every caller in the account, console included | Nothing in the member account; the management account is not affected by SCPs |
| Detective + remediation | "AWS Config" rules with automatic remediation ("SSM Automation" documents), conformance packs | No; detects and fixes after the fact | N/A (catches drift from any source) |
"CDK aspects": an aspect is a visitor that CDK calls on every construct in a scope during the prepare phase of synthesis. It can mutate constructs (CDK's own tagging is an aspect) or validate them and add an error annotation, which makescdk synthfail so nothing is deployed. Aspects do not crossStageboundaries, so apply them on the Stage itself. They run on the developer's or build machine, so treat them as fast feedback, not a server-side guarantee."CloudFormation Hooks": code that CloudFormation invokes before it creates, updates or deletes resources or stacks, and when a change set is created (targets:RESOURCE,STACK,CHANGE_SET, andCLOUD_CONTROLfor Cloud Control API calls). Write them as Guard hooks (declarative"CloudFormation Guard"rules), Lambda hooks (any logic, can call other services), custom hooks (CloudFormation CLI), or adopt"Control Tower"proactive controls, which are delivered as hooks. A failed check either stops the operation (FAIL) or only records a warning (WARN); roll out new hooks inWARNfirst to see what they would break. Hooks are activated per account and Region, so distribute them with a service-managed StackSet."AWS Config"rules: detective mode evaluates deployed resources and, with automatic remediation, runs an SSM Automation document on non-compliant resources (with a retry count and retry window). The managed rulecloudformation-stack-drift-detection-checkturns CloudFormation drift into a Config finding. Proactive evaluation mode answers "would these properties be compliant?" before deployment, but it does not block or remediate anything by itself."SCPs": the backstop for actions that must be impossible regardless of tool (denys3:PutBucketPublicAccessBlockchanges, deny leaving the organization, deny creating resources without required tags).
Multi-account and multi-Region deployment with "CloudFormation StackSets":
- Permission models: self-managed means you create the administration role and an execution role in every target account (works for accounts outside an Organization). Service-managed uses
"AWS Organizations"trusted access: CloudFormation creates the roles, you target the whole organization or OUs (optionally filtering accounts), and you can run it from a delegated administrator account instead of the management account. Service-managed StackSets never deploy into the management account itself and do not support nested stacks, macros or transforms. - Automatic deployment: new accounts that join a target OU get the stack automatically, and on removal you choose to delete or retain the stacks. Parameter overrides apply only to accounts present when you set them; accounts added later get the StackSet defaults.
- Safe rollout controls: maximum concurrent accounts, failure tolerance (per Region; exceeding it stops the operation and it does not continue to later Regions), Region concurrency (sequential in a chosen Region order, or parallel), and StackSet dependencies (deploy the networking StackSet before the workload one). A template update applies to every stack instance; you cannot update the template for some instances only.
- A StackSet is a regional resource: manage it from one Region, which deploys to the Regions you list.
⚠️ Exam Trap: A CloudFormation Hook, a CDK aspect and a Config proactive rule all act before deployment, but only the hook can block provisioning, and only for changes made through CloudFormation (or Cloud Control API). If the requirement says "no one, by any means, can create X", the answer needs an SCP; if it says "detect and automatically correct drift", it needs Config rules with automatic remediation.