Skip to content

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 .tf files; on a plan JSON it sees resolved values — run it on both.
  • An OPA deny that fires in CI blocks the merge — treat new rules as code changes: test them against real plans before gating.
  • terraform test files live in tests/ by convention next to the module under test; -filter= narrows runs while iterating.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.