Terraform Compliance

1.14.1 · active · verified Thu Apr 16

Terraform-compliance is a lightweight, security and compliance-focused BDD (Behavior Driven Development) test framework against Terraform. It enables negative testing capability for infrastructure-as-code, allowing teams to define and enforce policies against Terraform plans before deployment. It is an open-source tool that can be installed via pip or run via Docker. The library is currently at version 1.14.1 and sees frequent updates, often tied to supporting new Terraform CLI versions.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates how to use `terraform-compliance` by defining a simple Terraform resource, generating a plan, converting it to JSON, writing a BDD feature file for compliance, and then running the compliance checks. Ensure you have Terraform CLI installed and your feature files adhere to Gherkin syntax.

# 1. Create a Terraform configuration (e.g., main.tf)
# resource "aws_s3_bucket" "example" {
#   bucket = "my-unique-example-bucket-12345"
#   tags = {
#     Environment = "dev"
#     ManagedBy = "terraform-compliance-test"
#   }
# }

# 2. Initialize Terraform and generate a plan
# terraform init
# terraform plan -out=myplan.out

# 3. Convert the binary plan to JSON format
# terraform show -json myplan.out > myplan.json

# 4. Create a compliance feature file (e.g., features/s3_tags.feature)
# Feature: S3 Bucket Tagging
#   Scenario: S3 buckets must have 'Environment' and 'ManagedBy' tags
#     Given I have an S3 bucket
#     Then it must contain tags
#     And its 'Environment' tag must be defined
#     And its 'ManagedBy' tag must be defined

# 5. Run terraform-compliance against the plan and features
# terraform-compliance -p myplan.json -f features/

view raw JSON →