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
(base) ralph@Alienware17r3L:~/Projects/terraform/terraform-docker-demo-2$ tree . ├── docker.tf ├── features │ └── ports_external.feature └── versions.tf 1 directory, 4 files
File | Description |
---|---|
docker.tf | Terraform file to create wordpress and mysql interconnected microservices. |
features/ports_external.feature | Terraform Compliance feature file which validates that the Wordpress external port is set to 8081 |
versions.tf | Terraform file for initializing the provider |
Pre-requisite
- Install Terraform
- Install Python3
- Install pip3
- Install Terraform Compliance
Steps
- Extract the attached zip file (see Files section).
This will create a terraform-docker-demo-2/ folder. Go to the working directory for this project
Go to the working directory for this projectcd terraform-docker-demo-2/
Initialize Terraform
Initialize Terraformterraform init
Run a Terraform plan, saving the output file
Run a Terraform plan, saving the output file.terraform plan -out wordpress.out
Run terraform-compliance to validate that the Wordpress service has an external port set to 8081
Run terraform-complance to validate Wordpress external portterraform-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