Deploying Secure Baselines with Infrastructure as Code and Policy as Code
This lesson explains how deploying through reviewed, tested templates reduces misconfiguration, and how policy-as-code checks catch security problems before resources exist.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how StackSets deploy security baselines to every account.
- Check templates with cfn-lint, CloudFormation Guard and Hooks.
- Explain how deployed stacks are protected from manual changes.
StackSets for baselines
Every account in an organisation needs the same security baseline: IAM roles for security tooling and incident response, AWS Config rules, logging settings and detective controls. Deploying these by hand drifts quickly and misses new accounts. CloudFormation StackSets deploy one template to many accounts and Regions. With service-managed permissions, StackSets use AWS Organizations to create the necessary roles, and with automatic deployment enabled, the stack is deployed to every account added to a target OU and removed from accounts that leave it. CDK and Terraform pipelines are common alternatives.
StackSets baselines
Checking templates
Templates can be checked at two points: in the pipeline, before deployment, and inside CloudFormation during deployment. In the pipeline, cfn-lint catches invalid properties, deprecated settings and departures from best practice. CloudFormation Guard evaluates policy-as-code rules, such as "every bucket is encrypted" or "no security group allows 0.0.0.0/0 on port 22", against templates, Kubernetes manifests and Terraform plans. Access Analyzer custom checks examine the IAM policies the templates contain.
Pipeline checks can be bypassed by anyone who deploys outside the pipeline. CloudFormation Hooks close that gap: they run Guard rules or Lambda functions inside CloudFormation itself and block non-compliant resources however the deployment was started. Hooks are the basis of Control Tower's proactive controls. The following table summarises the tools.
| Tool | When | Checks |
|---|---|---|
| cfn-lint | Pipeline | Invalid properties, deprecated settings, best practice |
| CloudFormation Guard | Pipeline | Policy-as-code rules on templates, Kubernetes manifests, Terraform plans, e.g. "every bucket encrypted", "no 0.0.0.0/0 on port 22" |
| CloudFormation Hooks | Deployment, inside CloudFormation | Guard rules or Lambda; blocks non-compliant resources even if the pipeline is bypassed (basis of Control Tower proactive controls) |
| Access Analyzer custom checks | Pipeline | IAM policies in templates |
Pipeline checks fail the build on violations.
The following diagram shows one set of CloudFormation Guard rules and the two places where they are enforced.

Template checks
Protecting deployed stacks
Infrastructure as code is reliable only if the deployed resources still match the code. Manual changes made in the console bypass review and can weaken security controls. CloudFormation drift detection compares stack resources with their template and reports differences, and AWS Config can track changes continuously. Stack policies prevent updates to critical resources during stack updates, termination protection prevents a stack from being deleted accidentally, and SCPs can deny changes to baseline resources by anyone except the deployment role.
Protecting deployed stacks
Guard rules for storage security
Every template must encrypt S3 buckets with KMS, block public access, and never open SSH to the world. Guard rules express these requirements and run in the pipeline and, as a hook, inside CloudFormation:
let buckets = Resources.*[ Type == 'AWS::S3::Bucket' ]
rule buckets_use_kms when %buckets !empty {
%buckets.Properties.BucketEncryption.ServerSideEncryptionConfiguration[*]
.ServerSideEncryptionByDefault.SSEAlgorithm IN ['aws:kms', 'aws:kms:dsse']
<<Buckets must use SSE-KMS>>
}
rule buckets_block_public when %buckets !empty {
%buckets.Properties.PublicAccessBlockConfiguration.BlockPublicAcls == true
%buckets.Properties.PublicAccessBlockConfiguration.RestrictPublicBuckets == true
}
let sgs = Resources.*[ Type == 'AWS::EC2::SecurityGroup' ]
rule no_world_ssh when %sgs !empty {
%sgs.Properties.SecurityGroupIngress[*] {
when FromPort <= 22 ToPort >= 22 { CidrIp != '0.0.0.0/0' <<SSH must not be open to the internet>> }
}
}cfn-guard validate --data template.yaml --rules storage.guard fails the build with the custom messages when a rule is broken. Registering the same rules as a CloudFormation Guard hook enforces them even for stacks deployed outside the pipeline.
How can a build fail when a template creates an unencrypted bucket?
Show answer
With CloudFormation Guard rules in the pipeline.
Exam Tips
Remember for the exam
- For a baseline in every current and future account, use service-managed StackSets with automatic deployment.
- To fail a build when a template creates an unencrypted bucket, use CloudFormation Guard.
- To catch invalid template properties before deployment, use cfn-lint.
- To block non-compliant resources at deployment time whatever the pipeline, use CloudFormation Hooks.
- To detect manual changes to deployed resources, use drift detection.