Auditors trust controls they can see working. When compliance requirements live only in policy documents and spreadsheets, proving them means screenshots and interviews. When they are expressed as code—tested in pull requests, enforced at deployment, monitored at runtime, and logged as evidence—the proof is built in. This guide shows how to turn written controls into Terraform checks, cloud guardrails, and continuous evidence.
From written control to evidence
- ControlWritten requirement with an owner
- Policy as codeRego, Sentinel, or scanner rules
- Pre-deploy checkFails the pull request on violations
- Cloud guardrailSCP / Azure Policy / org policy at runtime
- EvidenceResults, approvals, and exceptions retained
1. Start from controls, not tools
Take each technical control from your framework—SOC 2, ISO 27001, PCI DSS, HIPAA, or internal standards—and rewrite it as a testable statement about infrastructure. “Data is encrypted at rest” becomes “every storage bucket, database, and volume has encryption enabled with an approved key.” Each statement gets an owner, the frameworks it satisfies, and the checks that prove it. That mapping is what auditors will ask for, and it keeps the codebase from becoming a pile of disconnected rules.
2. Check Terraform plans before they are applied
Checking the plan, not just the source, catches violations from module defaults and computed values. Convert the plan to JSON and evaluate it with Open Policy Agent through Conftest, with HashiCorp Sentinel in Terraform Cloud or Enterprise, or with scanners such as Checkov that include hundreds of ready-made rules.
package main
import rego.v1
# CTRL-ENC-01: storage must be encrypted with a customer-managed KMS key
deny contains msg if {
some rc in input.resource_changes
rc.type == "aws_s3_bucket_server_side_encryption_configuration"
rule := rc.change.after.rule[_]
rule.apply_server_side_encryption_by_default[_].sse_algorithm != "aws:kms"
msg := sprintf("CTRL-ENC-01: %s must use aws:kms encryption", [rc.address])
}
# CTRL-TAG-02: every resource must carry owner and data_classification tags
deny contains msg if {
some rc in input.resource_changes
rc.change.actions[_] in {"create", "update"}
tags := object.get(rc.change.after, "tags", {})
is_object(tags)
some required in ["owner", "data_classification"]
not tags[required]
msg := sprintf("CTRL-TAG-02: %s is missing tag '%s'", [rc.address, required])
}terraform plan -out=tfplan
terraform show -json tfplan > plan.json
conftest test plan.json --policy policy/ --output githubIncluding the control ID in every failure message pays off twice: developers know which requirement they broke, and evidence reports can be grouped by control without extra work.
3. Test the policies themselves
Policies are code and can have bugs—a rule that silently never fires is worse than no rule, because it creates false confidence. Write unit tests with example inputs that should pass and fail, and run them in the policy repository's own CI.
package main
import rego.v1
test_unencrypted_bucket_denied if {
count(deny) > 0 with input as {"resource_changes": [{
"address": "aws_s3_bucket_server_side_encryption_configuration.logs",
"type": "aws_s3_bucket_server_side_encryption_configuration",
"change": {"actions": ["create"], "after": {
"tags": {"owner": "platform", "data_classification": "internal"},
"rule": [{"apply_server_side_encryption_by_default": [{"sse_algorithm": "AES256"}]}]
}}
}]}
}How does your cloud measure up?
Score your setup across foundation, delivery, reliability, security, and cost in 3 minutes—instant results, no email needed.
4. Enforce the same intent at runtime
Pre-deployment checks only see changes that go through the pipeline. Console changes, scripts, and other tools bypass them. Cloud-native guardrails close that gap: service control policies and AWS Config rules on AWS, Azure Policy on Azure, and organisation policies on Google Cloud. Express the same control in both places—preventive where possible, detective everywhere—so drift is caught regardless of how it happened.
5. Manage exceptions as code
Every real environment has justified exceptions. Store them in the repository with the control ID, resource, reason, approver, and expiry date, and have the policy engine read that list. Expired exceptions fail the build automatically. Auditors see a controlled process instead of unexplained gaps.
- control: CTRL-ENC-01
resource: aws_s3_bucket_server_side_encryption_configuration.legacy_exports
reason: "Legacy partner integration requires SSE-S3 until contract renewal"
approved_by: ciso
ticket: GRC-311
expires: 2026-12-316. Produce evidence automatically
- Store policy check results from every pipeline run with the commit, plan hash, and outcome.
- Keep pull request approvals as change-management evidence.
- Export runtime compliance results from cloud configuration tools on a schedule.
- Generate a monthly report per control: checks run, failures, fixes, and active exceptions.
- Write evidence to immutable storage so it cannot be edited after the fact.
7. Keep it maintainable
Put policies in their own repository with versioned releases that pipelines consume, so a policy change can be reviewed and rolled out like any other code. Start with a small set of high-value controls—encryption, public exposure, logging, tagging, identity—and grow based on audit findings and incidents. Hundreds of unowned rules quickly become noise that teams learn to bypass.
Choosing a policy engine
| Tool | Strengths | Consider when |
|---|---|---|
| Open Policy Agent / Conftest | General-purpose Rego, works with any JSON input, large ecosystem | You want one language for Terraform, Kubernetes, and APIs |
| HashiCorp Sentinel | Native to Terraform Cloud and Enterprise, enforcement levels built in | You already run Terraform through HCP Terraform or Enterprise |
| Checkov / Trivy misconfig | Hundreds of ready-made rules mapped to benchmarks | You want fast coverage before writing custom policies |
| Cloud-native policy (SCPs, Azure Policy, org policies) | Enforced by the provider regardless of tooling | Always, as the runtime backstop |
Most mature setups combine a scanner for broad baseline coverage with a small set of custom OPA or Sentinel policies for organisation-specific controls, plus cloud-native guardrails at runtime.
A worked example: one control end to end
Take CTRL-NET-03: “Databases must not be reachable from the internet.” In the pull request, a Conftest policy rejects any database instance with publicly_accessible set to true and any security group allowing 0.0.0.0/0 on database ports. At runtime, an AWS Config rule flags public RDS instances and a service control policy denies modifying instances to become public in production accounts. Each night, the Config results for the rule are exported to the evidence bucket. When an auditor asks how CTRL-NET-03 is enforced, you show the policy code, the blocked pull requests, the runtime rule history, and zero exceptions—or the approved, expiring ones.
Repeat that pattern for your top twenty controls and you will have covered most of what infrastructure auditors examine.
Bringing auditors along
Auditors are used to screenshots and samples, so walk them through the new approach before the audit window. Show the control register, the policy code for a few controls, a pull request that was blocked, the runtime rule history, and the evidence bucket. Most auditors welcome automated evidence because it is more complete than samples, but they need to understand how it is produced and protected. Agree early which reports they will accept, and keep the export format stable so evidence from different months is comparable.
Checklist
- Controls rewritten as testable statements with IDs, owners, and framework mappings.
- Terraform plans checked with OPA/Conftest, Sentinel, or Checkov in CI.
- Policy unit tests in the policy repository.
- Matching runtime guardrails and detective rules in each cloud.
- Exceptions stored as code with approver and expiry.
- Automated, immutable evidence grouped by control.
- Versioned policy releases and a small, high-value rule set.
Compliance as code turns audits from archaeology into reporting. Our Control & Evidence Ready module implements this pipeline for SOC 2, ISO 27001, HIPAA, PCI DSS, and DPDP-aligned programmes.
Key takeaways
- Encode approved patterns in modules; block known-bad configs at merge time.
- Map controls to module outputs, policy packs, and live config queries.
- Keep auditor-friendly narratives tied to technical evidence paths.
FAQ
OPA vs native cloud policy?
Use both where useful: OPA/Conftest for PR-time checks on Terraform plans; cloud-native policy for continuous runtime posture.
Does compliance as code replace audits?
No. It makes audits faster and more accurate by reducing drift between documents and reality.