Unleashing the Power of Terraform: Best Practices and Project Setup

Sairam Krish
4 min readFeb 18, 2022

Mastering Terraform is an indispensable asset for DevOps professionals, particularly when dealing with cloud providers. In collaborative environments, the practice of manual cloud resource modifications, like those in AWS, often results in challenges related to tracking changes and understanding the underlying reasons.

In this comprehensive guide, we will delve into the following key topics:

  • Terraform Installation Using Docker: Streamlining your Terraform setup with Docker for increased efficiency.
  • Terraform Commands Cheat Sheet: A quick-reference guide to essential Terraform commands to boost your productivity.
  • Leveraging for_each in Terraform: Enhancing your Terraform scripts through the effective use of the for_each feature.
  • Harnessing the Power of jsonencode Function: Discover the advantages of utilizing the jsonencode function in your Terraform workflows.
  • Terraform Visualization Tools: Gain insights into visualizing your Terraform infrastructure, along with an evaluation of various tools.
  • Exploring Promising Terraform Libraries: A glance at promising Terraform libraries that can elevate your infrastructure automation.

Terraform Installation Using Docker

While Terraform provides native installations for macOS, Windows, and other platforms, employing Docker for your Terraform client offers distinct advantages:

  • Independence from XCode: On macOS, Terraform often depends on XCode, an update that can consume significant disk space and time.
  • Lightweight and Version Agnostic: Docker-based Terraform clients are lightweight, making it painless to switch between different Terraform versions as needed.

To set up Docker-based Terraform, consider using the HashiCorp Docker image — hashicorp/terraform

  • Here is a docker-compose based local setup, since we could have all environment params, arguments, command to invoke etc., in a easy to use form.
docker-compose.yaml file content

Terraform commands cheat sheet

Terraform workspaces

Terraform Workspaces are invaluable when dealing with distinct environments, such as development, quality assurance, and production. These workspaces allow you to maintain separate state files, ensuring better isolation and organization. The tfstate files are maintained in separate spaces.

# Create a new workspace
docker-compose run --rm terraform workspace new dev
# Switch to a workspace
docker-compose run --rm terraform workspace select dev

for_each block

for_each block is a powerful functionality in terraform. By using this, we can reduce the redundant terraform script and make the script more readable and maintainable.

resource "aws_secretsmanager_secret" "my_secrets" {
for_each = toset(["app_1", "app_2"])
name = format("%s/${each.key}", var.environment)
description = "Secrets used by apps"

The above resource will result in multiple secrets getting created based on the list that we provided.

Usage and reference of resources created with for_each example:

  • for_each resources are internally map type. We can address them with key name map['key_1'] and get any attribute of that object

Use jsonencode instead of EOF

There are different ways to add json content into the terraform resource property. If we use EOF kind of ways, I find it hard to debug the issue, since syntax errors and other issues doesn’t show up during `plan` phase. It only shows up during `apply` phase.

By using jsonencode, errors are catched during `plan` phase

Visualizing Terraform

Visualizing Terraform infrastructure can enhance understanding, but it’s not without its challenges. Here are some noteworthy options to consider:

  • Rover (1.6k GitHub Stars): A popular choice with an active community.
  • Terraboard: A web dashboard for inspecting Terraform states, providing a user-friendly interface. check here for more
  • Inframap: A tool for generating visual infrastructure diagrams.
  • Blast Radius: Interactive visualizations of Terraform dependency graphs. check here for more
  • Modules.ft: Transforms visual diagrams into infrastructure-as-code automatically. check here for more

Few lessons that I learnt:

  • docker-compose file shown above provides few options. This should help to play around easily
  • After playing around with few of them, I am not very happy with the results on a big terraform project. It was hard to get useful information for my needs.
  • For large terraform projects, generated diagrams are too big. Cannot read clearly
  • Normal terraform plan shows changes like git diff format, which is super useful. Most of the tools that played with, make viewing the changes more complex and hard to find the difference.
  • However I like the idea of visualizing terraform and this space is rapidly evolving.

Tools | Libraries

  • geopoiesis — Specialized continuous integration and deployment tool for modern declarative infrastructure provisioning and management. check here for more
  • terraforming — Export existing AWS resources to Terraform style (tf, tfstate). check here for more
  • terraformer — CLI tool to generate terraform files from existing infrastructure. check here for more
  • scenery — Terraform plan output prettifier. check here for more
  • terrahub — TerraHub is terraform automation and orchestration tool. Seamlessly integrated into console.terrahub.io, enterprise friendly GUI to show realtime terraform executions, as well as auditing and reporting capabilities for historical terraform runs. check here for more
  • Visual Studio Code Extension: Enhance your Terraform development experience with the HashiCorp Terraform extension.

In the ever-evolving field of Terraform, a commitment to best practices and staying informed about the latest tools and libraries is key to success. The journey of infrastructure as code automation is a rewarding one, and mastering Terraform is a significant milestone on that path.

For more in-depth resources, be sure to explore the following:

Happy Terraforming!