Terramate vs Terragrunt: Why Terramate Wins in Code Generation and Orchestration

Terramate vs Terragrunt: Why Terramate Wins in Code Generation and Orchestration

Overview

Generally writing Terraform configuration files is easy if you have a small codebase or a small infrastructure to work upon, but as soon as your infrastructure and codebase size increases it becomes difficult to maintain all the things, sometimes this leads to error in the infrastructure due to minor changes made by a developer on only one environment, now to solve this there’s a lot of tools out in the market, such as Terramate and Terragrunt that helps you to keep your code DRY(Don’t Repeat Yourself) and understandable.

In this blog, we will learn about Terramate and Terragrunt and how they work, We will also discuss some key features. At the end of the blog, we will discuss which tool is best to keep your codebase DRY(Don’t Repeat Yourself), but what does DRY means in Infrastructure code.

The DRY principle promotes code reuse and modularization. By following the DRY principle, developers can reduce code duplication, enhance code readability, and improve overall software quality. Terramate is a tool that can help you maintain the DRY principle in your Terraform code

Suppose you want to create infrastructure for three different environments, for that you don’t need to write the same files for the different environments instead you can create stacks using terramate create <stack_name> commands and write the code in stack.tm.hcl” file with all the resources block, copy same code to other stack, and hit terramate generate command. It will generate all the Terraform files for the infra. Now hit terramate run terraform init and terramate run terraform apply to create the infra for all three environments.

We will cover:

  1. What is Terramate and Terragrunt

  2. Basic commands of Terramate and Terragrunt

  3. Comparison between Terramate and Terragrunt

  4. Key points

Terramate

Terramate is a code generator and orchestrator for Terraform that adds powerful capabilities such as code generation, stacks, orchestration, change detection, globals, and more to Terraform.

  • Basic Terramate Commands

Instead of moving into each environment and running Terraform commands in each environment, you can run the Terramate commands directly, let’s look at a few of the top Terramate commands which are super helpful.

  • terramate create -> to create a stack

  • terramate generate -> to run the Terramate code generation

  • terramate list --changed -- -> to show a list of changed stack

  • terramate run <command> -> to run a particular command in each stack.

    Examples:

  • terramate run terraform init -> to run terraform init in each stack

  • terramate run terraform plan -> to run terraform plan in each stack

  • terramate run terraform apply -> to run terraform apply in each stack

  • terramate run terraform destroy -> to run terraform destroy in each stack

Terragrunt

Terragrunt is a thin wrapper for Terraform that provides extra tools for keeping your Terraform configurations DRY (Don't Repeat Yourself). With Terragrunt, you can easily manage remote states and multiple environments. It also helps you keep your codebase clean and organized.

  • Basic Terragrunt Commands

Instead of running Terraform commands directly, you run the same commands with Terragrunt:

  • terragrunt init -> equivalent to terraform init

  • terragrunt plan -> equivalent to terraform plan

  • terragrunt apply -> equivalent to terraform apply

  • terragrunt output -> equivalent to terraform output

  • terragrunt destroy -> equivalent to terraform destroy

  • terragrunt run-all init -> to run terraform init in all environment

  • terragrunt run-all plan -> to run terraform plan in all environment

  • terragrunt run-all apply -> to run terraform apply in all environment

  • terragrunt run-all destroy -> to run terraform destroy in all environment

These commands will call the corresponding Terraform commands, with Terragrunt performing additional logic before and after the Terraform calls.

Feature Comparision between Terramate and Terragrunt

Terramate and Terragrunt both are software tools that are integrated with Terraform in order to help you keep your codebase DRY. They help in running Terraform code in multiple environments at once and help to avoid manually writing the code in each environment/stack. Terramate is also capable of working with OpenTofu as well. For example: if my requirement is to create 10 stacks we can do that easily using the terramate commands.

Below comparison will give us quick glance at the features of both tools.

  1. Ease of Use

Using Terramate, if you want to make 3 environments or stack you first need to create a stack and put it inside the module directory, write only generate block ( define what you want to generate using Terramate for Terraform) which will be used by stacks to generate the code. Now use “terramate create <env_ name>” command to create the stacks, append the import block (taking module files as source) in stack.tm.hcl file in each stack, it will generate the terraform code using terramate.

Below is how you initiate in Terramate:

In Terragrunt, if you want to make a new environment (which is equivalent to a stack in Terramate) you need to do it manually and add a configuration file in each environment as “terragrunt.hcl” file name with the following code in it. Otherwise, Terragrunt won’t consider that environment.

So, in terms of ease, Terramate has got the lead here, because in Terramate you don’t need to manually write anything for the environment to be considered by the Terramate.

  1. Syntax Highlighting

  1. In the case of Terramate, this is how you write its code generation syntax inside the stack.tm.hcl file, like in below example we are generating terraform code for S3 bucket in AWS.
generate_hcl "main.tf" {
  content {
    resource "aws_s3_bucket" "Infrasity" {
      acl = "private"
      tags = {
        name = "S3"
      }
      versioning {
        enabled = true
      }
    }
  }
}

The syntax of what you want to generate looks much better when you use some of the IDE such as VS Code or IntelliJ IDEA.

  1. In the case of Terragrunt, this is how you write its code generation syntax inside the terragrunt.hcl file:
generate "storage"{
  path = "s3.tf"
  if_exists = "overwrite_terragrunt"
  contents = <<EOF
  resource "aws_s3_bucket" "Infrasity" {
  acl = "private"
  tags = {
    name = "S3"
    acl  = "private bucket"
  }
  versioning {
    enabled = true
  }
}
  EOF
}

Here syntax looks a little boring and the comment type look of what you are generating.

So, in terms of syntax highlighting, Terramate has got the lead here.

  1. Change Detection

Working with a Git-based environment you can check for what infrastructure is changed and apply your Terraform commands only to what changes are made on your infrastructure.

Terrmate offers change detection, by typing a simple command it detects all the changes made on the infrastructure and applies all desired Terraform commands only on changes.

For all the changes that were made on the environment, use this command in Terramate:

  • terramate list --changed -- “ this command will list all the changes

  • terramate run --changed -- terraform plan” this command will plan whatever on the bases of changes that were made on the infrastructure.

  • terraform run --changed -- terraform apply” this command will apply new changes to the infrastructure.

In the case of Terragrunt, we don’t see anything equivalent to this feature of Terramate, so Terramate has got lead here as it provides change detection.

  1. Uniform Code

In the case of Terramate, whenever we make a new stack we don’t need to do anything explicitly to involve it in the environment we made earlier to generate code that each stack already has.

  • For example, we made 3 environments/stacks that are Development, Production, and Staging and generated some terraform configuration from the globals definition for each stack. Later we thought to make 1 more stack named Testing and this must also include each of the configuration files that other stacks have.

  • Here we simply make a new stack and the configuration files will get auto-generated in this new stack as well along with the other stack, which means you run terramate once and it updates code uniformly at once in all the respective stack, please see the example below:

    • Generated all the codes for each stack (Development, Production, Staging)

  • Now on creating a new stack Testing, it will have all the same code in it as other stacks have.

Whereas in the case of Terragrunt, you are required to add terragrunt.hcl file in each environment and run “terragrunt init” in that particular environment to generate files in it, compared to Teramate code is auto-generated in all the stack automatically at once.

For example, if I have two environments (Development and Production) and by any chance I forgot to manually enter each environment and run “terragrunt init”, Terragunt won’t consider it when running for all environments, you need to explicitly enter each environment and run the command to make the code uniform if you forgot, it’s a great mistake.

  • In the above example, I forgot to run “terragrunt init” in the Production environment so when I need to generate all the configuration files Terragrunt won’t consider the Production environment, that simply means infrastructure would be different in dev compare to production.

This is a small flaw in the Terragrunt that it won’t maintain code uniformity until and unless you ask it explicitly (by running “terragrunt init” in each environment).

Whereas Terramate achieves this with ease and maintain the code uniformity.

Key Points

We have been using both tools for a while now to maintain our Infrastructure and to keep our codebase DRY.

  • Terramate has a good upper hand in terms of environment/stack creation since it can be done using a simple command.

  • Terramate and Terragrunt both support almost all cloud providers, so they both can generate all types of configuration files.

  • Terramate supports change detection which I don’t see in Terragrunt.

  • Terramate gives better code uniformity throughout.

You can explore more about Terramate and Terragrunt.

We understand that managing complex infrastructure can be tough, and making small changes without a lengthy process is important. That's why we recommend checking out Terramate and its examples.

Terramate has simplified our Terraform experience, and we believe it can do the same for you. So why wait? Give Terramate a try and improve your Terraform experience today!

Also, join their Discord channel for all future Updates about Terramate.

Did you find this article valuable?

Support Infrasity Blog by becoming a sponsor. Any amount is appreciated!