Infrastructure as Code Testing: How to Test Terraform Before It Hits Production

Infrastructure as Code Testing: How to Test Terraform Before It Hits Production

Infrastructure as Code (IaC) testing is the practice of validating infrastructure definitions — Terraform, Pulumi, CloudFormation — before they're applied to real environments. Without it, infrastructure changes are applied on faith, and failures mean provisioning errors, security misconfigurations, or cloud cost explosions.

This guide covers a practical IaC testing strategy for Terraform, from fast static analysis in CI to full integration testing in ephemeral environments.

Why Infrastructure Code Needs Testing

Application code bugs cause application failures. Infrastructure code bugs cause:

  • Security breaches: An S3 bucket marked acl = "public-read" exposes all files to the internet.
  • Cost explosions: Accidentally provisioning 100 EC2 instances instead of 1 generates thousands in unexpected cloud bills.
  • Outages: Deleting and recreating a database (Terraform's behavior for certain changes) instead of modifying it causes data loss.
  • Failed deployments: A missing IAM permission means the service can't start.

The blast radius of infrastructure bugs is larger than application bugs, and they're harder to roll back because infrastructure changes often involve data stores and network configurations.

The IaC Testing Pyramid

Like application testing, IaC testing has a pyramid:

            [ Integration Tests ]     — Provision real infra, verify it works
          [ Policy / Compliance Tests ]  — Validate against security policies
        [ Unit Tests ]                — Validate module logic without provisioning
      [ Static Analysis ]             — Syntax, formatting, secret detection
    [ Plan Validation ]               — `terraform plan` shows intended changes

Run everything bottom-up in CI. Fast tests run first; slow integration tests run last.

Layer 1: Static Analysis

Catch syntax errors, formatting issues, and obvious security problems instantly:

# .github/workflows/terraform-ci.yml
- name: Format check
  run: terraform fmt -check -recursive

- name: Validate syntax
  run: |
    terraform init -backend=false
    terraform validate

- name: Security scan with checkov
  uses: bridgecrewio/checkov-action@master
  with:
    directory: .
    framework: terraform
    soft_fail: false

Checkov catches common misconfigurations:

# checkov output example
Check: CKV_AWS_20: "Ensure the S3 bucket has access control list (ACL) is private"
FAILED for resource: aws_s3_bucket.data
File: /main.tf:12-18

12 | resource "aws_s3_bucket" "data" {
13 |   bucket = "my-company-data"
14 |   acl    = "public-read"  # ← This fails the check
15 | }

Other tools: tflint for provider-specific best practices, trufflehog or git-secrets for accidentally committed credentials.

Layer 2: Plan Validation

terraform plan shows what changes Terraform will make without applying them. Review the plan output in CI:

terraform init
terraform plan -out=tfplan -var-file=production.tfvars

# Parse the plan for dangerous changes
terraform show -json tfplan > plan.json

# Check for resource deletions (high risk)
cat plan.json | jq '[.resource_changes[] | select(.change.actions[] == "delete")] | length'

# Check for database changes (critical risk)
cat plan.json | jq '[.resource_changes[] | select(.type | contains("db_instance") or contains("rds"))] | .[] | {address, actions: .change.actions}'

Add a CI step that blocks the plan if it contains unexpected resource deletions:

DELETIONS=$(terraform show -json tfplan | \
  jq '[.resource_changes[] | select(.change.actions[] == "delete")] | length')

if [ "$DELETIONS" -gt "0" ]; then
  echo "FAIL: Plan includes $DELETIONS resource deletion(s) — manual review required"
  terraform show -json tfplan | \
    jq '.resource_changes[] | select(.change.actions[] == "delete") | .address'
  exit 1
fi

Layer 3: Policy as Code With OPA/Conftest

Open Policy Agent with Conftest lets you write policies for Terraform plans:

# policies/no_public_buckets.rego
package main

deny[msg] {
  r := input.resource_changes[_]
  r.type == "aws_s3_bucket"
  r.change.after.acl == "public-read"
  msg := sprintf("S3 bucket '%v' has public-read ACL — not allowed", [r.address])
}

deny[msg] {
  r := input.resource_changes[_]
  r.type == "aws_db_instance"
  r.change.after.publicly_accessible == true
  msg := sprintf("RDS instance '%v' is publicly accessible — not allowed", [r.address])
}

Run in CI:

terraform show -json tfplan > plan.json
conftest test plan.json --policy policies/

Conftest returns non-zero if any policy is violated, blocking the pipeline.

Layer 4: Terratest Integration Tests

Terratest (Go) provisions real infrastructure in a test environment, runs assertions, then tears it down:

// test/vpc_test.go
package test

import (
    "testing"
    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestVPCModule(t *testing.T) {
    t.Parallel()
    
    opts := &terraform.Options{
        TerraformDir: "../modules/vpc",
        Vars: map[string]interface{}{
            "environment": "test",
            "cidr_block":  "10.0.0.0/16",
        },
    }
    
    // Destroy at end of test
    defer terraform.Destroy(t, opts)
    
    // Apply the Terraform code
    terraform.InitAndApply(t, opts)
    
    // Verify outputs
    vpcId := terraform.Output(t, opts, "vpc_id")
    assert.NotEmpty(t, vpcId, "VPC ID should not be empty")
    
    privateSubnets := terraform.OutputList(t, opts, "private_subnet_ids")
    assert.Equal(t, 3, len(privateSubnets), "Should have 3 private subnets")
    
    // Verify VPC settings via AWS SDK
    vpc := aws.GetVpcById(t, vpcId, "us-east-1")
    assert.Equal(t, "10.0.0.0/16", aws.GetVpcById(t, vpcId, "us-east-1").CidrBlock)
}

Run integration tests:

cd test
go test -v -timeout 30m -run TestVPCModule

Terratest tests are slow (5–30 minutes) because they provision real cloud resources. Run them on pull requests to main, not on every commit. Use dedicated test cloud accounts to avoid impacting production.

Testing Infrastructure Changes Are Non-Destructive

One of the most important IaC tests: verify that a change to existing infrastructure is an in-place update, not a delete-and-recreate.

Certain Terraform resource attributes force replacement (destroy + create) rather than in-place update. These are marked as forces replacement in terraform plan. For databases, this means data loss.

Detect forced replacements in CI:

REPLACEMENTS=$(terraform show -json tfplan | \
  jq '[.resource_changes[] | select(.change.actions == ["delete", "create"])] | length')

if [ "$REPLACEMENTS" -gt "0" ]; then
  echo "WARNING: Plan includes $REPLACEMENTS resource replacement(s)"
  terraform show -json tfplan | \
    jq '.resource_changes[] | select(.change.actions == ["delete", "create"]) | {address, before: .change.before, after: .change.after}'
fi

If a replacement is detected for a database or stateful resource, the pipeline should require explicit human approval before proceeding.

Drift Detection

Terraform drift occurs when infrastructure changes outside of Terraform — manual changes in the AWS console, another tool modifying the same resource. Drift makes your Terraform state inconsistent with reality.

Detect drift in CI:

# terraform plan with -detailed-exitcode returns:
# 0 = no changes
# 1 = error
# 2 = changes exist (drift detected)

terraform plan -detailed-exitcode 2>&1
EXIT_CODE=$?

if [ $EXIT_CODE -eq 2 ]; then
  echo "DRIFT DETECTED: Infrastructure has changed outside Terraform"
  # Alert on-call or create a ticket
fi

Run drift detection on a schedule (daily) rather than on every commit.

CI/CD Pipeline Structure

# Full Terraform CI/CD pipeline
name: Terraform

on:
  pull_request:
    paths: ['infra/**']
  push:
    branches: [main]
    paths: ['infra/**']

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
      - run: terraform fmt -check -recursive
      - run: terraform init -backend=false && terraform validate
      - uses: bridgecrewio/checkov-action@master

  plan:
    needs: validate
    runs-on: ubuntu-latest
    steps:
      - run: terraform init && terraform plan -out=tfplan
      - run: ./scripts/check-plan-safety.sh  # Check for deletions/replacements
      - uses: actions/upload-artifact@v4
        with: {name: tfplan, path: tfplan}

  integration-test:
    needs: plan
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - run: cd test && go test -v -timeout 30m

  apply:
    needs: [plan, integration-test]
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production  # Requires approval in GitHub
    steps:
      - run: terraform apply tfplan

Summary

IaC testing follows the same principles as application testing — test early, test in layers, automate what you can. Static analysis catches security misconfigurations in seconds. Plan validation shows what changes will be made. Policy as code enforces compliance automatically. Integration tests verify the infrastructure actually works after provisioning.

Infrastructure changes have larger blast radii than application changes. A 30-minute integration test suite that catches a database deletion or a public S3 bucket pays for itself on the first failure it prevents.

Read more

Start now free