Ad-hoc HCL Code Generation
INFO
This is an experimental feature. Please enable the "tmgen"
experiment in your Terramate configuration by adding it to the terramate.config.experiments
list.
Advanced documentation can be found here once the feature reaches stable status.
Please contact us on GitHub or in Discord if you like to know more or want to give early feedback.
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. This means you do not need to place the to-be generated code in the generate_hcl
block of a separate .tm.hcl
file as done in the Code Generation.
You can use Terramate data such as globals , metadata or functions in a .tmgen
file.
Important
To use globals
in a .tmgen
file , create a separate config.tm.hcl
file to define them first.
VSCode Syntax Highlighting
To enable syntax highlighting in VSCode, you need to associate the .tmgen
filetype in your settings:
{
"files.associations": {
"*.tf.tmgen": "tf",
"*.json.tmgen": "json",
...
}
}
Example
- Create a new repository:
git init tmgen-guide
- Create a new file inside the repo called
terramate.tm.hcl
with the following content, enabling thetmgen
feature:
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.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
}
- Create a new file to define the global
config.tm.hcl
with the following content:
globals "terraform" "modules" "vpc" {
version = "2.2.0"
}
- Run
terramate generate
to generate amain.tf
file in theexample-stack
using themain.tf.tmgen
file as a blueprint.
terramate generate