A comprehensive collection of policy-as-code examples for integrating Open Policy Agent (OPA) with Terraform. This project helps you enforce security and compliance standards, automate policy checks, and ensure best practices in your cloud infrastructure deployments.
- 🛡️ Security Controls: Prevent misconfigured security groups and enforce encryption
- 🏷️ Resource Tagging: Ensure consistent tagging across all resources
- 📊 Blast Radius Control: Limit the scope of infrastructure changes
- ⏰ Deployment Scheduling: Restrict deployments during critical periods
- 🔄 Drift Detection: Identify unauthorized infrastructure changes
- 🌐 Region Restrictions: Control where resources can be deployed
- Terraform (v1.0.0 or later)
- Open Policy Agent (latest version)
- AWS CLI (configured with appropriate credentials)
-
Clone the repository:
git clone https://github.com/yourusername/opa-terraform-policies.git
-
Navigate to the project directory:
cd opa-terraform-policies
-
Initialize Terraform:
terraform init
-
Generate a Terraform plan:
terraform plan --out tfplan.binary
-
Convert the plan to JSON:
terraform show -json tfplan.binary > tfplan.json
-
Evaluate policies:
# Check Authorization Status opa exec --decision terraform/analysis/authz --bundle policy/ tfplan.json # Check Score (Blast Radius) opa exec --decision terraform/blast_radius/score --bundle policy/ tfplan.json # Check Authorization Reason opa exec --decision terraform/analysis/authz_reason --bundle policy/ tfplan.json # Check Drift Detection opa exec --decision terraform/drift/detect_drift --bundle policy/ tfplan.json
Policy | Description | Status |
---|---|---|
Blast Radius | Limits the scope of changes | ✅ Working |
Resource Tags | Enforces required tags | ✅ Working |
Region Restriction | Limits resource creation to specific regions | ✅ Working |
Deployment Schedule | Prevents deployments on restricted days | ✅ Working |
S3 Encryption | Ensures S3 buckets are encrypted | ✅ Working |
Security Groups | Prevents overly permissive rules | ✅ Working |
Tag Bypass | Allows exceptions with specific tags | ✅ Working |
# Prevent security groups with wide-open access
open_security_group_rules {
sg := input.resource_changes[_]
sg.type == "aws_security_group"
rule := sg.change.after.ingress[_]
rule.cidr_blocks[_] == "0.0.0.0/0"
rule.from_port <= 22
rule.to_port >= 22
}
# No deployments on weekends or Fridays
restricted_days := ["Friday", "Saturday", "Sunday"]
is_restricted_day {
day := time.weekday(time.now_ns())
restricted_days[_] == day
}
We welcome contributions! Please follow these steps:
- Fork the repository
- Create your feature branch:
git checkout -b feature/AmazingFeature
- Commit your changes:
git commit -m 'Add some AmazingFeature'
- Push to the branch:
git push origin feature/AmazingFeature
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Open Policy Agent Documentation
- Terraform AWS Provider
- The amazing DevOps community
⭐ Found this project useful? Please star it on GitHub!