Interview Prep/Terraform/Secrets Provisioners and Policy as Code

Write a policy that denies any plan creating an unencrypted EBS volume.

AdvancedScenarioSecrets Provisioners and Policy as Code
Answer

Situation read → Custom rule on plan JSON.

What I'd do →

terraform plan -out=tfplan
terraform show -json tfplan > plan.json
conftest test plan.json -p policy/
package main

deny contains msg if {
  rc := input.resource_changes[_]
  rc.type == "aws_ebs_volume"
  rc.change.actions[_] == "create"
  not rc.change.after.encrypted
  msg := sprintf("%s must set encrypted = true", [rc.address])
}

Why → The plan JSON contains the final computed values, so the policy sees what will actually be built.

Interview tip → Rego deny contains msg if is the current v1 syntax.

All Terraform questions

More on Secrets Provisioners and Policy as Code