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/
More on Testing and Policy as Code
How do you test infrastructure code, and what does the testing pyramid look like?BeginnerWhat does the static-analysis layer catch?BeginnerWhat does tflint do, and how is it configured?BeginnerHow do Checkov and Trivy differ?IntermediateHow does `terraform test` work?BeginnerHow do mock providers work in `terraform test`?Advanced