Policy as code means writing security rules as files that computers can read, test and check automatically.
This guide is for developers, new IT hires and students who keep hearing the phrase. You do not need to be a programmer to follow it: if your team builds anything in the cloud, rules like these may already be checking its work.
In short
- Policy as code turns a written rule, like "no public storage", into a file a computer can check.
- The rule files are saved in version history, reviewed and tested like any other code.
- Tools check changes against the rules before they go live, and can block or flag the ones that break them.
- Start by flagging problems, fix the noise, then switch the important rules to blocking.
What you will learn
- Explain policy as code in one sentence.
- Spot where rule checks run: in the build, in the cloud and in the app.
- Explain why rules start in warn mode before they block.
- Ask who owns and reviews your team's rules.
- Rule (policy)
- a statement of what is or is not allowed, such as "storage must not be public".
- Saved history (version control)
- a system that keeps every version of a file and who changed it.
What is policy as code?
Think of a spell checker. You do not read a rule book before every sentence. The checker reads your writing and underlines mistakes before you send it.
Policy as code works the same way for technology. A team writes its security rules in a file that a computer can read. A typical rule might say "storage must not be open to the public" or "only the admin group may delete records".
A tool then checks each change against those rules. A change might be a new cloud server, a settings file or a request inside an app. If the change breaks a rule, the tool can warn the person or block the change.
Because the rules are files, they live in version control (a system that keeps every version and who changed it). People review a rule change before it is accepted, and tests check that the rule does what it should.[3]
Where the comparison stops working: a spell checker only suggests. A policy tool can be set to refuse a change outright, so a wrong rule can stop good work too.
Why does it matter?
Rules kept only in a document rely on people remembering them. Rules kept as code can be checked on every change, the same way each time, with a record of the result.
If you are a developer
What could happen: your change is blocked and you do not know why, so you look for a way round it.
Check this: read the message the check gives, and ask where the rule file lives.
If you are new to IT or cloud work
What could happen: you set up storage by clicking in a web console, and nobody checks it was set up safely.
Check this: ask which cloud rules your team has switched on, and whether they warn or block.
If you manage a team
What could happen: rules pile up with no owner, and people stop trusting the checks.
Check this: make sure every rule has an owner, a reason and a test.
What does it look like?
Real tools each have their own rule language. Here is a made-up rule in plain words, laid out the way rule files often are.
Example only. All values are made up.
# rules/storage.policy (saved in version control) rule: no-public-storage (1) check: storage.public_access is false (2) when broken: warn (3) test: example-bucket-0000 set to public -> warn (4) owner: [email protected] (5)
- The rule's name, so people can find it in reports.
- The condition the tool checks on each change.
- What happens if the rule is broken: warn for now, block later.
- A test with a made-up storage name, so the team knows the rule really catches the problem.
- Who to ask if the rule is wrong or needs an exception.
Why this is risky: a rule with no test can quietly stop catching anything. A rule set to block on day one can halt good work, so people learn to bypass checks.
The safer way: each rule has an owner and a test. It starts in warn mode, and moves to blocking once the noise is fixed. Microsoft's cloud policy guidance gives the same advice: start with audit effects before enforcement.[4]
How does it work?
Most policy as code follows the same few steps.
- Write. Someone turns a written rule into a rule file in the tool's language.
- Review and test. The rule is saved in version control, reviewed by a second person and tested. For example, the Open Policy Agent tool runs tests written alongside its rules.[2]
- Check. A change is sent to the tool, often as structured data. The tool compares it with the rules and returns a decision.[1]
- Act. The system that asked carries out the decision: it allows, warns or blocks the change.[1]
- Report. Results are recorded, so the team can see what broke which rule, and when.[4]
These checks can run in three common places. In the build pipeline (the automatic steps that test and release code), a check can stop a risky settings file before it goes live.[3] In the cloud, services such as Azure Policy can deny or flag resources that break a rule.[4] In Kubernetes (a system for running apps in containers), built-in admission policies check requests as they arrive.[5] Apps can also ask a policy engine "may this user do this?", which our authorisation beginner guide covers.


A worked example
Wanda Okafor joins a small online booking company as a junior cloud engineer. Her team lead is Marek Grover.
In her first week, Marek shows her a list of written cloud rules in a shared document. One says "no storage may be open to the public". He admits nobody checks it.
Wanda thinks of a spell checker. Instead of hoping people read the list, a tool could check every change. Marek agrees she should try one rule.
She writes the "no public storage" rule as a file, with a test that uses a made-up storage name set to public. Marek reviews it, and the test passes.
They switch the rule on in warn mode. On the first day it flags three storage areas. Two are old test areas nobody needs, so they are removed. The third holds pictures for the public website, which really should be public.
Marek approves a written exception for that one, with a reason, an owner and a review date. Wanda adds it to the rule file, so the exception is reviewed like everything else.
After two quiet weeks, they switch the rule to blocking. A month later a developer tries to make a new storage area public by mistake. The check blocks it with a clear message, and he fixes it in minutes.
Myths and mistakes
| What people think | What to do instead |
|---|---|
| Policy as code is only for big companies. | Start with one or two rules that matter most, such as no public storage. |
| Turn every rule on in blocking mode straight away. | Start in warn or audit mode, fix the noise, then block.[4] |
| Once the rules are written, the job is done. | Rules need owners, tests and reviews, like any other code.[2] |
| Policy as code replaces people's judgement. | It checks the rules people agreed. Exceptions still need a human decision, recorded with a reason. |
| The checks cover everything, so we are safe. | They only check what the rules describe and where the checks run. Changes made outside those paths can be missed. |
What can I do this week?
- Ask where your team's security rules are written down today.
- Find out whether any checks already run in your build pipeline or cloud account.
- Pick one rule that matters and ask what it would take to check it automatically.
- Read the message next time a check blocks or warns you, and follow it to the rule.
Checklist
- Important security rules are written as rule files, not only in documents.
- Rule files are kept in version control.
- Every rule change is reviewed by a second person.
- Every rule has a test, an owner and a reason.
- New rules start in warn or audit mode before they block.
- Exceptions are written down with a reason, an owner and a review date.
- Check results are recorded and reviewed.
Common questions
Do I need to learn a new language?
Usually a little. Each tool has its own way of writing rules. Open Policy Agent uses a language called Rego, and Kubernetes admission policies use one called CEL. Many teams start from example rules and change them, which is a gentle way to learn.
Is policy as code the same as a policy engine?
Not quite. A policy engine is the tool that makes the decision. Policy as code is the habit of writing, storing and testing the rules as files. Most policy engines read rules written as code, so the two often go together.
Is policy as code the same as infrastructure as code?
No, but they are close friends. Infrastructure as code describes what to build, such as servers and storage. Policy as code describes what is allowed. A common setup checks the infrastructure files against the rules before anything is built.
What happens when a rule is wrong?
Fix the rule through the same review process, not by turning checks off. A wrong rule is a normal bug. Tests help catch it early, and warn mode keeps a bad rule from blocking work while it is fixed.
Where next?
Ready for the detail? Read the practitioner guide: Policy-as-Code for Authorisation Guide. It goes further into rule languages and engines, how to build the policy pipeline and how to secure the engine itself.
Also useful:
- Security Policy as Code
- Authorisation and Policy Engines for Beginners
- Infrastructure as Code Security for Beginners
- Cloud Misconfigurations for Beginners
Related NHI Mgmt Group resources: Open Policy Agent in Production · CI/CD Pipeline Security for Beginners · Least Privilege for Beginners
Key terms
- Policy as code
- Writing security rules in a form computers can read, kept in version control and checked automatically.
- Policy engine
- A tool that checks a request or change against the rules and returns a decision.
- Open Policy Agent
- A free, general-purpose policy engine that checks structured data against rules.
- Rego
- The rule language used by Open Policy Agent.
- Version control
- A system that keeps every version of a file, with who changed it and when.
- Guardrails
- Controls that keep activity within agreed limits, such as cloud rules that apply to every account.
- CI/CD pipeline
- The automatic steps that build, test and release software.
Sources and further reading
- Introduction to Open Policy Agent. Open Policy Agent documentation, CNCF. https://www.openpolicyagent.org/docs/latest/, accessed 28 Nov 2026.
- Policy testing. Open Policy Agent documentation, CNCF. https://www.openpolicyagent.org/docs/latest/policy-testing/, accessed 28 Nov 2026.
- Policy as code. Sentinel documentation, HashiCorp. https://developer.hashicorp.com/sentinel/docs/concepts/policy-as-code, accessed 28 Nov 2026.
- Overview of Azure Policy. Microsoft Learn, Microsoft. https://learn.microsoft.com/en-us/azure/governance/policy/overview, accessed 28 Nov 2026.
- Validating Admission Policy. Kubernetes documentation, The Kubernetes Authors. https://kubernetes.io/docs/reference/access-authn-authz/validating-admission-policy/, accessed 28 Nov 2026.