Lesson 5 of 7 Deployment Strategies and Infrastructure as Code

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.

Sort into groups

When does the check run?

Exam skills 5.1.2

Decide when each policy-as-code control evaluates resources.

Log in to try this activity Interactive activities are free with an account.

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.

WhenControlCatches
Pull requestcfn-guard, CDK aspectsViolations in templates and CDK code
ProvisioningCloudFormation Hooks, proactive controlsNon-compliant resources from any pipeline
API callSCPsNon-compliant requests from any tool
After deploymentConfig rules, drift detectionManual changes and drift

The following diagram shows where each control acts in the life of a change.

Policy as code at each stage. CloudFormation Guard checks templates in the pull request, CDK aspects check every construct at synthesis, CloudFormation Hooks check each resource as CloudFormation provisions it, and AWS Config rules with drift detection catch changes made outside infrastructure as code after deployment.
The earlier a check runs, the cheaper the fix; the later a check runs, the more kinds of change it sees.
Worked example

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.

Check yourself

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.