Policy as code means writing rules as versioned, testable text that an engine evaluates, in a pipeline or at the cloud API
Facts read 16 Nov 2026. Page reviewed 16 Nov 2026.
Policy as code is the practice of writing rules in a high-level language, keeping them as text in version control, testing them, and having an engine evaluate them automatically. HashiCorp, Open Policy Agent and AWS each publish an engine for it. The rules can run before deployment, at admission or inside the cloud API, as read on 16 Nov 2026.
HashiCorp defines policy as code as writing code to manage and automate policies
HashiCorp's Sentinel documentation says policy as code "is the idea of writing code in a high-level language to manage and automate policies". It contrasts this with policies set by clicking in a console, which it calls hard to repeat and version, with no system for testing a policy other than trying an action that would violate it.
The same page lists five benefits: sandboxing (guardrails for automated systems), codification (logic in code rather than oral tradition), version control, testing, and automation. Open Policy Agent describes itself as a policy engine that "unifies policy enforcement across the stack". AWS describes CloudFormation Guard as a general-purpose policy-as-code evaluation tool.
A rule can be checked at four points, and each point catches different changes
The last column is the verdict of this page. No single point covers everything, so a mature setup uses more than one. The policy as code tools page lists the engines by the same fields.
| Point | What is checked | Engines in this set | What it misses |
|---|---|---|---|
| Before deploy, in CI | A Terraform plan in JSON, a CloudFormation template, a Kubernetes manifest | Open Policy Agent, Sentinel, CloudFormation Guard | Anything changed in the console or by API |
| At cluster admission | A Kubernetes resource as it is created or updated | Kyverno, OPA through Gatekeeper | Anything outside the cluster |
| At the cloud API | Every request in the account | SCP, Azure Policy, organization policy | Nothing the policy language cannot express |
| After creation, from events or schedules | Live resources | Cloud Custodian, AWS Config rules | The window before correction |
The cloud's own policies are policy as code too, and they are the only ones evaluated at the API
A service control policy is a JSON document that can live in a repository and go through review like any other code. So is an Azure Policy definition. Every one of the 100 library entries names its source file and license, many from open-source repositories. What differs is the evaluator: the cloud itself, rather than a separate engine reading a copy of the intent. The AWS sample SCP repository, for instance, is MIT-0 licensed, which lets a team copy a policy into its own repository and change it.
That is why this site treats the two as layers. The cloud-native object is the one that is hard to bypass. An engine upstream is the one that tells a developer early and with a readable message. Testing a guardrail before you apply it covers the cloud-native test options.
Plan-time checks have a documented blind spot
Open Policy Agent states that policies on a Terraform plan cannot see unknown values (computed at apply time), dynamic blocks, or the results of functions not yet evaluated. A plan-time rule that depends on such an attribute passes without checking it.
HashiCorp documents a related limit: HCP Terraform policies are checked during Terraform runs, against the plan, and HashiCorp says custom imports are not supported. A change made outside Terraform is never presented to them. HashiCorp also says a policy set holds policies from one framework only. Real examples of rules in five engines are on the policy as code examples page, and compliance as code covers rule bundles tied to standards.
Related pages
Sources
- HashiCorp: policy as code (Sentinel), read 16 Nov 2026
- Open Policy Agent: introduction, read 16 Nov 2026
- Open Policy Agent: Terraform, read 16 Nov 2026
- AWS: what is CloudFormation Guard, read 16 Nov 2026
- HashiCorp: policy enforcement in HCP Terraform, read 16 Nov 2026
- GitHub: kyverno/kyverno, read 16 Nov 2026
- GitHub: cloud-custodian/cloud-custodian, read 16 Nov 2026
Last reviewed 16 Nov 2026.