How would you fail a pipeline when a plan destroys something critical?
AdvancedProductionTesting and Policy as Code
Answer
Inspect the plan JSON for delete actions on protected resource types.
terraform show -json tfplan | jq -e '
[.resource_changes[]
| select(.change.actions | index("delete"))
| select(.type | IN("aws_db_instance","aws_s3_bucket","aws_kms_key"))] | length == 0'
A non-zero exit fails the job, forcing an explicit override path. Equivalent logic can live in OPA or Sentinel.
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