Policy as Code and Drift Detection for Infrastructure
Enforce infrastructure rules automatically with policy as code (OPA/Rego, Checkov, tfsec/Trivy, Kyverno).
Published December 4, 2026
Infrastructure as code lets you review changes, but humans miss things: a public S3 bucket, an unencrypted database, a security group open to the world, a missing cost tag. Policy as code turns those rules into automated checks that run on every plan. Drift detection covers the other gap: changes made outside code that make reality differ from what your repository says.
The concept
Policy tools evaluate configuration or plans against rules. Static scanners like Checkov and Trivy (which absorbed tfsec) check Terraform, Kubernetes manifests and Dockerfiles for hundreds of known misconfigurations out of the box. General policy engines like Open Policy Agent (OPA) with the Rego language, or Kyverno for Kubernetes, let you encode organisation-specific rules and enforce them in CI or at admission time.
Drift happens when someone changes infrastructure in a console, an emergency hotfix bypasses the pipeline, or another tool modifies shared resources. Running scheduled plans (terraform plan -detailed-exitcode) and alerting on differences, or using GitOps agents with self-healing for Kubernetes, surfaces drift so it can be reconciled or codified.
Policy in the pipeline
Policy tools
| Tool | Scope | Use for |
|---|---|---|
| Checkov | Terraform, CloudFormation, k8s, Dockerfiles | Built-in misconfiguration rules |
| Trivy (config) | IaC + images + k8s | Unified scanning in CI |
| OPA / Rego (conftest) | Any JSON/YAML, plans | Custom organisation policies |
| Kyverno / Gatekeeper | Kubernetes admission | Enforce at deploy time in clusters |
| Sentinel | HCP Terraform | Policies in Terraform Cloud workflows |
Detecting drift
Practical examples
Run Checkov on Terraform
Fails the build on violations.
checkov -d infra/ --framework terraform --compact --quietA custom OPA policy for Terraform plans
Require an owner tag on all S3 buckets.
package terraform.tags
deny[msg] {
rc := input.resource_changes[_]
rc.type == "aws_s3_bucket"
not rc.change.after.tags.owner
msg := sprintf("%s must have an 'owner' tag", [rc.address])
}
# terraform show -json tfplan > plan.json && conftest test plan.json -p policy/Drift check in a scheduled CI job
Exit code 2 means the plan has changes.
terraform plan -detailed-exitcode -lock=false -input=false > /dev/null; code=$?
if [ $code -eq 2 ]; then echo "Drift detected"; exit 1; fiCommon mistakes
- Running policy checks only on a schedule, after bad changes are already live.
- Hundreds of noisy findings nobody triages; start with high-severity rules.
- Fixing drift manually again in the console instead of reconciling with code.
- Policies without documented exceptions, so teams bypass them.
Key takeaways
- Policy as code automates infrastructure rules in CI and at admission.
- Use scanners for common issues and OPA/Kyverno for custom rules.
- Detect drift with scheduled plans or self-healing GitOps.
- Start small, document exceptions, and fix drift in code.
FAQ
Should policies block or warn?
Start with warnings to tune noise, then block on high-severity rules.
How do I allow justified exceptions?
Most tools support inline skip annotations with a reason; review them like code.
Can drift be prevented entirely?
Restricting console write access and routing all changes through pipelines reduces it dramatically.