Lesson 13 of 14 · 17m
Linting, Policy, and Testing Modules
checkov and tflint scan config, OPA evaluates the resolved plan JSON, and `terraform test` proves module behavior — each gates a different thing.
Code review doesn't scale to infrastructure. A human can eyeball that 0.0.0.0/0 ingress on port 443 is intended and the same CIDR on port 22 is a breach — until the diff is 200 lines and the review happens on a phone. Policy as code moves that judgment into a gate that runs the same check on every plan, every time.
Two layers, different inputs
Static analysis on the config. checkov, tflint, and trivy config (which absorbed tfsec) scan .tf files for known-bad patterns — public S3 buckets, unencrypted EBS, SG rules open to the world — without running anything. Cheap, fast, in every PR job. The limit: they see literals, not resolved values. A CIDR passed in via var.admin_cidr is invisible to a scanner reading text.
Policy on the resolved plan. terraform show -json plan.tfplan dumps the post-resolution plan — every attribute, computed or literal — and OPA/conftest evaluates Rego rules against it:
terraform plan -out=plan.tfplan
terraform show -json plan.tfplan > plan.json
conftest test --policy policy/ plan.json# policy/sg.rego — no SSH from the internet, ever
deny contains msg if {
rc := input.resource_changes[_]
rc.type == "aws_security_group_rule"
rc.change.after.type == "ingress"
rc.change.after.from_port <= 22
rc.change.after.to_port >= 22
some cidr in rc.change.after.cidr_blocks
cidr == "0.0.0.0/0"
msg := sprintf("%s: port 22 open to 0.0.0.0/0", [rc.address])
}The plan JSON sees the resolved 0.0.0.0/0 whether it came from a literal, a variable, or a module three levels deep — that's the gap static scanning can't cover, and the reason the two layers coexist rather than compete. Sentinel is HCP Terraform's hosted version of the same idea; OPA is the portable one.
Testing the modules themselves
Policies gate merges. Tests gate the modules you publish — terraform test (Terraform 1.6+) runs .tftest.hcl files containing run blocks:
# tests/vpc.tftest.hcl
run "rejects_bad_cidr" {
command = plan
variables {
cidr = "not-a-cidr"
}
expect_failures = [var.cidr]
}
run "creates_two_azs" {
command = plan
assert {
condition = length(aws_subnet.private) == 2
error_message = "module must create exactly two private subnets"
}
}command = plan tests assertions without spending money or touching cloud APIs; command = apply runs a real deploy-and-destroy cycle for behavior that only exists at runtime. mock_provider blocks stub the provider entirely for pure unit tests. Gruntwork's Terratest predates the native framework — Go tests that apply real configs against real AWS and assert on the live API — slower, costlier, and still the stronger check for module logic that only manifests in a running system.
Assertions in the config itself
check blocks (1.5+) run after apply and warn without failing — "the endpoint actually responds" as a standing assertion attached to the config, reported in plan/apply output. precondition/postcondition in a resource's lifecycle block do fail — hard stops when an assumption breaks, evaluated per-resource at plan time.
The division of labor: precondition guards inputs, postcondition guards what the API returned, check monitors reality, terraform test proves module behavior, OPA proves policy. They overlap on purpose — a security rule you enforce in five places survives the four you forgot to update.
Field notes
checkov --framework terraform -d .runs against a directory of.tffiles; on a plan JSON it sees resolved values — run it on both.- An OPA
denythat fires in CI blocks the merge — treat new rules as code changes: test them against real plans before gating. terraform testfiles live intests/by convention next to the module under test;-filter=narrows runs while iterating.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
What is Infrastructure as a Service? The Layer That Makes Everything Else Possible
Infrastructure as a Service gives you virtualized servers, storage, and networking on demand without owning any hardware. You rent compute and own everything that runs on it. Here is what IaaS actually is, what it demands from you.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,