Terraform tfpolicy vs Sentinel and OPA for plan-time policy checks

Contents

  1. 1. What tfpolicy actually gates
  2. 2. Low-friction start: official libs + Agent Skills
  3. 3. Pain 1: policy queue ate my pipeline
  4. 4. Pain 2: blocking a provider source meant chewing plan JSON

Something I’ve picked up in ops: some tech you never learn—and that’s fine.

Terraform policy used to mean Sentinel or OPA. Different languages, different mental models.

This year HashiCorp shipped tfpolicy to GA. If you already write HCL, the bar to entry is a lot lower.

What tfpolicy actually gates

It works at three layers: Provider, Module, and Resource. Terraform hands you the context—you’re not reverse-engineering plan JSON by hand.

graph LR
  subgraph Setup
    PP["Provider Policy
source, version"] MP["Module Policy
source, version"] end subgraph Plan RPp["Resource Policy
attrs in plan"] end subgraph Apply RPa["Resource Policy
attrs after apply"] end MP --> RPp --> RPa
Checks Examples
Provider source & version Pin or allow-list providers
Module source Only org-published or local modules
Resource attrs No anonymous public storage, etc.

Setup can fail before providers download. Plan and Apply each get their own shot at resource rules.

With Rego I lived in plan export land—walk resource_changes, glue policy to JSON, debug through another layer. tfpolicy at least doesn’t make you a JSON parser first.

Low-friction start: official libs + Agent Skills

HashiCorp also pushed Terraform Agent Skills. Writing rules got cheaper. (AI tokens: yeah, not my bill.)

The terraform-policy skill can:

  • Draft .policy.hcl from a plain-English ask
  • Write and tune .policytest.hcl
  • Migrate Sentinel → Terraform Policy
  • Move whole policy repos

Heavier lifting splits into tfpolicy-author and tfpolicy-test.

Describe what you want and hand it to the agent. For example:

Write an Azure policy: Storage Accounts must block anonymous public access, with positive and negative policy tests.

You still run tfpolicy validate and tfpolicy test on whatever comes back.

What to block and who approves exceptions—that’s still on you.

You don’t have to start from zero. The Pre-written Terraform Policy Library has a pile of ready-made rules:

Cloud Policies
AWS 423
Azure 82
GCP 68

The Azure set tracks CIS Microsoft Azure Foundations 6.0.0—storage, SQL, Key Vault, networking, IAM, the usual suspects.

Run the official pack first, add custom rules where you need them. (Other clouds: you’re mostly on your own—fair warning.)

Convenient now. Painful to remember when OPA was the default. Two war stories:

Pain 1: policy queue ate my pipeline

On Terraform Cloud, more workspaces means more plans—and then Policy Evaluation in line.

Busy day, a bunch of repos firing at once. Plan’s done; Bitbucket still waits. Everyone’s stuck behind the OPA queue.

You open TFC later and half of them already finished—they just never made it back to Bitbucket in time.

Now you can run tfpolicy in CI without waiting on TFC’s policy step for everything:

1
2
tfpolicy validate --policies=policies/
tfpolicy test --policies=policies/

Pain 2: blocking a provider source meant chewing plan JSON

Alicloud had two provider addresses for a while: old hashicorp/alicloud, current aliyun/alicloud. Skip the block and Terraform defaults to the legacy one.

Mix sources and required_providers fights you. Plans spam deprecation warnings; from China the registry pain is its own thing.

OPA meant digging resource_changes and configuration.provider_config. Rego can do it—eventually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package terraform

import rego.v1

deny contains msg if {
some resource in input.resource_changes
resource.provider_name == "registry.terraform.io/hashicorp/alicloud"
msg = sprintf("Resource '%s' uses invalid provider.", [resource.address])
}

deny contains msg if {
some i
input.configuration.provider_config[i].full_name ==
"registry.terraform.io/hashicorp/alicloud"
msg = sprintf("Provider config '%s' uses invalid provider.", [i])
}

Same rule in tfpolicy: watch provider metadata. Setup goes red before you waste a plan.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
policy {
required_providers {
alicloud = {
source = "aliyun/alicloud"
}
}
}

provider_policy "alicloud" "approved_source" {
enforce {
condition = meta.source == "registry.terraform.io/aliyun/alicloud"
error_message = "Only aliyun/alicloud is allowed. Migrate off hashicorp/alicloud."
}
}

So: learn tfpolicy early if you care, or… maybe you never needed to learn the old stack at all.


Links

Questions? Leave a comment below or email: newbvirgil@gmail.com