Compliance as Code: Automating What Auditors Want
Updated: Nov 19
Corrected, 4 October 2026. We checked this post's framework details against the source standards. The HIPAA safeguard list had left out person or entity authentication, and two claims about SOC 2 are now qualified. Updated, 19 November 2026: Microsoft is retiring Azure Blueprints, so it's no longer recommended here.
In most organisations, compliance looks like this. Once a year, the security team sends every engineering team a spreadsheet asking them to describe their controls. The teams fill it in based on what they believe is true. The responses are compiled into a report, an auditor asks follow-up questions, and everyone goes back to work until next year.
The process is expensive and adversarial, and it measures what people believe about their systems rather than what the systems actually do. Compliance as code replaces the belief with a check: policies written as rules, evaluated automatically against real configuration and real activity, producing evidence continuously.
That's the promise, and it holds up. But there's a gap most teams fall into. A check that passes is evidence that one path to production is controlled. It isn't proof that the control works. This post covers how to close that gap: start from the evidence your auditor actually asks for, cover every route a change can take, handle exceptions in the open, and choose tools that fit where your changes happen.
What compliance as code means
Compliance as code expresses regulatory requirements, security policies and internal standards as machine-readable rules, and evaluates them automatically against infrastructure, code and process records.
Instead of a policy document saying "all storage buckets must be encrypted at rest", you write a rule that checks every bucket's encryption setting. Instead of asking teams to describe their access controls, a rule checks permission policies against least-privilege requirements. The output isn't a filled-in spreadsheet. It's a continuously updated record of which controls pass and which fail, with the data behind each result.
Two kinds of rule do different jobs:
Kind of rule | When it runs | What it does | Example |
Preventive | Before a change is applied | Blocks non-compliant changes | Reject a Terraform plan that makes a bucket public |
Detective | Continuously, against what's running | Finds drift and changes made outside the pipeline | Flag a bucket whose encryption was switched off in the console |
You need both. Preventive rules only see changes that go through the pipeline. Detective rules see what actually exists, however it got there.
The test: for each automated control you have, write down whether it's preventive, detective or both. Any important control with only a preventive rule is blind to changes that bypass the pipeline.
Start from the evidence, not the framework
The hardest part of compliance as code isn't writing rules. It's deciding what each rule must prove. Frameworks are written in principles. SOC 2, for example, is organised around the AICPA's Trust Services Criteria: security, which every SOC 2 report covers, plus availability, processing integrity, confidentiality and privacy where they're relevant. A criterion like "the entity uses encryption to protect data in transit" has to become specific checks: TLS required on every load balancer, database connections encrypted, plain HTTP rejected, service-to-service traffic encrypted.
The most reliable starting point isn't the framework text. It's the list of evidence your auditor asked for last time. Ask, for each item:
"What evidence did the auditor actually ask for, and where does the data that proves it live?"
That question turns an abstract control into a concrete data source. "Show us that production changes are reviewed" becomes "the pull request history, the deployment log and the cloud provider's audit trail". Once you know where the data lives, you know what the rule has to query, and you can see straight away when the data doesn't exist.
The test: take your last audit's evidence request list. For each item, note where the data came from and how long it took to produce. Items that took the longest and came from manual screenshots are your first candidates for automation.
A worked example: the change-control rule that covered one path
This is a composite drawn from common patterns, not one company. The figures are illustrative.
A SaaS company preparing for its second SOC 2 Type 2 audit, which assesses whether controls operated effectively over a period rather than just whether they were designed, automated its change-management control. The control said that changes to production are reviewed and approved before they're applied. The team implemented it as:
branch protection requiring one approving review before merge to the main branch
a policy rule in the deployment pipeline that rejected any Terraform plan not linked to an approved pull request
Every check passed for the whole audit period. The dashboard was green.
The auditor sampled 25 production changes from the cloud provider's own audit trail, not from the pipeline. Three had no pull request at all. One was a security group opened in the console during an incident and never closed. One was a database parameter changed by hand to fix a performance problem. One was a deployment run from an engineer's laptop with the pipeline's credentials. All three were real changes to production, and none had gone through the path the rule checked.
The fork. The team could have argued that the three were exceptions outside the scope of the control. The control didn't say "changes made through the pipeline". It said "changes to production". So they went back and listed every way a change could reach production:
Path to production | Controlled before? | Control added |
Merge to main, deployed by the pipeline | Yes | Kept |
Console changes by people | No | Detective rule on the cloud audit trail: any write action by a person, outside an approved break-glass window, opens a ticket |
Pipeline credentials used from outside the pipeline | No | Credentials restricted to the pipeline's identity; use from anywhere else alerts |
Emergency break-glass access | No (undocumented) | Break-glass role with time-limited access; every use needs a follow-up review within two working days |
Manual database changes | No | Database audit logging; changes reconciled weekly against tickets |
The next audit period had five break-glass uses, each with a review recorded. No unexplained changes appeared in the auditor's sample.
The discovery move. The question that exposed the gap was the evidence question, asked against the auditor's own data source: what does the cloud audit trail show, and does every entry trace back to an approved change? The team's rule had been written against the data they controlled, the pipeline. The auditor tested against the data that showed what actually happened.
The test: list every way a change can reach production in your environment, including consoles, scripts, emergency access and third-party tools with write access. For each path, name the rule that would catch an unapproved change. Any path without one is where your next audit exception will come from.
Exceptions belong in code too
Every real environment has legitimate exceptions: a legacy system that can't be encrypted until it's migrated, a vendor integration that needs a broader permission. If exceptions are handled by switching rules off or editing them by hand, you lose track of them, and they outlive the reason they were granted.
Handle exceptions the same way as the rules:
Recorded in the policy repository, next to the rule they modify, so they're reviewed like any other change.
Specific: one resource or account, not a whole rule disabled.
Owned: a named person accountable for removing it.
Expiring: a date after which the rule applies again, unless someone renews the exception on purpose.
An exception list kept this way is itself evidence, because it shows the auditor that deviations are known, justified and time-limited.
The test: count the exceptions to your policy rules. For each, check whether it has an owner and an expiry date. Any without both should be renewed with an owner and date, or removed.
The tools, and where they fit
Choose tools by where your changes happen and where the evidence lives.
Tool | Best at | Notes |
Open Policy Agent (OPA) | General policy engine for any structured data: Kubernetes admission, Terraform plans, API authorisation | Policies written in Rego; a graduated Cloud Native Computing Foundation project |
HashiCorp Sentinel | Policy on Terraform and other HashiCorp products | Runs inside the plan-and-apply workflow |
AWS Config | Detective rules on AWS resources, with configuration history | Managed rules for common requirements, plus custom rules |
Azure Policy | Audit, deny or remediate Azure resources | For packaging policies with templates, use template specs and deployment stacks; Azure Blueprints is being retired |
Google Cloud organisation policies, Security Command Center | Constraints and posture findings across a Google Cloud organisation | Combine preventive constraints with detective findings |
On Azure: Microsoft announced the retirement of Azure Blueprints in 2023. Its current timeline is a phased retirement starting 31 July 2026 and full retirement on 31 January 2027, with deployment stacks and template specs as the replacements. Earlier versions of this post recommended Blueprints, which is why the update note above exists.
For the controls themselves, NIST's OSCAL (Open Security Controls Assessment Language) defines machine-readable formats for control catalogues, system security plans and assessment results, in XML, JSON and YAML. If your organisation reports against NIST-based frameworks such as FedRAMP, mapping your rule results to OSCAL means the evidence can be exchanged in a standard form, not just pasted into a document.
Building it into the delivery pipeline
The most effective place for preventive rules is the pipeline, before a change reaches production:
Before merge: secret scanning, dependency vulnerability scanning and configuration linting on every pull request.
At plan time: policy rules evaluated against the infrastructure plan before it's applied, catching public buckets, open security groups and unencrypted resources before they exist.
At deployment: gates that require the checks and any approvals to have passed.
After deployment: detective rules that compare what's running with what was declared, and catch changes that didn't come through the pipeline.
Step 4 is the one most teams leave out, and the worked example shows why it matters most for an audit.
SOC 2, ISO 27001 and HIPAA in practice
SOC 2 is principles-based, which gives you flexibility in how you meet each criterion but means you have to justify your choices. Access control, change management, monitoring, incident response and data protection generate evidence continuously and are good candidates for automation. A Type 2 report covers a period, so continuous detective evidence is especially valuable: it shows the control operating throughout, not just on the day someone checked.
ISO/IEC 27001 is more structured, with a defined set of controls in Annex A (93 in the 2022 revision). Mapping controls to checks is more direct, and there's significant overlap with SOC 2, so automating one gives substantial coverage of the other.
HIPAA's technical safeguards in 45 CFR 164.312 map closely to automated rules: access control, audit controls, integrity, person or entity authentication, and transmission security. The administrative safeguards, such as policies, training and risk assessment, are harder to automate. Use automated evidence where it fits, and keep the human processes for the rest.
Working with auditors
Most auditors welcome evidence generated from system state, because it's more reliable than self-reported answers. Some will need to understand how it works. Show them the rules, how they're tested and changed, where the data comes from and how exceptions are handled. Then expect them to test it against an independent source, as in the worked example. Evidence that only covers the path you control is the first thing a good auditor will look past.
The Continuous AI Auditing post applies the same idea, starting from what data would prove each policy statement, to AI systems.

What to do this week
Pull last year's evidence request list and note where each item's data came from.
List every path to production, including consoles, scripts and emergency access, and name the rule that covers each.
Add one detective rule on your cloud audit trail for changes made outside the pipeline.
Audit your exceptions for an owner and an expiry date.
If you're on Azure, check whether anything still depends on Blueprints and plan the move to deployment stacks.
Final takeaway
Compliance as code turns annual self-reporting into continuous evidence, but only if the rules cover what the control actually promises. Start from the evidence your auditor asked for and where that data lives. Cover every path a change can take to production, with detective rules for the ones that bypass the pipeline. Keep exceptions in code with owners and expiry dates. And treat a green dashboard as evidence to be tested, the way an auditor will test it, rather than as proof.
Sources
AICPA, 2017 Trust Services Criteria for Security, Availability, Processing Integrity, Confidentiality, and Privacy (with revised points of focus, 2022).
ISO/IEC 27001:2022, Information security management systems — Requirements, Annex A. https://www.iso.org/standard/27001
45 CFR § 164.312, Technical safeguards (HIPAA Security Rule). https://www.law.cornell.edu/cfr/text/45/164.312
Microsoft, "Azure Blueprints retirement" and FAQ, Microsoft Learn: retirement announced 14 September 2023; phased retirement from 31 July 2026; full retirement 31 January 2027; migrate to deployment stacks and template specs. https://learn.microsoft.com/en-us/azure/governance/blueprints/blueprint-retirement-faq
NIST, "Open Security Controls Assessment Language (OSCAL)": machine-readable formats for control catalogues, system security plans and assessment results. https://csrc.nist.gov/Projects/open-security-controls-assessment-language
Open Policy Agent documentation: a graduated CNCF project; policies written in Rego. https://www.openpolicyagent.org/docs


