# Policy as Code with OPA

> Master Policy as Code using Open Policy Agent (OPA) and Rego to enforce custom, organization-specific security rules across your infrastructure.

Canonical: https://codelivly.com/rooms/policy-as-code-with-opa
Last updated: 2026-10-29

| | |
|---|---|
| Level | Advanced |
| Category | Cybersecurity |
| Lessons | 5 |
| Estimated time | 30 min |
| XP | 100 |

This module transitions you from relying on predefined scanner rules to writing custom, dynamic security policies using Open Policy Agent (OPA) and its declarative query language, Rego. You will learn how to evaluate Terraform plans, Kubernetes manifests, and CI/CD pipeline configurations against strict organizational compliance rules. We will cover Rego syntax, policy testing with Conftest, integrating OPA into CI/CD pipelines for automated gating, and handling policy violations gracefully. The module culminates in a hands-on guided lab where you will author, test, and enforce a custom OPA policy to block non-compliant infrastructure deployments before they reach production.

## Lessons

1. What Is Policy as Code and Why Does It Matter (30 min)
2. Rego Language Basics (30 min)
3. Evaluating Terraform Plans with Conftest (30 min)
4. Enforcing Kubernetes Admission Policies with OPA Gatekeeper (30 min)
5. Guided Lab: Blocking a Non-Compliant Terraform Plan in CI (30 min)

## Related

- [Learning path: Infrastructure as Code Security](https://codelivly.com/learning-paths/infrastructure-as-code-security)
- [All rooms](https://codelivly.com/rooms)
