Policy as code with OPA, Checkov and Trivy
Check the plan against rules before apply: plan JSON, OPA, Checkov and Trivy compared, plus Terraform Policy, generally available in 1.17.
Formatting, validation and TFLintterraform test: the native testing framework
Policy as code with OPA, Checkov and Trivy
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Scanners read source; policy engines usually read the plan JSON from terraform show -json, which has every module expanded and the action for each resource.
- OPA with conftest has no built-in rules, Checkov ships a large check library, Trivy scans IaC with trivy config; each fits different teams.
- A replacement shows as actions delete+create, which a policy can block even when a reader skims past it.
- Policies must live where the author of the change cannot quietly edit them, with visible exceptions. Terraform 1.17 made Terraform Policy generally available.
Valid is not the same as allowed
A plan can be perfectly valid, pass every test, and still be something your organisation does not allow: a security group open to the internet on port 22, a bucket with no versioning, a p5.48xlarge GPU instance in a dev account. Reviewers catch some of this by eye. They will miss it on the 40th pull request of the week.
Policy as code moves the rule out of someone’s head into a file that runs against every change. There are two places to look, and the tools differ by which one they read.
Two inputs: the code and the plan
The source (.tf files) is available before anything is planned and needs no credentials. A scanner reads it directly and matches resource arguments against known-bad patterns. The drawback is that it sees only what is written. A value that comes from a variable, a module or a for_each may be unknown to it.
The plan is the full result, with every module expanded and every value that can be known resolved. terraform show -json renders it as JSON, which is what policy engines consume. The workflow, from Checkov’s plan-scanning docs:
terraform plan --out tfplan.binary
terraform show -json tfplan.binary | jq > tfplan.json
checkov -f tfplan.json
The JSON has a resource_changes array. Each entry carries the resource address, its type, and a change object with actions (create, update, delete, or delete+create for a replacement) and the after values. A policy asks questions of that structure. This one, jq, which I ran, pulls out everything about to be destroyed:
$ jq -r '.resource_changes[]|select(.change.actions|index("delete"))|"\(.address) \(.change.actions|join("+"))"' plan.json
aws_db_instance.main delete+create
I should be clear that plan.json was written by me in the shape terraform show -json produces. I did not generate it with Terraform. If a pipeline saves the plan and applies that file, what the policy checked is exactly what gets applied.
Three tools
Open Policy Agent (OPA) is a general policy engine with its own language, Rego. You write rules that produce deny messages, and feed it the plan JSON. conftest is the common wrapper for running it on files in CI. OPA has no idea what Terraform is, so you write every rule yourself, but it works the same for Kubernetes manifests, API requests and CI configs. Ned in the Cloud’s video, the embed here, covers OPA with Terraform, including a Rego plan walk-through. Here is the idea as a Rego rule, written from OPA’s documentation and not run, because opa is not installed:
package terraform
deny[msg] {
rc := input.resource_changes[_]
rc.type == "aws_db_instance"
rc.change.actions[_] == "delete"
msg := sprintf("%s would be destroyed", [rc.address])
}
Checkov, is a Python tool with a large built-in library of checks, such as CKV_AWS_21, “Ensure all data stored in the S3 bucket have versioning enabled”. You run checkov -d . on source or checkov -f tfplan.json on a plan. The docs list a few checks that are skipped for plan files because they depend on lifecycle blocks, which a plan does not carry. PyPI’s current Checkov release on 28 November 2026 is 3.4.11.
Trivy, from Aqua Security, scans container images, but trivy config <dir> also scans IaC including Terraform, finds the file type on its own, and takes --severity HIGH,CRITICAL. Trivy’s checks bundle is downloaded from the internet, so an air-gapped runner needs extra setup. Its version on my check date was v0.76.2. Trivy is where the older tfsec scanner went, which I know from the project history and did not re-verify here.
| Tool | Built-in rules | You write rules in | Reads |
|---|---|---|---|
| OPA / conftest | None | Rego | Plan JSON (or any JSON) |
| Checkov | A large library of cloud checks | Python or YAML | Source or plan |
| Trivy | A bundled checks library | Rego | Source (and plans) |
What a policy run looks like
OPA and Checkov are not installed here, so I wrote a Python stand-in that applies five rules to that hand-made plan. The rules mirror what you would write in Rego: buckets need versioning and an Owner tag, no SSH from 0.0.0.0/0, instance types from an allow-list, no deletion of a database. It is not OPA, and its output is mine:
$ python3 -I policy.py plan.json
FAIL aws_s3_bucket.assets: bucket has no versioning resource
FAIL aws_s3_bucket.assets: missing Owner tag
FAIL aws_security_group.admin: SSH open to the world
FAIL aws_instance.big: instance type p5.48xlarge not in allow-list
FAIL aws_db_instance.main: database would be destroyed (actions: delete+create)
6 resource changes, 5 violation(s)
It exits non-zero, which is how a pipeline stops. Notice aws_db_instance.main: its action is delete+create, which is a replacement. An engineer reading terraform plan might skim past “must be replaced”. A policy cannot.
Where policies live and who can override them
A policy only helps if it is hard to bypass. A scan in CI that anyone can edit in the same pull request is a suggestion. Put the rules in a separate repository with its own review, or in the platform. Plan for exceptions, since there will be some: Checkov and Trivy support skip comments in code, and OPA rules can read an exception list. Make each exception visible in review.
A newer built-in option
Terraform 1.17.0, released on 21 October 2026, says in its changelog that “Terraform Policy is now generally available”, and that the -policies flag on plan, apply and query no longer needs an experimental build. HashiCorp’s docs describe policies as HCL policy blocks with enforce blocks, evaluated at setup, plan and apply time, with enforcement levels, and the tfpolicy CLI for local testing. I did not run or write one, and the docs page I read had no example policy, so I do not show one. If you use HCP Terraform, Sentinel and this new feature are the vendor’s route. OPA, Checkov and Trivy work with any CI.
About the video
The embed is Ned in the Cloud’s “How to Use Open Policy Agent with Terraform for Better Infrastructure Management”, 14 minutes 49 seconds, January 2023. It covers OPA with Terraform and does not cover Checkov or Trivy, which this lesson takes from their documentation. The video is from 2023, so it predates Terraform Policy and recent OPA syntax changes: I recall newer OPA versions preferring deny contains msg if { ... } over the older form in my sketch, but I did not check. I did not watch it fully. English was judged from the title, description, channel and its English auto-caption track. Deeper has Aqua’s own 11-minute video on scanning tfplan.json with Trivy, from 2023, and a short Checkov walk-through from 2025.
Where this fits
Policies say what is allowed. Another gate is money, and cost estimation with Infracost puts a price on the same plan.
Check yourself
Why scan the plan as well as the source?
The plan has modules expanded and many values resolved, while source scanning sees only what is written and can miss values from variables or for_each.
What does an action of delete and create mean in a plan?
A replacement: the resource will be destroyed and created again.
Why is a policy check in the same pull request as the code weak?
The author can edit or disable it. Keep rules in a separately reviewed place.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.