Enforcing Standards with Policy as Code
Reviewing templates by hand does not scale and misses mistakes. Policy as code expresses security, compliance and operational standards as machine-checkable rules that run in pipelines and, as a final safeguard, inside CloudFormation itself. This lesson explains template linting, AWS CloudFormation Guard, CloudFormation Hooks and Control Tower proactive controls, cdk-nag and IAM Access Analyzer policy checks, and how they combine.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish between syntax linting, policy rules and IAM policy validation for infrastructure code.
- Explain how AWS CloudFormation Guard rules are written and run in a pipeline.
- Explain how preventive checks at deployment time (hooks and proactive controls) complement pipeline checks.
- Describe how IAM Access Analyzer validates policies and checks for new access in templates.
Layers of Policy Checks
Standards can be checked at several points. Each point catches problems at a different cost and with different certainty, and mature organisations use all of them.
- In the developer's editor and pre-commit hooks: immediate feedback, but optional.
- In the pipeline build stage: mandatory for code that goes through the pipeline; failures stop the change before deployment.
- At deployment time in the account: CloudFormation Hooks and Control Tower proactive controls evaluate every stack operation, including ones that bypass the pipeline.
- After deployment: AWS Config rules and Security Hub detect non-compliant resources, however they were created, as covered in later modules.
Preventive checks (the first three) stop problems; detective checks (the last) find problems that slipped through or were created outside IaC. Exam scenarios that require that non-compliant resources are never created point to preventive controls; scenarios that require detecting existing non-compliance point to detective controls.
The following diagram shows the four layers in the order a change meets them.

Which policy check?
Template Linting with cfn-lint
cfn-lint is an open-source linter for CloudFormation and SAM templates. It validates templates against the resource specification: property names and types, allowed values, references to resources that do not exist, deprecated runtimes and many best-practice rules. It catches errors that would otherwise fail during deployment, such as a misspelt property, in seconds. Custom rules can be added. aws cloudformation validate-template checks only syntax, so cfn-lint is far more thorough.
AWS CloudFormation Guard
AWS CloudFormation Guard (cfn-guard) is an open-source policy-as-code tool that evaluates JSON or YAML documents, such as CloudFormation templates, CDK-synthesised templates, Terraform plans in JSON or Kubernetes manifests, against rules written in a domain-specific language. A rule selects resources by type and asserts conditions on their properties.
# s3_rules.guard
let buckets = Resources.*[ Type == 'AWS::S3::Bucket' ]
rule s3_encryption_enabled when %buckets !empty {
%buckets.Properties.BucketEncryption.ServerSideEncryptionConfiguration[*]
.ServerSideEncryptionByDefault.SSEAlgorithm IN ['aws:kms', 'AES256']
<<Buckets must have default encryption>>
}
rule s3_versioning_enabled when %buckets !empty {
%buckets.Properties.VersioningConfiguration.Status == 'Enabled'
<<Buckets must have versioning enabled>>
}In a pipeline, a CodeBuild step runs cfn-guard validate --data template.yaml --rules rules/, which exits non-zero when a rule fails and therefore fails the build. Guard also has a test command for unit testing rules against sample inputs. AWS publishes Guard rule sets in the Guard rules registry mapped to compliance frameworks, and AWS Config custom policy rules can be written in Guard to evaluate deployed resources with the same language.
Checks at Deployment Time
Pipeline checks cover only changes that go through the pipeline. To guarantee a standard for every stack in an account, the check must run inside CloudFormation. CloudFormation Hooks, described in the CloudFormation module, run before resources are created, updated or deleted and can fail the operation. Guard hooks reuse Guard rules directly, and Lambda hooks run custom code.
AWS Control Tower proactive controls are AWS-managed hooks that Control Tower enables on OUs. Each control checks a specific property, such as requiring encryption on an Amazon EFS file system or requiring an RDS instance to be Multi-AZ, for every CloudFormation operation in the enrolled accounts. Because they are managed, they need no rule authoring, and they appear alongside preventive (SCP-based) and detective (Config-based) controls in the Control Tower console.
cdk-nag and IAM Access Analyzer Checks
For CDK applications, cdk-nag applies rule packs during synthesis, as described in the AWS CDK lesson, so problems are found before a template is even produced. Because it runs inside the build, it is a pipeline check.
IAM policies inside templates deserve special attention, because an over-broad policy may be valid syntax and pass simple rules. IAM Access Analyzer provides two pipeline-friendly capabilities. Policy validation (ValidatePolicy) checks policies against IAM grammar and best practices and returns errors, security warnings and suggestions. Custom policy checks use automated reasoning to answer specific questions: CheckNoNewAccess determines whether a new policy grants more access than a reference policy, CheckAccessNotGranted determines whether a policy grants any of a list of sensitive actions, such as iam:PassRole or s3:DeleteBucket, and CheckNoPublicAccess determines whether a resource policy allows public access. The open-source IAM Policy Validator for CloudFormation runs these checks against templates in a pipeline.
The table below summarises the tools and where they run.
| Tool | What it checks | Where it runs | Type |
|---|---|---|---|
| cfn-lint | Template correctness and best practices | Editor, pipeline | Preventive |
| CloudFormation Guard | Custom policy rules on templates and other JSON or YAML | Pipeline, Guard hooks, Config custom policy rules | Preventive and detective |
| cdk-nag | Rule packs on CDK constructs | CDK synthesis in the pipeline | Preventive |
| IAM Access Analyzer checks | IAM policy validity, new access, sensitive actions, public access | Pipeline | Preventive |
| CloudFormation Hooks | Resources, stacks and change sets before provisioning | CloudFormation in each account | Preventive |
| Control Tower proactive controls | Managed resource property checks | CloudFormation in enrolled accounts | Preventive |
| AWS Config rules | Deployed resource configuration | Each account | Detective |
A Policy Gate for Infrastructure Changes
An organisation wants every infrastructure change to meet encryption and network standards, wants IAM policies in templates never to grant more access than the previous version without security review, and wants the standards to apply even to stacks created manually.
-
Build stage
Run cfn-lint and cfn-guard against the organisation's rule set; both fail the build on violations.
-
IAM gate
Run the IAM policy validator with CheckNoNewAccess against the policies of the currently deployed template; if new access is found, the pipeline requires a security team approval action.
-
Account enforcement
Activate a Guard hook with the same rules in FAIL mode in every account through StackSets, and enable relevant Control Tower proactive controls.
-
Detection
Deploy AWS Config rules for the same standards to detect resources created outside CloudFormation.
A team must guarantee that no CloudFormation stack in its accounts can create a security group that allows inbound SSH from 0.0.0.0/0, including stacks created from the console. Is a cfn-guard step in the pipeline sufficient?
Show answer
No. A pipeline check covers only changes made through the pipeline. A CloudFormation Hook (for example a Guard hook in FAIL mode) or an equivalent Control Tower proactive control evaluates every stack operation in the account.
Exam Tips
Remember for the exam
- cfn-lint validates templates far more thoroughly than validate-template; cfn-guard evaluates custom rules and exits non-zero to fail builds.
- Pipeline checks cover only pipeline changes; CloudFormation Hooks and Control Tower proactive controls enforce standards for every stack operation in an account.
- IAM Access Analyzer ValidatePolicy, CheckNoNewAccess, CheckAccessNotGranted and CheckNoPublicAccess check IAM policies in pipelines.
- Preventive controls stop non-compliant resources from being created; detective controls such as AWS Config find existing non-compliance.