This repository is an example of testing IaC libraries written with Terraform using GitHub Actions. For a detailed run-through, see the see accompanying blog post Automate Terraform Testing with GitHub Actions and Terratest.
To successfully run all the examples, you need the following tools:
- Repository in GitHub with the ability to run GitHub Actions
- Terraform, v0.12.20+, a tool for building, changing, and versioning infrastructure safely and efficiently.
- Terratest, a Go library that provides patterns and helper functions for testing infrastructure. Will be installed as a Go Library.
- Go, v1.13+ binary in your PATH.
- AWS Account and credentials for Terraform
Note that to run the tests, you will have to have AWS Credentials available for Terraform.
As the unit tests don't create any resources, they're safe to run as is. Check out the terratest unit test file for details.
To run the unit tests, go to the test
directory and run go test -v -tags=unit
.
Integration test creates actual resources in AWS, so you will first have to set up your AWS Credentials.
Once that's done, you can run go test -v -tags=integration
. Check out the terratest integration test file
for details.
Go test output can sometimes be hard to read. A good tool to get human readable output is gotestsum
.
Instead of the very verbose output, you get something like this:
❯ gotestsum --format short-verbose -- -tags=unit
PASS TestStaticSiteValidity (12.63s)
PASS .
DONE 1 tests in 15.416s
- https://www.terraform.io/docs/modules/index.html
- https://www.terraform.io/docs/modules/composition.html
- https://blog.gruntwork.io/how-to-create-reusable-infrastructure-with-terraform-modules-25526d65f73d
- https://blog.gruntwork.io/5-lessons-learned-from-writing-over-300-000-lines-of-infrastructure-code-36ba7fadeac1