Contents
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.hclfrom 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 | tfpolicy validate --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 | package terraform |
Same rule in tfpolicy: watch provider metadata. Setup goes red before you waste a plan.
1 | policy { |
So: learn tfpolicy early if you care, or… maybe you never needed to learn the old stack at all.
Links
- Introducing tfpolicy
- Terraform Agent Skills
- Pre-written Terraform Policy Library
- Terraform Policy Documentation
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
