Home/Glossary/Policy as Code

Policy as Code

Defining and enforcing security, compliance, and operational policies through code that can be versioned, tested, and automated.

DevSecOpsAlso called: "pac", "guardrails", "policy automation"

Policy as Code transforms written policies into executable code, enabling automated enforcement across infrastructure and applications.

Policy as Code tools

  • Open Policy Agent (OPA): General-purpose policy engine, Rego language.
  • HashiCorp Sentinel: Terraform Enterprise policy framework.
  • AWS CloudFormation Guard: Validate CloudFormation templates.
  • Azure Policy: Built-in Azure governance.
  • Kyverno: Kubernetes-native policy engine.

Use cases

  • Infrastructure: Block public S3 buckets, require encryption.
  • Kubernetes: Enforce pod security, require resource limits.
  • CI/CD: Gate deployments on policy compliance.
  • Cost control: Limit instance sizes, require tags.
  • Compliance: Enforce CIS benchmarks, regulatory requirements.

Integration points

  • Pre-commit: Validate before code is committed.
  • CI/CD: Check during pull request and deployment.
  • Admission control: Enforce at Kubernetes API level.
  • Runtime: Continuous compliance monitoring.

Benefits

  • Consistent policy enforcement across environments.
  • Version control and peer review for policy changes.
  • Automated testing of policy logic.
  • Self-service within guardrails.
  • Audit trail of policy decisions.

Best practices

  1. Start with high-impact policies (security, cost).
  2. Provide clear violation messages with remediation guidance.
  3. Test policies against real configurations before enforcement.
  4. Implement exception workflows for legitimate edge cases.
  5. Version policies alongside infrastructure code.