Explore how Terramate can uplift your IaC projects with a free trial or personalized demo.
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.
.tmgen
to the file type.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.
Prerequisite: Install Terramate CLI
git init tmgen-guide
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"
]
}
}
terramate create example-stack
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
}
config.tm.hcl
with the following content:globals "terraform" "modules" "vpc" {
version = "2.2.0"
}
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.
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!
Explore how Terramate can uplift your IaC projects with a free trial or personalized demo.