Infrastructure policy and regression tests
Separate the layers of verification
Consider Cedar, a fictional order service with a private S3 artifact bucket, a deployment pipeline and a database. Infrastructure as code (IaC) describes intended infrastructure; valid JSON or YAML does not mean valid AWS resource configuration. A schema checker can find unsupported properties; policy checks can identify organization-specific violations; neither proves actual resource behavior. A regression test prevents a previously fixed defect from returning. For example, assert all four S3 Block Public Access settings are true for the artifact bucket, then change one to false and confirm the test fails. This is a narrow instructional requirement, not a complete S3 security audit.
Select the right tool for each claim
Use cfn-lint to inspect CloudFormation structure and properties against available specifications; use CloudFormation Guard to evaluate explicit policy rules on structured input. The reviewed documentation describes cfn-guard validate --rules rules.guard --data template.yaml --type CFNTemplate. For SAM, sam validate --lint --template myTemplate.yaml invokes detailed linting. CDK assertions inspect synthesized templates: a partial property match proves only the tested properties, not all resources. Terraform validate checks configuration consistency after required modules and providers are initialized; terraform test can run plan or apply operations, so inspect its run blocks and provider mocks before executing. None of these native tools is installed in this authoring environment; commands are documented examples, not executed results. Keep untrusted synthesis and parsing isolated without credentials or network.
Review permissions and proposed changes
IAM Access Analyzer policy validation reports grammar and best-practice findings; custom checks can evaluate specified new-access conditions. A valid policy is not proof that a particular request succeeds: identity, resource, session and organization policy conditions still matter. A CloudFormation change set previews proposed changes, including replacements; review deletion, replacement, data retention and dependent applications before execution. Creation of a real change set is an AWS API operation, not a purely local action. The following fixture is an intentionally small static template excerpt; it is not deployable evidence or a complete access policy.
{ "Resources": { "ArtifactBucket": { "Type": "AWS::S3::Bucket", "Properties": { "PublicAccessBlockConfiguration": { "BlockPublicAcls": true, "IgnorePublicAcls": true, "BlockPublicPolicy": true, "RestrictPublicBuckets": true } } } }}Build a test matrix with negative cases
Unit tests check pure logic, contract tests compare request and response shapes, integration tests check interactions, and smoke tests exercise a small critical path after rollout. Add a negative authorization test for a different tenant, duplicate-event replay, restore verification and bounded load. Record environment, artifact digest, inputs, expected output, actual output and limitations for each test. A mocked API can reproduce selected error responses but not real IAM, quotas, timing or regional behavior. A skipped mandatory check stays unknown, not passed. Practice: remove a bucket setting in a copy of the fixture and predict the policy finding; restore the original before running tests.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Infrastructure policy and regression tests — knowledge check
Reason about test evidence, safe promotion and recovery.
What does valid JSON establish?
Which tool evaluates explicit CloudFormation policy rules?
What does a CDK partial property assertion prove?
Why inspect Terraform test run blocks?
What should a negative tenant authorization test establish?