1
 
 
Account
In your account you can view the status of your application, save incomplete applications and view current news and events
March 16, 2026

Terraform Tips:
S
ecuring Infrastructure-
as-Code 
with Terratest
– Part 3

What is this article about?

The third part of the Terraform series focuses on testing Terraform Infrastructure-as-Code (IaC).
At OTTO IT, many teams use Terraform to effectively orchestrate, automate and manage their
cloud infrastructure. As with normal application code, it is crucial not to neglect testing, especially
when it comes to Terraform modules. This ensures that deployments are stable and error-free. In
this article, you will learn the basics of testing with Terraform and get practical best practices for
your workflow.

Terraform Tips Series:

Why is testing IaC important?

Testing in the IaC context ensures that Terraform modules work as intended before they are
deployed in production. It helps to:

  •  Detect errors early on
  •  Improve code maintainability
  •  Increase the reliability of deployments

Automated testing prevents changes to the infrastructure from leading to unexpected problems – and thus to costly downtime.

How does testing Terraform differ from classic application testing?

While traditional testing focuses on business logic, testing infrastructure code often requires the
provision of real cloud resources. However, a complete end-to-end test is usually too time
consuming and costly. Pure unit tests, on the other hand, are not sufficient.

The solution: divide your architecture into modules and test them individually. This creates an
iterative, cost-efficient development approach that detects errors early on and ensures quality.

What tools are available for testing infrastructure-as-code?

There are many testing frameworks that can assist with testing infrastructure-as-code. In
addition to the Hashicorp variant, these include Terratest and Kitchen-Terraform.
In this article, we will focus on Terratest, a popular Go-based framework for automated testing of
Terraform modules.

Testing Terraform files – step by step

Determine your test environment

During testing, resources are created and deleted. To avoid accidental deletions, it is
recommended to use a separate development environment that is separate from Dev, Stage, and
Live.

Best practices:

  •  Each test should be independent and not use a shared state.
  •  Use separate service accounts for each test run.
  •  Example directory structure:
Terraform-Modulstruktur mit Terratest-Tests
Terraform-Modulstruktur mit Terratest-Tests

Start with "go test"

First, install Go and set up Terratest according to QuickStart. Then you can run your tests with the command "go test" and check whether the infrastructure is provisioned as expected.

Write your tests

In your test files, you define assertions that check the resources created – similar to manual
checks (e.g. "Is port 22 open?").

Typical assertions:

• Existence of certain resources (e.g., EC2 instance, security group)
• Properties of the resources (e.g., SSH access allowed)
• Validation of the module's outputs

Examples for AWS, Google, and Azure can be found here.

Run your tests

After writing the tests, they are executed using go test. The tool provides immediate feedback on
whether the infrastructure has been deployed correctly. Errors are displayed immediately, which
makes troubleshooting much easier.

Automate your tests with CI/CD

To make tests an integral part of your workflow, integrate them into your CI/CD pipeline – e.g.
with GitHub Actions.

Best practices for integrating Terratest into CI/CD:

  •  Use unique prefixes/suffixes for resources to decouple parallel test runs.
  •  Use a remote backend (GCP Storage or AWS S3) for the Terraform state.
  •  Set fixed test timeouts to avoid hanging jobs.
  •  Automate clean-up with "terraform destroy" to avoid unnecessary costs.

Here is a simple example of a GitHub Actions configuration.

Outlook

Testing is a key component of a reliable and scalable infrastructure-as-code strategy. Whether
you are just getting started with Terraform or want to optimise existing processes, automated
testing and its integration into CI/CD ensure the quality and stability of your infrastructure.

FAQ

What is Terratest?
Terratest is a Go-based framework for automated testing of Terraform modules and
infrastructure-as-code deployments.

How do you test Terraform modules?
Terraform modules can be tested with tools such as Terratest by deploying, verifying, and then
destroying infrastructure.

Why is Terraform testing important?
Automated testing detects errors early and prevents faulty infrastructure deployments.

Do You want to become
part of the team?

1 person likes it.

0No comments yet.

Write a comment
Answer to: Reply directly to the topic

Written by

Nina Braunger
Nina Braunger
Software Developer

Similar Articles

Saved!

We want to improve out content with your feedback.

How interesting is this blogpost?

We have received your feedback.