Enforcing Policy as Code Across the Delivery Lifecycle
This lesson explains how cfn-guard, CDK aspects, CloudFormation Hooks, SCPs and Config rules enforce rules at different points, and why layering them closes gaps.
Learning outcomes
After completing this lesson, you will be able to:
- Describe what cfn-guard, CDK aspects and CloudFormation Hooks check and when.
- Explain the gaps that SCPs and Config rules close.
- Design layered controls for a governance rule.
Checks before deployment
AWS CloudFormation Guard (cfn-guard) is a policy language and tool that validates templates against rules, for example that every bucket has encryption and Block Public Access; it runs in pull request pipelines against CloudFormation templates, including those synthesised from the CDK. CDK aspects visit every construct in an application during synthesis and can add errors, warnings or missing settings, so one aspect enforces a rule for every resource of a type. These checks give developers feedback before anything is deployed.
When does the check run?
Checks at provisioning and at the API
CloudFormation Hooks run when CloudFormation creates or updates resources, stacks or change sets, evaluating them with Guard rules, Lambda functions or managed controls, and fail or warn before a non-compliant resource is created, whichever pipeline submitted the template; Control Tower proactive controls are implemented as hooks. SCPs deny non-compliant API calls from any tool, including the console and SDKs, for rules that can be expressed as conditions on API requests.
Detection and drift remediation
Changes made outside infrastructure as code are invisible to hooks and pipeline checks. AWS Config rules evaluate every configuration change and run remediation through Systems Manager Automation, and CloudFormation drift detection shows where stacks differ from their templates. Layering the controls closes the gaps that each one leaves.
| When | Control | Catches |
|---|---|---|
| Pull request | cfn-guard, CDK aspects | Violations in templates and CDK code |
| Provisioning | CloudFormation Hooks, proactive controls | Non-compliant resources from any pipeline |
| API call | SCPs | Non-compliant requests from any tool |
| After deployment | Config rules, drift detection | Manual changes and drift |
The following diagram shows where each control acts in the life of a change.

Enforcing encryption everywhere
A company requires every EBS volume, S3 bucket and RDS database to be encrypted with customer managed keys, and teams deploy through CloudFormation, the CDK and occasionally the console.
A CDK aspect and a shared cfn-guard rule set fail pull requests that omit encryption; a CloudFormation Hook with the same Guard rules blocks non-compliant stacks in every account.
EBS encryption by default is enabled in every account and Region, an SCP denies creating unencrypted databases, and Config rules with remediation catch anything created in the console during an incident.
Why do CloudFormation Hooks not replace AWS Config rules?
Show answer
Hooks only see changes made through CloudFormation. Config rules evaluate every configuration change, including console and SDK changes, and can remediate drift.
Exam Tips
Remember for the exam
- Block non-compliant resources before CloudFormation creates them, for every pipeline: CloudFormation Hooks.
- Enforce a rule on every construct of a CDK app at synthesis: a CDK aspect.
- Feedback on pull requests: cfn-guard validate in the pipeline.
- Changes made outside IaC: Config rules with remediation and drift detection.