A Jump Start on Terraform Testing

A Jump Start on Terraform Testing

Within this article, I aim to quickly outline and inform you of some of the different forms of testing that can be implemented on your Terraform code.

Within this article, I aim to quickly outline and inform you of some of the different forms of testing that can be implemented on your Terraform code.

We will cover:

  • Static Testing/Analysis
  • Linters
  • Basic Security Testing
  • Unit Testing
  • Integration Testing
  • Property Testing
  • E2E Testing
  • Example code and links

Static Testing/Analysis

What is Static testing?

Static testing is a software testing technique by which we can check the defects in software without actually executing it. Often you can find small issues that you may have missed when eyeballing your code.

What are the benefits of static testing/analysis?

  • Fast.
  • Stable.
  • No need to deploy resources.
  • Very easy to use.

Hashicorp provides you with a way to perform static analysis with the command — terraform validate. This command will allow for a quick check to make sure you have not made a trivial mistake with spelling, missing a bracket, or what I believe to be more useful; checking for unused variables.

All you will need to do here is run your terraform validate command in your code directory.

_Note- _If you want to do something with your output text you can always format to JSON with the flag _`-json_`_ ._

What is a typical example of where this type of testing can help us?Duplication. To give an example:

I have followed a common Hashicorp standard of creating a_ proivider.tf _file to store my provider information:

provider "aws" {
access_key = var.AWS_ACCESS_KEY_ID
secret_key = var.AWS_SECRET_ACCESS_KEY
region = var.region
version = “~> 2.56”
}

However, I have also copied code that already has a provider block nested within it. Therefore, I now have two provider blocks within my Terraform working directory, one in my provider file and one in my main file. If I run a validate command, this becomes clear:

$terraform validate

Error: Duplicate provider configuration
on provider.tf line 1:
1: provider “aws” {
A default (non-aliased) provider configuration for "aws" was already given at main.tf:3,1–15\. If multiple configurations are required, set the "alias" argument for alternative configurations.

devops hashicorp-terraform testing terraform

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Extend your DevOps Strategy For Success in the Cloud?

DevOps and Cloud computing are joined at the hip, now that fact is well appreciated by the organizations that engaged in SaaS cloud and developed applications in the Cloud. During the COVID crisis period, most of the organizations have started using cloud computing services and implementing a cloud-first strategy to establish their remote operations. Similarly, the extended DevOps strategy will make the development process more agile with automated test cases.

What Is DevOps and Is Enterprise DevOps Any Good?

What is DevOps? How are organizations transitioning to DevOps? Is it possible for organizations to shift to enterprise DevOps? Read more to find out!

Testing Microservices Applications

The shift towards microservices and modular applications makes testing more important and more challenging at the same time. Learn more here.

DevOps Basics: What You Should Know

What is DevOps? What are the goals it helps achieves? What are its benefits? This article has answers!

Why You Should Be Adopting a DevOps Culture in 2020 - DZone DevOps

The year 2020 has arrived, and its arrival brings a lot of innovations and transformations in the Information and Technology (IT) sector to DevOps technologies.