BlogPolicy & audit

Policy as Code with OPA: Practical Guide

Write Rego policies, validate Terraform plans, and enforce compliance before apply.

TL;DRBlog Key takeaways

Policy as Code with OPA: Practical Guide: Implement policy-as-code using Open Policy Agent: write Rego rules, test them, integrate with Terraform/OpenTofu plans, and gate enforcement in CI.

• Devopsify provides a tenant-scoped control plane with governed execution and audit.

• AI assistance is read-only and proposal-based; humans approve.

• Try the pattern in demo mode with zero credentials.

Devopsify is a tenant-scoped infrastructure control plane that unifies multi-cloud inventory, topology, governed provisioning, delivery operations, audit, and AI-assisted investigation under one declarative graph. Write Rego policies, validate Terraform plans, and enforce compliance before apply. This post examines the practical steps, trade-offs, and operational signals that make the pattern reviewable and auditable, from inventory discovery to policy evaluation and deployment waves.

What is policy as code?

Policy as code means defining governance rules in a machine-readable format that can be evaluated automatically against infrastructure changes. Instead of manual security reviews or tribal knowledge about what's allowed, policies are written as code, version-controlled, tested, and enforced at the right point in the CI/CD pipeline. Open Policy Agent (OPA) is the most widely adopted engine for this, using the Rego language to express policies as declarative rules.

Writing your first Rego policy

Start simple. A basic policy might check that all AWS S3 buckets have encryption enabled. In Rego:

  • package main
  • deny[msg] {
  • input.resource_type == "aws_s3_bucket"
  • not input.server_side_encryption_configuration
  • msg := sprintf("Bucket %v must have encryption enabled", [input.bucket_name])
  • }

This policy reads resource data from input, checks whether encryption configuration exists, and produces a denial message if it doesn't. The key insight is that Rego policies operate on structured data: they don't parse YAML or JSON text; they work with already-parsed objects.

Testing policies before deployment

OPA includes `opa test` for unit testing policies. Write test cases that cover both passing and failing scenarios. For example, test that a bucket with encryption passes the policy, while one without encryption fails. Use `opa eval` to manually test policies against sample input data during development. Always version-control your policies alongside your infrastructure code.

Policy evaluation flow
Terraform plan ──▶ policy-as-code evaluator              ┌──────┬────────┼────────┐              │ Pass │ Fail   │ Warn   │              └──────┴────────┴────────┘              allow apply     deny/block    log-only

Integrating with Terraform/OpenTofu

The most common integration point is evaluating policies against Terraform plans before apply. Tools like Conftest, Sentinel, or Devopsify's built-in policy engine can read plan output (JSON format) and evaluate Rego policies against it. The workflow: generate plan → convert to JSON → run OPA evaluation → block or warn based on results. This catches violations before they reach production.

Implementation checklist

  • Define policy requirements with engineering and security teams
  • Write Rego policies covering each requirement
  • Test policies with `opa test` using passing and failing inputs
  • Integrate policy evaluation into CI pipeline (before plan approval)
  • Configure enforcement mode (block/warn/log) per policy severity
  • Document policies and maintain as living documentation
AspectWithout DevopsifyWith Devopsify
InventorySiloed consoles✓ Unified graph
PolicyManual review✓ Pre-apply gate
AuditScreenshots✓ Per-change trail

How does this pattern fit your operating model?

  1. Connect read-first via SDK adapters or on-prem agents.
  2. Discover drift and topology on schedule.
  3. Govern attach policy and approvals.
  4. Operate propose with AI, approve as human, execute with audit.

Common Questions

How does Devopsify ensure the pattern is auditable?

Every proposed change carries its inventory snapshot, policy result, required approvals, and execution result as one traceable record: no gaps, no screenshots.

Can I try this without credentials?

Yes. Demo mode uses labeled mock data. Walk the same inventory, policy, and AI investigation flows with zero cloud credentials.

Does AI execute changes?

No. AI investigates and proposes; humans approve and policy gates enforce. Execution is platform-only and fully audited.

Cover photo via Openverse under a Creative Commons license. Illustrative imagery only.

THE NEXT STEP

This is a pattern, not a promise.

Every story here is an illustrative implementation pattern. To verify one against your own estate, start in demo mode (zero credentials) or request guided access.