The Terraform plan ran, but something felt wrong. Infrastructure was about to change, and no one had verified the rules. This is where Open Policy Agent (OPA) steps in.
OPA is a policy engine that checks configurations before they deploy. With Terraform, it becomes a guardrail. You write policies in Rego, OPA’s declarative language, and run them against Terraform plans. Every resource, every variable, every parameter — verified before apply.
Integrating OPA with Terraform starts with exporting your plan as JSON. Terraform’s terraform show -json command produces output that OPA can read. You point OPA to this file and match it against custom rules. For example: ensure all S3 buckets have encryption, block public IPs, enforce naming conventions. These rules run locally, in CI/CD pipelines, or in cloud-native workflows.