Policy as code
How rules written as code, such as required tags or approved regions, are checked against every Terraform plan, the three policy frameworks HCP Terraform supports, and how enforcement levels and policy sets work.
Diagrams in this video · 2Free in archhy
The diagrams, full size
Copy any of them into archhy, free, and change it for your own system.
After this lesson you can
- Explain what policy as code is and what it checks
- Name the three policy frameworks and how they differ
- Describe advisory, overridable and mandatory enforcement
- Explain how policy sets attach to projects and workspaces
Transcript
Policy as code
Space Llama here. Last time, outside tools got a vote in the run. Today, your own rules get a vote too. Every tag, every region, every time. Policy as code.
Rules as code
Every company has rules. Every resource needs an owner tag. Production lives only in approved regions. No deploys on Friday afternoon. Written in a wiki, rules get forgotten. Written as code, they get checked. A policy is a rule that HCP Terraform checks against the Terraform plan, during every run, before anything changes. HCP is the HashiCorp Cloud Platform. Pass, and the run moves on to apply. Fail, and it goes back to the author with a clear reason. And because policies are code, they live in version control, get reviewed, and have a history. Just like your infrastructure.
Three policy frameworks
HCP Terraform supports three policy frameworks. Terraform policy is the newest. You write policies in HCL, the HashiCorp Configuration Language, same as your Terraform code. It needs Terraform 1.17 or later. Sentinel is HashiCorp's own policy-as-code language, and it's been around for years. OPA is the Open Policy Agent, with policies written in a language called Rego. Handy if your company already uses it elsewhere. Terraform policy works with workspaces and Stacks. Sentinel and OPA work with workspaces.
Policy sets
Policies are grouped into policy sets. A set holds policies of one framework only. On the left, a repository of policies. Keeping policies in version control is what HashiCorp recommends. In the Standard and Premium editions of HCP Terraform, a policy set can be connected straight to that repository. Top right, a tagging set, in Terraform policy, applied to the payments project. Every workspace inside gets checked. Below, a regions set, in Sentinel, applied to a single workspace. You can also apply a set to the whole organization. A workspace can have several sets, even in different frameworks.
How strict is a policy?
Each policy has an enforcement level, which decides what a failure does. Advisory never interrupts the run. It only shows the failure. Perfect while you're testing a new rule. Mandatory stops the run. Depending on the framework, someone with permission may override a failure: soft-mandatory in Sentinel, mandatory overridable in Terraform policy. That keeps a person in the loop for the rare, deliberate exception. With a record of who said yes.
Getting started
Start small. Two or three rules everybody agrees on, like required tags. HashiCorp also publishes pre-written policies for common standards, so you don't have to start from a blank page. Begin them as advisory, fix what they find, then make them mandatory. A small amount of policy is included in the free tier, so you can try this without paying. And you can try Terraform policies on your own machine too, before they ever block a teammate.
Recap
Let's recap. Policy as code checks every plan against your rules, before anything changes. Write policies in Terraform policy, Sentinel or OPA, and group them into policy sets for projects, workspaces or the organization. Advisory policies warn. Mandatory ones stop the run, some with an override for people who have permission.
Practice and what's next
That's policy as code. The rules, finally reading every plan. To practice, try the free practice questions at Gazeek Labs. Next lesson, Agents: reaching private networks. See you there.
Gazeek Flash · free
Exam questions on your phone
Free questions on Guardrails and day two: quick-fire batches and timed exams, on the web or Android.






