Prompt
I need policy as code for infrastructure provisioning approvals
Latest observation
Below is a practical policy-as-code pattern for infrastructure provisioning approvals. I’ll give you a production-friendly example using OPA/Rego for Terraform/CI pipelines, plus an alternative if you’re using AWS Organizations / SCPs, Azure Policy, or Kubernetes admission control.
Goal
Enforce rules like:
- High-cost or production infrastructure requires approval
- Restricted services require security review
- Public exposure requires explicit exception
- Certain regions/accounts are blocked
- Changes above a threshold need a change ticket / approver
1) Recommended architecture
A common flow is:
- Developer submits Terraform plan or IaC change
- CI pipeline generates an artifact:
- Terraform plan JSON
- CloudFormation change set
- Pulumi preview
- Policy engine evaluates it
- If policy passes:
- auto-apply, or
- route to approval gate based on risk
- If policy fails:
- block deploy or require exception workflow
Typical components:
- OPA / Conftest for Terraform and generic IaC
- Sentinel if you use HashiCorp products
- Azure Policy / AWS SCP / GCP Org Policy for cloud control plane guardrails
- GitHub Actions / GitLab / Jenkins for approval workflows
2) Example policy model
Define inputs like:
{
"environment": "prod",
"cost_estimate_monthly": 1200,
"resources": [
{
"type": "aws_instance",
"name": "app",
"public_ip": true
}
],
"requested_by": "alice",
"approvals": [
"bob"
]
}
Policy rules could be:
prodrequires at least 2 approvals- any public IP requires security approval
- cost above
$1000/monthrequires manager approval - certain resource types require platform approval
3) OPA/Rego example
policy.rego
package infra.approval
default allow = false
# Main allow rule
allow {
not deny_reason[_]
approved
}
# Require approvals based on environment and risk
approved {
input.environment != "prod"
}
approved {
input.environment == "prod"
count(input.approvals) >= 2
}
# Deny if any blocked region is used
deny_reason[msg] {
input.region == "cn-north-1"
msg := "Region cn-north-1 is not allowed"
}
# Deny if public exposure exists without security approval
deny_reason[msg] {
some r
r := input.resources[_]
r.public_ip == true
not contains(input.approvals, "security")
msg := sprintf("Resource %s exposes public IP and needs security approval", [r.name])
}
# Deny if monthly cost exceeds threshold without manager approval
deny_reason[msg] {
input.cost_estimate_monthly > 1000
not contains(input.approvals, "manager")
msg := sprintf("Estimated monthly cost %v exceeds threshold and needs manager approval", [input.cost_estimate_monthly])
}
# Deny prod changes without platform approval for specific resource types
deny_reason[msg] {
input.environment == "prod"
some r
r := input.resources[_]
r.type == "aws_db_instance"
not contains(input.approvals, "platform")
msg := sprintf("Production database %s requires platform approval", [r.name])
}
Sample input
{
"environment": "prod",
"region": "us-east-1",
"cost_estimate_monthly": 1500,
"resources": [
{
"type": "aws_instance",
"name": "web",
"public_ip": true
},
{
"type": "aws_db_instance",
"name": "db",
"public_ip": false
}
],
"requested_by": "alice",
"approvals": ["security"]
}
Expected result
This should fail because:
prodneeds 2 approvals- cost over threshold needs manager approval
- DB in prod needs platform approval
4) Conftest example for Terraform plan JSON
If you use Terraform, you can evaluate the plan output.
Generate plan JSON
terraform plan -out tfplan
terraform show -json tfplan > plan.json
conftest test plan.json -p policy/
You can write policies that inspect Terraform resource changes.
Example policy/terraform.rego
package terraform.approval
default allow = false
deny[msg] {
some rc
rc := input.resource_changes[_]
rc.type == "aws_security_group"
some change
change := rc.change.after
change.ingress[_].cidr_blocks[_] == "0.0.0.0/0"
msg := sprintf("Security group %s allows public ingress", [rc.name])
}
deny[msg] {
some rc
rc := input.resource_changes[_]
rc.type == "aws_instance"
rc.change.after.associate_public_ip_address == true
msg := sprintf("Instance %s has public IP enabled", [rc.name])
}
5) Approval workflow in CI
A simple GitHub Actions gate:
name: infra-approval
on:
pull_request:
jobs:
policy-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup OPA
run: |
curl -L -o opa https://openpolicyagent.org/downloads/latest/opa_linux_amd64_static
chmod +x opa
sudo mv opa /usr/local/bin/
- name: Terraform plan
run: |
terraform init
terraform plan -out tfplan
terraform show -json tfplan > plan.json
- name: Evaluate policy
run: |
conftest test plan.json -p policy/
To implement manual approval, add a protected environment in GitHub Actions:
jobs:
apply:
needs: policy-check
environment: production
runs-on: ubuntu-latest
steps:
- run: terraform apply -auto-approve tfplan
This way, production requires human approval before apply.
6) Pattern for approval-based policy
If you want approvals to be explicit, model them as data:
{
"change_id": "CHG-12345",
"environment": "prod",
"risk": "high",
"approvals": [
{
"role": "security",
"approved_by": "bob"
},
{
"role": "manager",
"approved_by": "carol"
}
]
}
Then enforce:
- high risk → security + manager
- prod → platform + manager
- network exposure → security
Rego for role-based approvals
package infra.approval
default allow = false
allow {
not deny_reason[_]
}
deny_reason[msg] {
input.risk == "high"
not has_approval("security")
msg := "High risk changes require security approval"
}
deny_reason[msg] {
input.environment == "prod"
not has_approval("manager")
msg := "Production changes require manager approval"
}
deny_reason[msg] {
exposes_public_network
not has_approval("network")
msg := "Public network exposure requires network approval"
}
has_approval(role) {
some a
a := input.approvals[_]
a.role == role
}
exposes_public_network {
some r
r := input.resources[_]
r.public_ip == true
}
7) Best practices
- Use deny-by-default
- Keep policy data-driven:
- thresholds in config
- approver roles in policy
- Separate:
- guardrails (never allowed)
- approval rules (allowed with review)
- Log all decisions
- Version policies like application code
- Test policies with unit tests
- Maintain an exception process with expiry dates
8) What to use based on your stack
If you use Terraform
- OPA + Conftest for flexible policy-as-code
- Sentinel if you are on Terraform Cloud/Enterprise
If you use AWS
- SCPs for hard organizational restrictions
- Config + EventBridge + Lambda for detection and remediation
- OPA in CI for pre-deploy approvals
If you use Azure
- Azure Policy for resource restrictions
- GitHub Actions / Azure DevOps approvals for change gates
If you use Kubernetes
- Kyverno or OPA Gatekeeper
- Admission policies for namespace, resource limits, privileged pods, etc.
9) If you want a ready-made approval workflow
A common enterprise setup is:
- PR opens
- CI runs policy checks
- Policy engine determines required approvers
- GitHub/GitLab required reviewers are auto-assigned
- Merge blocked until approvals collected
- Apply happens only after merge + environment approval
If you want, I can provide one of these next:
- a complete OPA policy pack for Terraform approvals
- a GitHub Actions workflow that enforces approvals
- a Sentinel policy version
- a Kyverno/Gatekeeper policy for Kubernetes
- a generic approval matrix template for infra changes