Terragrunt - upgrade your Terraform code

Terraform vs Terragrunt. What is the relationship between these tools? Learn it in our article.

The absence of locking for Terraform state and the absence of a solution for configuration your Terraform state as code are the two issues that Terragrunt tackles at the beginning of its creation. Over the years new issues occurred. And now Terragrunt is smart enough to be integrated into Terraform itself and to keep your backend configuration and Terraform CLI arguments DRY, instead of WET. Don’t Repeat Yourself, instead of Write Everything Twice.

Terraform and Terragrunt logos

Terraform

Developed by Hashicorp, Terraform is an open-source tool aiming to build, change and manage infrastructure as code. Terraform facilitates the continuous support of any, service, infrastructure, or cloud. Terraform manages popular service providers like AWS Cloud, Google Cloud, Microsoft Azure, as well as custom solutions.

The key features of the Terraform are 

  • Infrastructure as a code. Among Terraform's major advantages is the way a network is represented in a form of code that allows the ideal condition to be conveyed in simple words that are interpreted and understood easily.
  • Execution plan. Such a strategy then directs people involved in the system and shows clearly what Terraform will do when it starts the process. Terraform avoids unexpected twists in such a manner, and everyone is aware of what is going on further.
  • Resource management. Terraform builds a graph of them all to manage resources effectively, and parallels the management of any non-dependent resources. What's also helpful is that thanks to extensible providers, Terraform is able to manage a wide range of resources: hardware, IaaS, PaaS, and SaaS.
  • Change automation. All changes can be done with minimal human intervention, which undoubtedly makes the entire process faster and less vulnerable to human error. Because of the execution plan and preparation, a client always knows what is going to happen, and what will happen.

Terraform code is presented in files with the extension ".tf" with a language called HCL. It is a declarative language, so its aim is to explain the infrastructure you want, and Terraform can work out how to build the infrastructure.

The biggest challenge with Terraform comes when you have several individuals operating a program who submit simultaneously, and you need to import and evaluate remote state files. Fortunately for us, there are other resources to support us with this challenge and to not be too difficult to implement. One such tool is Terragrunt.

TERRAGRUNT

Terragrunt is a thin Terraform wrapper. It offers additional powerful tools to those already present in Terraform. They are keeping your configuration DRY (Don’t Repeat Yourself), operating with multiple Terraform modules, and controlling remote state. Terragrunt is considered to be very helpful. Its built-in functions help us to customize the remote state, locking, additional points, etc. depending on your terraform.tfvars file configuration.

Benefits of Terragrunt

The basic concept is to describe all of your Terraform code only once in the repo modules, while you will only have terragrunt.hcl files in the live repo which provide a DRY mechanism to configure and deploy and module in each environment It translates to a live directory with far fewer files and code lines.

The most advantageous features of Terragrunt are:

  • Preserving the status file. Terraform writes the infrastructure state to a remote data store, which can then be exchanged with all team members and incorporate a lock mechanism to avoid improvements from being produced concurrently. With Terragrunt we can preserve the status file through, for example, the environment variables that use a parameterized path for the Terraform infrastructure state file. Using the same code, terraform.tfvars and Terraform environment parameters can determine different state files for each system.
  • Don’t Repeat Yourself (DRY). You'll usually handle several cloud applications, environments, and accounts. If you are using Terraform only, you have to operate with a folder tree. This very much concerns copying and pasting the same code lines. You can forget about those activities by using clean Terragrunt. Your code is now clear.
  • Automation. We built a Jenkins Pipeline utilizing points #1 and #2 to check the code and get a roadmap over the actual infrastructure.

You may wonder, but even implementing these minor improvements bring about the major importance to the everyday life of DevOps engineers.

Choose Quintagroup as a Terragrunt provider if you are interested in these two technologies, or want to implement your own idea using Terraform and Terragrunt.

Connect with our experts Let's talk