Back to all blog posts
Changelog

Introduction: Simple HCL Code Generation

Introducing Ad-hoc HCL code generation, a more convenient way of generating HCL code for single stacks.

Photo of Annu Singh
Annu Singh
· min read
Simple HCL Code Generation

Introduction

The Terramate Code Generation feature keeps your code DRY by automating code generation in multiple stacks. While beneficial for multiple stacks, many users have asked for a more straightforward method for single-stack code generation. In response to this feedback, we are introducing Ad-hoc HCL code generation, a more convenient way of generating HCL code for single stacks.

With this new feature, you do not need to place the to-be generated code in the generate_hcl block of a separate .tm.hcl file. Instead you can use any HCL file as a blueprint to generate code for a stack by adding .tmgen to the file type and running terramate generate against it.

Use-Cases

  • Use Terraform, OpenTofu, or any other HCL file as a blueprint to generate code for a single stack by adding .tmgen to the file type.
  • Interpolate any Terraform or OpenTofu configuration (or any other HCL file) with Terramate data such as globals , metadata or functions .

Note: Use generate_hcl statement to generate code in multiple stacks using conditions or filters .

Before (Code Generation method):

This method is appropriate when generating code in multiple stacks, as we can use conditions or filters in a generate_hcl block.

# main.tm.hcl

globals "terraform" "modules" "vpc" {
  version = "2.2.0"
}

generate_hcl "main.tf" {
  # Can use conditions and filters
  stack_filter {
    project_paths = ["networking/**"]
  }

  content {
    module "vpc" {
    source  = "cloudposse/vpc/aws"
    version = tm_try(global.terraform.modules.vpc.version, "2.0.0")

    namespace = "eg"
    stage     = "test"
    name      = "app"

    ipv4_primary_cidr_block = "10.0.0.0/16"

    assign_generated_ipv6_cidr_block = true
  }
}

After (Ad-Hoc HCL Code Generation method):

# config.tm.hcl

globals "terraform" "modules" "vpc" {
  version = "2.2.0"
}
# main.tf.tmgen

module "vpc" {
  source  = "cloudposse/vpc/aws"
  version = tm_try(global.terraform.modules.vpc.version, "2.0.0")

  namespace = "eg"
  stage     = "test"
  name      = "app"

  ipv4_primary_cidr_block = "10.0.0.0/16"

  assign_generated_ipv6_cidr_block = true
}

To use Terramate data like globals , create a separate config.tm.hcl file to define them first. Terramate variables and functions can also be used in the .tmgen file.

Example:

Prerequisite: Install Terramate CLI

  1. Create a new repository:
    git init tmgen-guide
  2. Create a new file inside the repo called terramate.tm.hcl with the following content, enabling the tmgen feature:
    terramate {
      config {
        # Enables the simplified adhoc HCL code generation
        # <https://terramate.io/docs/cli/code-generation/tmgen>
        experiments = [
          "tmgen"
        ]
      }
    }
  3. Create a new stack:
    terramate create example-stack
  4. Create a new file main.tf.tmgen with the following content:
    module "vpc" {
      source  = "cloudposse/vpc/aws"
      version = tm_try(global.terraform.modules.vpc.version, "2.0.0")
    
      namespace = "eg"
      stage     = "test"
      name      = "app"
    
      ipv4_primary_cidr_block = "10.0.0.0/16"
    
      assign_generated_ipv6_cidr_block = true
    }
  5. Create a new file to define the global config.tm.hcl with the following content:
    globals "terraform" "modules" "vpc" {
      version = "2.2.0"
    }
  6. Run terramate generate to generate a main.tf file in the example-stack using the main.tf.tmgen file as a blueprint.
    terramate generate

In conclusion, Ad-hoc code generation simplifies single-stack code generation with Terramate. You can use any HCL file as a blueprint by adding .tmgen to the file type and running terramate generate . This enhancement addresses user feedback, making code generation more intuitive.

Join the Community

We’re committed to continually improving Terramate to meet your evolving needs. Dive into the Terramate community and connect with like-minded individuals who share your passion for infrastructure management. Whether you’re a seasoned pro or just getting started, our community is here to support you every step of the way. Join us on Discord  and be part of the conversation!

Annu is a Developer Advocate at Terramate. He has a software engineering and technical writing background, working primarily with Go to contribute to open-source projects in the cloud-native domain. Despite working at Terramate, Annu is also a community manager at @Dapr.io.