Interview Prep/Terraform/Testing and Policy as Code

How do OPA and Conftest check a Terraform plan?

IntermediateProductionTesting and Policy as Code
Answer

Export the plan to JSON and evaluate Rego rules against resource_changes.

package terraform
deny[msg] {
  rc := input.resource_changes[_]
  rc.type == "aws_s3_bucket"
  not rc.change.after.server_side_encryption_configuration
  msg := sprintf("%s must be encrypted", [rc.address])
}
terraform show -json tfplan > plan.json && conftest test plan.json -p policy/
All Terraform questions Roadmap: Terraform in production

More on Testing and Policy as Code