Terragrunt vs Terraform: Battle of DevOps tools
Terraform is arguably one of the top tools used by DevOps professionals. However, there is another tool you may not have heard about called Terragrunt. What is Terragrunt? What about Terragrunt vs Terraform? Are they competing solutions?
Table of contents
What is Terraform?
Most likely, you have heard about Terraform. Terraform is an open-source infrastructure as code (IaC) tool that allows developers to create, modify, and manage multiple environments through simple text files.
It’s a powerful tool for infrastructure provisioning on different cloud platforms, and its wide adoption can be attributed to several key features that eliminate the need to use the same infrastructure manual steps that have been used for years.
Below, running the terraform command to see the command parameters.
Hashicorp develops Terraform and allows developers to define infrastructure using HCL files. These declarative syntax files provide an efficient way to manage cloud platforms, ensuring consistency across different environments.
With the support of multiple cloud providers, Terraform enables streamlined infrastructure provisioning and the ability to handle complex infrastructures. Note the following:
Declarative Syntax with HCL Files
Using HashiCorp Configuration Language (HCL), developers can write their infrastructure code in a human-readable format. The HCL files define infrastructure, giving clear visibility and control over the cloud infrastructure setup.
Modularity and Reusable Components
Terraform modules are building blocks that can be reused across different environments. These reusable modules reduce code duplication and enhance efficiency in managing complex infrastructures.
State Management
Terraform stores the state of the managed infrastructure in state files. These files enable synchronization and shared access, making collaboration among teams smoother.
Provider Ecosystem
Terraform supports many cloud providers, allowing developers to manage different cloud resources through the same code. This consistency facilitates smoother transitions between different configurations and cloud providers.
Scalable and Extensible
Terraform’s design enables the handling of both small and large-scale infrastructure deployments. With its extensible architecture, it can grow with your organization’s needs without manually configuring each new environment.
Terraform Cloud
Terraform Cloud is a hosted service provided by HashiCorp that offers collaboration, governance, and automation capabilities for Terraform users. It allows teams to work together on shared Terraform projects by managing and storing state files, providing a platform for remote operations, and enabling collaboration through access controls and workspace management.
With support for various cloud providers, Terraform Cloud simplifies infrastructure provisioning and enhances the process of managing complex infrastructures across different environments. Its integration with existing Terraform configurations means that developers can leverage the benefits of Terraform Cloud without significant changes to their existing infrastructure code, facilitating a more efficient and cohesive workflow.
Installing Terraform
Hashicorp has made installing Terraform very easy. You download the Terraform binary to your client and then add a PATH statement to your executable. That’s it. There isn’t really an “installation” process per se aside from downloading the binary and the PATH configuration.
You can visit the Terraform download page here: Install | Terraform | HashiCorp Developer.
What is Terragrunt?
Terragrunt is a thin wrapper around Terraform that provides additional functionality to enhance Terraform’s capabilities. It’s a tool designed to solve some common challenges faced in managing multiple environments and large-scale infrastructure management.
It introduces features like enabling collaboration, reducing code duplication, and providing additional commands like terragrunt apply and terragrunt plan. Its integration with Terraform makes handling multiple Terraform modules more manageable and less error-prone.
Remote State Management
One key feature that sets Terragrunt apart is its emphasis on remote state management. Terragrunt’s ability to manage remote state files across different versions and environments brings an additional control layer. With commands such as terragrunt apply, managing your entire infrastructure becomes more accessible.
DRY (Don’t Repeat Yourself) Principles
Terragrunt focuses on reducing code duplication by enabling the reusability of terraform code and configurations. It offers a way to manage multiple modules in Terraform without repeating the same code.
Dependency Management
Terragrunt offers features for managing dependencies between different modules, ensuring that infrastructure components are applied in the correct order.
Working with Multiple Environments
Managing multiple environments, whether a dev environment or a production setup, becomes more convenient with Terragrunt. It allows for defining input variables and configurations that can be easily adapted across different environments.
Integration with Terraform
Terraform natively supports Terragrunt and acts as a seamless extension of it. It translates Terragrunt-specific configurations into Terraform-compatible ones, providing a cohesive experience for infrastructure management.
Installing Terragrunt
Installing the Terragrunt executable is easy. Like terraform, you download the Terragrunt file and add a PATH statement. The Terragrunt download is an executable. Copy the file where you want to reference it, then add the PATH statement for the Terragrunt executable like you would for the Terraform binary.
Download the latest Terragrunt binary from here: Releases ยท gruntwork-io/terragrunt (github.com).
Below, I have added the Terragrunt executable to the same directory where I have Terraform located mainly to take advantage of the PATH statement I already have in place for Terraform.
After getting the terragrunt executable downloaded and ensuring the PATH statement is working, you should be able to execute terragrunt and see the output of the command parameters.
Creating the Terragrunt.hcl file
To get started with Terragrunt, you need to create a Terragrunt.hcl file that configures Terragrunt. Below is an example of code I grabbed directly from the Terragrunt “Quick Start” page:
# Indicate where to source the terraform module from.
# The URL used here is a shorthand for
# "tfr://registry.terraform.io/terraform-aws-modules/vpc/aws?version=3.5.0".
# Note the extra `/` after the protocol is required for the shorthand
# notation.
terraform {
source = "tfr:///terraform-aws-modules/vpc/aws?version=3.5.0"
}
# Indicate what region to deploy the resources into
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
region = "us-east-1"
}
EOF
}
Terragrunt uses the same syntax essentially as terraform:
terragrunt init
terragrunt plan
terragrunt apply
terragrunt output
terragrunt destroy
Below, I am running a terragrunt init.
To get started with Terragrunt, follow these steps:
- Install Terraform: Begin by installing Terraform, the core tool that Terragrunt complements.
- Install Terragrunt: Next, install Terragrunt, the thin wrapper that enhances Terraform’s functionality.
- Create a Terragrunt Configuration: Place your specific Terragrunt configuration within a
terragrunt.hcl
file.
Now, you can use Terragrunt by running the same commands you would typically use with Terraform, but replace “terraform” with “terragrunt”:
terragrunt plan
terragrunt apply
terragrunt output
terragrunt destroy
Terragrunt operates by forwarding nearly all commands, arguments, and options directly to Terraform. However, it customizes the behavior according to the configurations found in your terragrunt.hcl
file.
Comparing Terragrunt vs Terraform: Key Features and Differences
Managing Multiple Environments: Terragrunt’s Edge
While both tools offer robust solutions for handling multiple environments, Terragrunt specifically focuses on managing multiple environments and complex infrastructure. This gives it a unique advantage based on many opinions of those who have managed large environments with Terragrunt vs Terraform.
For most that may have a small, single-region infrastructure, or even multi-region infrastructure that is not very complex in nature, you may not see any benefits using Terragrunt vs Terraform.
As the complexity of your environment grows, and you are using more complex topologies where you need to segregate state files, Terragrunt will help organizations to simplify their structure time in managing their infrastructure as code.
Code Duplication and Complexity
Terragrunt helps to reduce the amount of code and the complexity of your environment and allows the use of separate state files and running these in parallel.
Dependency Management and Automated Testing
Both Terragrunt and Terraform provide essential features for dependency management and automated testing. These capabilities are critical in ensuring the stability and reliability of infrastructure deployments, whether running Terraform commands or utilizing Terragrunt’s specialized functions.
Latest versions of Terraform and DRY
Many have stated that the latest versions of Terraform have eliminated many of the issues that Terragrunt was solving vs those early releases of Terraform. It will be up to organizations to test and see if they gain benefits from using Terragrunt on top of Terraform.
Wrapping up
There are mixed opinions in the community from what I have seen on using native Terraform vs Terragrunt. The general consensus seems to be that Terragrunt becomes very beneficial when you have very complex environments across many different accounts.
However, if you are managing small Infrastructure as Code environment configurations, native Terraform will likely do everything you need without the added complexity of adding the Terragrunt wrapper around Terraform.