Explore how Terramate can uplift your IaC projects with a free trial or personalized demo.
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
.tmgento 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_hclstatement 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.hclfile to define them first. Terramate variables and functions can also be used in the.tmgenfile.
Example:
Prerequisite: Install Terramate CLI
- Create a new repository:
git init tmgen-guide - Create a new file inside the repo called
terramate.tm.hclwith the following content, enabling thetmgenfeature:terramate { config { # Enables the simplified adhoc HCL code generation # <https://terramate.io/docs/cli/code-generation/tmgen> experiments = [ "tmgen" ] } } - Create a new stack:
terramate create example-stack - Create a new file
main.tf.tmgenwith 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 } - Create a new file to define the global
config.tm.hclwith the following content:globals "terraform" "modules" "vpc" { version = "2.2.0" } - Run
terramate generateto generate amain.tffile in theexample-stackusing themain.tf.tmgenfile 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!
)
)