Prompt
how to do policy as code for cloud infrastructure
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
Policy as code for cloud infrastructure means expressing governance rules in code so they can be versioned, reviewed, tested, and automatically enforced.
What it looks like
Instead of manually checking things like:
- “No public S3 buckets”
- “All databases must be encrypted”
- “Resources must be tagged with owner and environment”
- “Only approved regions are allowed”
you define those rules in a policy language and run them against:
- Terraform plans
- Kubernetes manifests
- Cloud configs
- Live cloud resources
- CI/CD pipelines
Common approach
1) Define your policy goals
Start with a few clear categories:
- Security: encryption, public access, IAM restrictions
- Compliance: approved regions, retention, logging
- Cost: instance types, autoscaling limits
- Operations: tagging, naming conventions, required monitoring
2) Choose where to enforce
There are usually 3 enforcement points:
- Pre-deploy
- Validate Terraform, CloudFormation, Helm, Kubernetes YAML in CI
- Deploy-time
- Block bad changes in the pipeline or admission controller
- Post-deploy
- Continuously scan live infrastructure for drift and violations
3) Pick a policy engine
Popular tools by use case:
- Open Policy Agent (OPA) / Rego
- General-purpose policy engine
- Great for Kubernetes, CI/CD, Terraform
- HashiCorp Sentinel
- Common in Terraform Cloud/Enterprise
- Kyverno
- Kubernetes-native policy engine
- AWS Config / Azure Policy / GCP Organization Policy
- Cloud-provider-native controls
- Conftest
- Uses OPA/Rego to test config files locally or in CI
Example: Terraform policy with OPA/Rego
Policy: deny public S3 buckets
package terraform.security
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_s3_bucket_public_access_block"
resource.change.after.block_public_acls == false
msg := sprintf("S3 bucket public access block is disabled for %s", [resource.address])
}
Run it
Use Conftest or an OPA pipeline step against Terraform plan JSON.
Example: Kubernetes policy with Kyverno
Policy: require labels
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-labels
spec:
validationFailureAction: Enforce
rules:
- name: check-labels
match:
resources:
kinds:
- Pod
validate:
message: "app and env labels are required"
pattern:
metadata:
labels:
app: "?*"
env: "?*"
A good implementation pattern
Step 1: Start with “guardrails,” not everything
Begin with high-value, low-friction rules:
- No public storage
- Encryption required
- Required tags
- Approved regions
- Disallow risky IAM wildcard permissions
Step 2: Use “warn” mode first
Before blocking deployments:
- Log violations
- Show warnings in CI
- Give teams time to fix templates
Then switch to enforce.
Step 3: Keep policies small and modular
Organize by domain:
security/networking/compliance/tagging/
Step 4: Test policies like application code
Write tests for:
- Positive cases
- Negative cases
- Edge cases
Step 5: Version control and code review
Store policies in Git, and require PRs for changes.
Recommended workflow
- Developer writes Terraform/Kubernetes config
- CI generates plan/rendered manifests
- Policy engine evaluates them
- Build fails if policy is violated
- Approved deployment proceeds
- Cloud-native controls monitor live resources
Best practices
- Prefer deny by exception for sensitive environments
- Make policies understandable with clear error messages
- Avoid overly broad rules that block valid use cases
- Add documented exceptions and expiry dates
- Track policy coverage and false positives
- Use both static policy checks and cloud-native continuous enforcement
Typical tool stack
A practical stack might be:
- Terraform for infrastructure
- OPA/Conftest in CI
- Kyverno for Kubernetes admission control
- AWS Config / Azure Policy / GCP Policy for cloud-native checks
- GitHub Actions / GitLab CI / Jenkins to run checks
Simple example policy set
- Require tags:
owner,cost_center,environment - Deny
0.0.0.0/0on admin ports - Require encryption on storage and databases
- Allow only approved cloud regions
- Deny root account access keys
- Require logging/monitoring for production
If you want, I can give you:
- a starter architecture for policy as code in AWS/Azure/GCP, or
- a working example with Terraform + OPA + CI.