Black Friday sale: an extra 40% off every course until Monday, November 30.New Year sale: an extra 30% off every course until Friday, January 1.Memorial Day sale: an extra 25% off every course until Monday, May 31.SysAdmin Day sale: an extra 25% off every course until Sunday, August 1.Back to School sale: an extra 25% off every course until Sunday, August 22.Labor Day sale: an extra 25% off every course until Monday, September 6.Black Friday sale: an extra 40% off every course until Monday, November 29.New Year sale: an extra 30% off every course until Saturday, January 1.Choose your certification →

Copyright (c) 2026 MindMesh Academy. All rights reserved. This content is proprietary and may not be reproduced or distributed without permission.

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:
LayerMechanismBlocks?Bypassed by
Authoring (synthesis)"CDK aspects" (and rule packs such as cdk-nag), "CloudFormation Guard" checks in the pipelineFails cdk synth or the buildAnyone not using that CDK app or pipeline
Provisioning"CloudFormation Hooks"Yes, in FAIL modeConsole, 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 includedNothing in the member account; the management account is not affected by SCPs
Detective + remediation"AWS Config" rules with automatic remediation ("SSM Automation" documents), conformance packsNo; detects and fixes after the factN/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 makes cdk synth fail so nothing is deployed. Aspects do not cross Stage boundaries, 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, and CLOUD_CONTROL for 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 in WARN first 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 rule cloudformation-stack-drift-detection-check turns 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 (deny s3:PutBucketPublicAccessBlock changes, 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.

See how it connects
Alvin Varughese
Written byAlvin Varughese
Founder•26 professional certifications earned