hands-on lab

Testing Terraform Code with Terratest

Difficulty: Advanced
Duration: Up to 40 minutes
Students: 1,264
Rating: 4.5/5
Get guided in a real environmentPractice with a step-by-step scenario in a real, provisioned environment.
Learn and validateUse validations to check your solutions every step of the way.
See resultsTrack your knowledge and monitor your progress.

Description

Testing code is a common software development practice. Developers validate pieces of their code in an automated fashion to ensure it is working as expected. The new age of infrastructure development adopts this best practice to manage and maintain infrastructure code. Because of the fast-paced development of Cloud Service Providers, infrastructure code rots quickly. The same code used to deploy an EC2 instance has a good chance of not working in 6 months if not kept up to date. Writing tests for Terraform code validates modules are always working as expected. It also provides faster feedback allowing quick changes to modules with confidence that they won't break the infrastructure using them in production. Terratest is an open-source Golang library used for testing Terraform and is quickly rising in popularity.

In this lab, you will write a test for a Terraform module using the Terratest GO library.

Learning objectives

Upon completion of this lab, you will be able to:

  • Understand how to write a test for a Terraform module
  • Learn how test files work in Golang

Intended audience

This lab is intended for:

  • Individuals familiar with using Terraform and how to deploy resources
  • Individuals interested in learning how to use Terragrunt to increase Terraform's effectiveness
  • Enterprises looking to scale their Terraform codebase

Prerequisites

You should be familiar with:

  • Cloud Services
  • DevOps
  • Terraform

The following course and lab can be used to fulfill the prerequisites:

Updates

January 10th, 2024 - Lab updated to utilize CodeTour IDE

 

May 7th, 2023 - Updated theia to enable autosave

February 22nd, 2021 - Pin the Terratest version to avoid an issue in newer releases

Environment before

Environment after

Covered topics

Lab steps

Writing a Test with Terratest