Interview Prep/Terraform/Testing and Policy as Code

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.

All Terraform questions Roadmap: Terraform in production

More on Testing and Policy as Code