Learn Together: Terramate

Kerem Demirtürk
3 min readNov 3, 2023

Hey Everyone,

Today, we’re going to learn together Terramate, While I’ve had experience with Terraform and Terragrunt in the past, Terramate is a new territory for me, and it looks incredibly intriguing.

Before we dive in, I want to give a shoutout to the Terramate documentation. It’s truly impressive; it answered a lot of questions that popped into my mind, offering clear explanations, helpful notes, and much more.

Now, let’s get down to the basics!

I know that Terramate is an Infrastructure as Code (IaC) tool and serves as an orchestrator for Terraform. What’s particularly intriguing is its ability to manage applications or resources as stacks. This feature, in my opinion, makes Terramate an excellent choice, especially for those familiar with CloudFormation. Let’s delve deeper into what it has to offer.

  • Terramate enriches Terraform with a slew of powerful capabilities, including code generation, stack management, orchestration, change detection, data sharing, and much more.

Installation for me:

brew install terramate

For others, detailed installation instructions can be found here: Terramate Installation. Please note that having Terraform already installed on your computer is a prerequisite.

Following the installation, I created a GitHub repository and started working on it.

to configure the project root by creating a terramate.tm.hcl configuration at the top level or any sub-directory in your Terramate project.

After that, I followed the steps and created my own stack using the command:

terramate create myblog

This command created a folder named “myblog” with stack definitions inside it.

Terramate recognizes files with the .tm or .tm.hcl extension, and any file containing a stack {} block is considered a stack. A stack is simply a directory where Terramate generates Terraform files.

P.S: You can manually create the stack.tm.hcl file without an ID or by running the terramate create command.

Creating Terraform code for our blog is a unique experience with Terramate! To explore this, start by adding the following code to your stack.tm.hcl file:

generate_hcl "mysite.tf" {
content {
resource "local_file" "mysite" {
filename = "/tmp/tfmysite/index.html"
content = <<-EOF
<html>
<title>My Website</title>
</html>
EOF
}
}
}

Once this code is in place, you can use Terramate to generate your Terraform code with the following command:

terramate generate

After running the terramate generate command, you'll find your myblog.tf file. You can then follow up with terraform init and terraform plan to understand what's happening. This unique approach is quite intriguing and can be incredibly useful for blog setup.

Now, let’s talk about global variables. They might sound familiar, but let’s illustrate with an example. To start, create a globals.tm.hcl file in the root directory, and give a title to your blog:

# file: globals.tm.hcl

globals {
title = "My Blog"
}

The name globals.tm.hcl is not required, and the content could be included in an existing terramate.tm.hcl file. All Terramate files are merged during runtime, similar to how Terraform merges .tf files. This simplicity provides great flexibility.

With the global variable defined, you can use it inside your stack. You initially defined your title as:

<title>My Website</title>

To incorporate the new global title, modify it as follows:

<title>${global.title}</title>

To apply this change, run:

terramate generate

I’m excited to delve deeper into this topic because it’s both different and fascinating. Stay tuned for more adventures with Terramate. See you next time!

Kerem DEMIRTURK

Source: https://terramate.io/docs/cli/

--

--