Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Policy as code means writing organisational rules in a machine-readable form that lives in version control, is tested like software, and is enforced automatically at the points where decisions are made.

What Is Policy as Code?
Policy as code is the practice of expressing organisational rules — security standards, compliance requirements, architectural constraints — in files that a machine evaluates, rather than in documents that a human is supposed to have read. The rules live in a repository, go through review, carry tests, and are enforced automatically wherever a relevant decision is made.
The shift is from “our standard says storage buckets must be encrypted” to a rule that refuses to create one that is not.
A configuration standard in a wiki has three predictable problems. Nobody reads it at the moment they need it. It drifts from what is actually deployed within weeks. And verifying compliance means a person manually comparing a document to an environment, which happens annually at best and is wrong by the time it is finished.
Encoding the same rule gives you three things the document never could: it applies to every change rather than the ones someone checked, it is versioned so you can see when and why it changed, and the enforcement record is evidence. Most security misconfigurations in cloud environments are violations of a rule the organisation had already written down somewhere.
The same requirement usually needs expressing at more than one point, because each point catches a different mistake.
| Point | Typical mechanism | Catches |
|---|---|---|
| Developer machine | Pre-commit hook | Obvious mistakes, secrets, before they are shared |
| Pull request | IaC scanner, Conftest, custom rules | Misconfiguration in proposed change, with review context |
| Before apply | Terraform plan evaluation, Sentinel | Resolved values that source files cannot show |
| Admission | Kyverno, Gatekeeper, Validating Admission Policy | Anything deployed by any route, including manually |
| Runtime | Posture management, cloud guardrails such as SCPs | Drift, console changes, resources created outside the pipeline |
A rule enforced only in continuous integration is bypassed by anyone with console access. A rule enforced only at admission gives developers no feedback until deployment. The combination of an early advisory check and a late blocking one is what works.
Open Policy Agent with Rego is the general-purpose option, used through Gatekeeper in Kubernetes and Conftest in pipelines. Kyverno keeps Kubernetes policy in YAML. HashiCorp Sentinel gates Terraform runs in the commercial editions. AWS service control policies and Azure Policy enforce at the cloud control plane, where nothing can route around them. Infrastructure scanners such as Checkov and KICS allow custom rules of their own.
The choice matters less than consistency. Three engines with three languages and three exception processes is a common and avoidable outcome of letting each team pick separately.
denied by policy violation 7 generates a support ticket.Auditors increasingly ask how a control is enforced rather than whether it is documented. For ISO 27001:2022, control A.8.9 covers configuration management, and a policy repository with a decision log answers it far better than a narrative. Cyber Essentials requires secure configuration across devices and services, and the NCSC’s cloud security principles expect separation and configuration to be demonstrable. For financial entities under DORA, evidence that ICT changes are checked against defined requirements before deployment is directly in scope.
The reporting side also improves: policy engines emit machine-readable results, so “which workloads violate which control today” becomes a query rather than a project.
No, though they work together. Infrastructure as code describes what you want to exist. Policy as code describes what is permitted to exist. One is a specification, the other a constraint on all specifications. Policies are usually evaluated against infrastructure code, but they also apply to running resources, API requests, pipeline artefacts, and anything else where a rule should be enforced automatically.
If Kubernetes is the main enforcement point and application teams will write the rules, Kyverno’s YAML approach is quicker to adopt. If you need one language across Kubernetes, pipelines, and service authorisation, Open Policy Agent with Rego is the stronger foundation despite the learning curve. Cloud-native guardrails such as service control policies should be used regardless, because nothing can bypass them.
Build the exception process before you start enforcing. Each exemption should name an owner, state the reason, and carry an expiry date, and it should live next to the resource it applies to rather than in a pipeline configuration nobody reads. Review expiring exceptions on a schedule. Without this, teams invent bypasses that are invisible to everyone including you.
Badly implemented, yes. The usual causes are enforcing from day one rather than auditing first, error messages that do not say how to comply, and no route to an exception. Done well it does the opposite, because the answer to what is allowed is immediate and automatic instead of waiting for a security review. Measure how long a policy failure takes to resolve, and fix the rules that score worst.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.