Terraform Policy Testing with OPA and Conftest: Enforce Standards Before Apply
Terraform compliance tools like Checkov and tfsec catch known vulnerability patterns. But they can't enforce your organization's specific standards: "all S3 buckets in the payments module must have KMS encryption with a specific key ARN", or "no EC2 instance type larger than t3.large in non-production", or "all resources must have a cost-center tag".
Open Policy Agent (OPA) with Conftest gives you a policy-as-code layer you control. Write policies in Rego, run them against your Terraform plan output, and fail the CI job before terraform apply touches your infrastructure.
The OPA + Conftest Approach
The workflow:
terraform plan -out=plan.tfplanterraform show -json plan.tfplan > plan.jsonconftest test plan.json --policy policies/- If any policy fails: block the pipeline
You're testing the plan — what Terraform intends to do — not the applied state. This means you catch violations before they're created.
Installing Conftest
# macOS
brew install conftest
# Linux
curl -L https://github.com/open-policy-agent/conftest/releases/download/v0.46.0/conftest_0.46.0_Linux_x86_64.tar.gz | tar xz
sudo mv conftest /usr/local/bin/
# Verify
conftest --versionUnderstanding Terraform Plan JSON Structure
The terraform show -json output has this structure:
{
"format_version": "1.1",
"resource_changes": [
{
"address": "aws_s3_bucket.payments",
"type": "aws_s3_bucket",
"change": {
"actions": ["create"],
"after": {
"bucket": "payments-prod",
"tags": {
"Environment": "production",
"CostCenter": "payments"
}
}
}
}
],
"configuration": { ... }
}Your Rego policies query this structure.
Writing Your First Policy
Enforce that all S3 buckets have a CostCenter tag:
# policies/s3_tags.rego
package main
import future.keywords.in
# Deny if S3 bucket missing CostCenter tag
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_s3_bucket"
resource.change.actions[_] == "create"
not resource.change.after.tags["CostCenter"]
msg := sprintf(
"S3 bucket '%s' must have a CostCenter tag",
[resource.address]
)
}Test it:
conftest test plan.json --policy policies/Output when a violation is found:
FAIL - plan.json - main - S3 bucket 'aws_s3_bucket.payments' must have a CostCenter tagWriting Security Policies
Block Public S3 Buckets
# policies/s3_security.rego
package main
# Block public S3 ACLs
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_s3_bucket_acl"
resource.change.actions[_] in ["create", "update"]
resource.change.after.acl in ["public-read", "public-read-write", "authenticated-read"]
msg := sprintf(
"S3 bucket ACL '%s' is set to '%s' — public buckets are not allowed",
[resource.address, resource.change.after.acl]
)
}
# Block deletion of S3 public access block
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_s3_bucket_public_access_block"
resource.change.actions[_] in ["create", "update"]
not resource.change.after.block_public_acls
msg := sprintf(
"S3 public access block '%s' must have block_public_acls = true",
[resource.address]
)
}Enforce Encryption
# policies/encryption.rego
package main
# All EBS volumes must be encrypted
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_ebs_volume"
resource.change.actions[_] == "create"
not resource.change.after.encrypted
msg := sprintf("EBS volume '%s' must be encrypted", [resource.address])
}
# RDS instances must have storage_encrypted
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_db_instance"
resource.change.actions[_] == "create"
not resource.change.after.storage_encrypted
msg := sprintf("RDS instance '%s' must have storage_encrypted = true", [resource.address])
}Instance Type Restrictions
# policies/instance_types.rego
package main
allowed_instance_types := {
"t3.micro", "t3.small", "t3.medium", "t3.large",
"m5.large", "m5.xlarge",
}
production_allowed_types := allowed_instance_types | {
"m5.2xlarge", "m5.4xlarge", "c5.xlarge", "c5.2xlarge",
}
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_instance"
resource.change.actions[_] == "create"
env := resource.change.after.tags["Environment"]
env != "production"
not resource.change.after.instance_type in allowed_instance_types
msg := sprintf(
"EC2 instance '%s' uses type '%s' which is not allowed in non-production (allowed: %v)",
[resource.address, resource.change.after.instance_type, allowed_instance_types]
)
}Writing OPA Unit Tests
Rego policies should be unit tested too:
# policies/s3_tags_test.rego
package main_test
import data.main.deny
# Test: bucket with tag should NOT be denied
test_bucket_with_cost_center_tag_passes {
not deny["S3 bucket 'aws_s3_bucket.payments' must have a CostCenter tag"] with input as {
"resource_changes": [{
"address": "aws_s3_bucket.payments",
"type": "aws_s3_bucket",
"change": {
"actions": ["create"],
"after": {
"bucket": "payments-prod",
"tags": {"CostCenter": "payments"}
}
}
}]
}
}
# Test: bucket without tag SHOULD be denied
test_bucket_without_cost_center_tag_fails {
count(deny) > 0 with input as {
"resource_changes": [{
"address": "aws_s3_bucket.payments",
"type": "aws_s3_bucket",
"change": {
"actions": ["create"],
"after": {
"bucket": "payments-prod",
"tags": {}
}
}
}]
}
}Run OPA unit tests:
opa test policies/ -vOrganizing Policies
policies/
s3/
tags.rego
tags_test.rego
security.rego
security_test.rego
ec2/
instance_types.rego
instance_types_test.rego
security_groups.rego
rds/
encryption.rego
backup.rego
tags/
required_tags.rego # Apply to all resourcesWarning vs. Deny
Conftest supports warn in addition to deny. Warnings show in output but don't fail the run:
# Non-blocking warning
warn[msg] {
resource := input.resource_changes[_]
resource.type == "aws_instance"
resource.change.after.instance_type == "t2.micro"
msg := sprintf(
"EC2 instance '%s' uses t2.micro — consider t3.micro for better performance",
[resource.address]
)
}CI Integration
# .github/workflows/terraform-policy.yml
name: Terraform Policy Check
on: [push, pull_request]
jobs:
policy-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with: { terraform_version: "1.7.0" }
- name: Setup OPA
run: |
curl -L -o opa https://openpolicyagent.org/downloads/v0.58.0/opa_linux_amd64_static
chmod +x opa && sudo mv opa /usr/local/bin/
- name: Setup Conftest
run: |
curl -L https://github.com/open-policy-agent/conftest/releases/download/v0.46.0/conftest_0.46.0_Linux_x86_64.tar.gz | tar xz
sudo mv conftest /usr/local/bin/
- name: Test OPA policies
run: opa test policies/ -v
- name: Terraform Init
run: terraform init -backend=false
working-directory: infrastructure/
env:
TF_VAR_environment: "test"
- name: Terraform Plan
run: terraform plan -out=plan.tfplan
working-directory: infrastructure/
env:
TF_VAR_environment: "test"
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Export Plan to JSON
run: terraform show -json plan.tfplan > plan.json
working-directory: infrastructure/
- name: Run Policy Checks
run: conftest test infrastructure/plan.json --policy policies/ --all-namespacesCombining with Checkov
OPA/Conftest handles your custom organization policies. Checkov handles known CVEs and security anti-patterns. Run both:
- name: Checkov (known vulnerabilities)
uses: bridgecrewio/checkov-action@master
with:
directory: infrastructure/
- name: OPA Conftest (org policies)
run: conftest test infrastructure/plan.json --policy policies/When to Use Each Tool
| Tool | Use for |
|---|---|
| Checkov / tfsec | Known security anti-patterns (public S3, unencrypted EBS) |
| OPA + Conftest | Custom org policies (tagging standards, instance sizes, approved regions) |
| terraform-compliance | BDD-style compliance tests (Given/When/Then) |
| Terratest | Integration testing (does the resource actually work?) |
Conclusion
OPA with Conftest gives you a programmable policy layer for Terraform that enforces your organization's standards before any infrastructure is created. Write policies in Rego, unit test them with opa test, and run them against terraform show -json output in CI. The result: a compliance gate that catches violations in pull requests, not production incidents.