Overview

Terraform Compliance is a static code analysis tool that tests Terraform plans against Behavior Driven Development (BDD) Features and Scenarios.  The  language is based on Gherkin.  However, terraform-compliance differs from Gherkin in that it interprets whole Gherkin phrases instead of just the "Given", "When", and "Then" statements.

This Proof of Concept (PoC) verifies the ability of Terraform Compliance to validate terraform code using a feature file.

Files

terraform-docker-demo-2.zip

terraform-docker-demo-2
(base) ralph@Alienware17r3L:~/Projects/terraform/terraform-docker-demo-2$ tree
.
├── docker.tf
├── features
│   └── ports_external.feature
└── versions.tf

1 directory, 4 files
FileDescription
docker.tfTerraform file to create wordpress and mysql interconnected microservices.
features/ports_external.featureTerraform Compliance feature file which validates that the Wordpress external port is set to 8081
versions.tfTerraform file for initializing the provider

Pre-requisite

  1. Install Terraform
  2. Install Python3
  3. Install pip3
  4. Install Terraform Compliance

Steps

  1. Extract the attached zip file (see Files section).
    This will create a terraform-docker-demo-2/ folder.
  2. Go to the working directory for this project

    Go to the working directory for this project
    cd terraform-docker-demo-2/
  3. Initialize Terraform

    Initialize Terraform
    terraform init
  4. Run a Terraform plan, saving the output file

    Run a Terraform plan, saving the output file.
    terraform plan -out wordpress.out
  5. Run terraform-compliance to validate that the Wordpress service has an external port set to 8081

    Run terraform-complance to validate Wordpress external port
    terraform-compliance --features features --planfile wordpress.out

Example Output of Failing Test

Example Output of Passing Test

In docker.tf, change the default port to 8081

Reference

  • No labels