HashiCorp's tfpolicy writes Terraform rules in HCL
· Do Minh Tuan · Business Hackers 🚀
HashiCorp has introduced tfpolicy, a policy-as-code framework built for Terraform and written in HCL, the same configuration language Terraform itself uses. It is in public beta on HCP Terraform.
Policy-as-code means writing your rules as code instead of as a document that somebody reads once. Before a change goes live, an automated check looks at what you are about to build and decides whether it is allowed. No public buckets. Every resource tagged with an owner. No database instance above the size we agreed on.
Policy-as-code has been around for a while. What is new here is the language.
Until now, enforcing rules on Terraform usually meant a second tool with a second syntax, either Open Policy Agent with Rego or HashiCorp's own Sentinel. Both are capable. Both also ask an infrastructure engineer to switch languages mid-task, which is exactly the friction I would avoid.
Why the language matters more than the features
I think policy tooling often fails for reasons that have nothing to do with the policy engine. The engine worked. The rules were correct. The problem was that writing a rule required a specialist, so every new check went through one person, and the backlog grew until the rules stopped matching reality.
Bringing policies into HCL changes that in a small but important way. The person writing the module is the person who can add the guardrail. The check lives next to the thing it checks, in the same review, with the same tooling. That is a much better chance of the policy staying current.
It also means the policy review happens in the pull request, where engineers already look. A failed check during a local plan is a teaching moment. A failed check during a deployment, owned by a platform team three time zones away, is an argument.
The trade I would watch is expressiveness. A dedicated policy language can express things HCL cannot. If your organisation has genuinely complex rules about cross-account relationships or approval chains, you may hit the limits of a configuration-language approach. I doubt most companies have rules that complex. They have twelve checks about tags, encryption and public access, and HCL is plenty for that.
What I would put in the first ten rules
Start with the checks that cost money or cause incidents, not the ones that are easy to write.
Block public storage by default. That single rule prevents the most common and most embarrassing cloud incident.
Require an owner tag on everything that costs money. This sounds like housekeeping and it is really a cost control: an untagged resource is invisible in the billing review, and invisible resources are the ones that run for a year.
Cap instance sizes on non-production environments. Teams spin up a large machine to test something, forget it, and the bill arrives six weeks later. A policy that limits the size in dev is cheap insurance.
Require encryption at rest and in transit for anything holding customer data. This is usually already on by default at the provider level, and the exceptions are what you want to catch.
And require a backup configuration on stateful resources. The disasters I worry about here are not attacks. They are a database nobody configured to keep a copy of.
The failure mode of policy-as-code
Policies fail in two directions, and both are common.
Too strict, and engineers route around you. They stop using the module, or they find a resource type the rule does not cover, or they get an exception that becomes permanent. Every exception is a hole, and after twenty exceptions the policy is decoration.
Too loose, and it gives false comfort. A rule that checks whether a tag exists does not check whether the tag is correct. It only checks that someone typed something. A compliance dashboard can read green while the values underneath are nonsense.
So I would give the rule set an owner and a review cadence. Not a big process. A quarterly look at what fired, what got bypassed, and what should be deleted. Policies that never fire are either perfect or pointless, and it is almost never the first.
If you are selling into enterprises, security questionnaires will ask how you enforce configuration standards. The honest answer used to be 'we have a checklist and a careful team'. That answer loses deals.
A repo with policy checks that run on every pull request is a much better answer, and it is not expensive to build. The evidence is in the pipeline, not in a PDF.
It also compounds. Every incident becomes a rule, and the rule prevents the second occurrence. That is a healthier way to grow a platform than relying on the memory of the person who was on call.
One caveat for smaller teams: I would not adopt this because it sounds mature. Adopt it when you have at least a handful of Terraform modules and more than one person touching them. Before that, a short written standard and a review by someone senior is cheaper and works better.
What I am watching is whether tfpolicy stays inside Terraform and HCP Terraform, or grows into something that also covers other tools. Policy is most useful when it covers everything a team deploys, and infrastructure is only part of that.
My guess is that the HCL approach wins adoption for exactly the reason it looks weaker: the people who have to write the rules already know the language.
Written with AI assistance, reviewed and edited by Do Minh Tuan.