
Compliance as code and policy as code bring software engineering practices to security and compliance. Instead of describing rules in documents and checking them by hand, you write rules as machine-readable code, store them in version control, test them automatically and enforce them in pipelines and platforms. Policy as code usually refers to the rules themselves, such as "no public storage buckets," evaluated by engines like Open Policy Agent. Compliance as code is the broader practice of expressing controls, mappings, tests and evidence for frameworks such as SOC 2 and ISO 27001 as code. This guide explains both, the tools involved, practical examples and how to adopt them in a small team.
Related guides: Policy drift detection · What is GRC engineering? · Cloud security audit · Benefits of compliance automation
Key takeaways
- Policy as code expresses rules in a language a machine can evaluate and enforce automatically.
- Compliance as code applies the same idea to whole frameworks: controls, mappings, tests and evidence.
- Benefits include consistency, speed, version history and continuous evidence.
- Common tools include Open Policy Agent, Checkov, cloud-native policy services, Kyverno and compliance platforms.
- Start with a handful of high-impact rules in CI/CD and expand gradually.
Definitions

| Aspect | Policy as code | Compliance as code |
|---|---|---|
| Focus | Individual rules and guardrails | Entire control framework and evidence |
| Examples | "Encryption must be enabled," "Containers cannot run as root" | SOC 2 CC6.1 mapped to automated tests with evidence collection |
| Where it runs | CI/CD pipelines, admission controllers, cloud policy services | Compliance platforms, GRC pipelines, reporting |
| Outputs | Allow, deny or warn decisions | Control status, mappings, evidence and reports |
| Users | Platform and security engineers | GRC engineers, compliance teams, auditors |
Both rely on the same principles: version control, peer review, automated testing and repeatable deployment.
Why teams adopt these practices
- Consistency. The same rule is applied every time, everywhere.
- Shift left. Problems are caught in pull requests before they reach production.
- Speed. Engineers get immediate feedback instead of waiting for a review.
- Audit trail. Version history shows when a rule changed, who approved it and why.
- Continuous evidence. Automated checks produce evidence for every control run, not just at audit time.
- Less drift. Guardrails prevent noncompliant changes from being deployed.
Common tools
| Tool | Type | Typical use |
|---|---|---|
| Open Policy Agent (OPA) with Rego | General-purpose policy engine | Kubernetes admission, API authorization, Terraform checks via Conftest |
| Kyverno | Kubernetes-native policy engine | Validating and mutating Kubernetes resources |
| HashiCorp Sentinel | Policy framework for HashiCorp tools | Terraform Cloud and Enterprise policy checks |
| Checkov, tfsec and Trivy | Infrastructure as code scanners | Scanning Terraform, CloudFormation, Kubernetes manifests |
| AWS Config, Azure Policy, Google Cloud Organization Policy | Cloud-native policy services | Detecting or preventing noncompliant cloud resources |
| OSCAL | NIST data format | Machine-readable control catalogs, system security plans and assessment results |
| Compliance automation platforms | Framework mapping and evidence | Linking automated tests to SOC 2, ISO 27001 and other controls |
Practical examples
Infrastructure as code checks in CI
Every pull request that changes Terraform runs Checkov or Conftest. Rules block public storage, unencrypted databases, security groups open to the internet on admin ports and resources without required tags. The pipeline fails with a clear message, and the engineer fixes it before merging.
Kubernetes admission control
Kyverno or OPA Gatekeeper rejects pods that run as root, use the "latest" image tag, lack resource limits or pull from unapproved registries.
Cloud guardrails
Organization policies prevent creating resources in unapproved regions and disallow disabling audit logging.
Control mapping as code
A repository holds YAML files that map each SOC 2 or ISO 27001 control to the automated tests that support it, the owners and the evidence sources. Changes go through pull requests, and a pipeline publishes an updated control matrix.
Evidence collection as code
Scheduled jobs pull configuration exports, user lists and scan results from APIs, timestamp them and store them for auditors.
A simple policy example
A rule in plain language, "S3 buckets must block public access," becomes a test that checks the bucket's public access block settings are all true. In OPA, this is a few lines of Rego that evaluate the Terraform plan and return a deny message for any bucket without the setting. The same rule can run in CI, in a nightly scan of deployed resources and as evidence for ISO 27001 Annex A 8.3 or SOC 2 CC6.1.
How to adopt compliance as code
- Pick high-impact rules. Start with five to ten rules tied to real risks and audit findings: encryption, public exposure, MFA, logging, tagging.
- Choose tools that fit your stack. If you use Terraform and Kubernetes, OPA or Checkov plus Kyverno is a common combination.
- Run in warn mode first. Report violations without blocking, fix the backlog, then switch to enforce.
- Map rules to controls. Record which framework requirements each rule supports.
- Store results as evidence. Keep logs of checks and decisions.
- Version and review rules. Treat policy changes like code changes with reviews.
- Measure. Track violations caught before deployment and drift found after.
Limitations
- Not every control can be automated. Risk assessment, vendor judgment, training quality and management review still need people.
- Poorly written rules create noise or false confidence.
- Engineers need to understand the rules, or they will look for workarounds.
- Policy languages such as Rego have a learning curve.
How auditors view compliance as code
Auditors are increasingly comfortable with automated controls, as long as they can see what the rule does, that it was in place throughout the period, that changes to it were controlled and that exceptions were handled. Provide rule definitions, change history, run logs and samples of blocked or flagged changes. Automated controls can often be tested with fewer samples than manual ones, which reduces audit effort.
Example rule set for a first rollout
| Rule | Where enforced | Framework link |
|---|---|---|
| Storage must not be public | IaC scan in CI and nightly cloud scan | ISO 27001 A 8.3, SOC 2 CC6.1 |
| Databases must be encrypted at rest | IaC scan | ISO 27001 A 8.24, SOC 2 CC6.1 |
| Admin ports not open to the internet | IaC scan and cloud policy | ISO 27001 A 8.20, SOC 2 CC6.6 |
| Audit logging cannot be disabled | Cloud organization policy | ISO 27001 A 8.15, SOC 2 CC7.2 |
| Containers must not run as root | Kubernetes admission control | ISO 27001 A 8.9 |
| Resources must have owner tags | IaC scan | Asset inventory, ISO 27001 A 5.9 |
Start in warn mode for two to four weeks, fix existing violations, then enforce.
How SecureSlate helps
SecureSlate gives small and mid-size teams the benefits of compliance as code without building it all themselves. It runs continuous automated tests across cloud, identity, code and devices, maps each test to SOC 2, ISO 27001, HIPAA and other frameworks with multi-framework mapping, scans code and dependencies and secrets, and stores timestamped evidence for auditors.
FAQ
What is policy as code?
Writing security and compliance rules in a machine-readable language so they can be evaluated and enforced automatically.
What is compliance as code?
Expressing compliance controls, framework mappings, tests and evidence collection as code, managed with version control and automation.
What is Open Policy Agent?
An open-source, general-purpose policy engine that evaluates rules written in Rego for use cases such as Kubernetes admission control and infrastructure as code checks.
Can compliance as code replace auditors?
No. It makes controls more reliable and evidence easier to provide, but independent auditors still provide assurance.
Is compliance as code only for large companies?
No. Small teams using infrastructure as code can add a few policy checks in CI quickly, and compliance platforms provide much of the rest.
Do auditors accept automated controls as evidence?
Yes, when you can show the rule definition, that it ran throughout the period, how changes to it were approved and how exceptions were handled.
Where should policy code live?
In a version-controlled repository with branch protection and required reviews, ideally separate from application code so security and platform teams can own it.
Disclaimer (legal note)
This article is for general information only and is not legal, regulatory, or professional advice. Requirements vary by framework, industry, and jurisdiction. Consult qualified advisors for your specific obligations.
Need compliance without the complexity?
SecureSlate automates ISO 27001, SOC 2, GDPR, HIPAA, and more. Built for growing teams. See it in action.
Find compliance gaps in 30 seconds