Skip to content

Ad-hoc HCL Code Generation


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.


To use globals in a .tmgen file , create a separate file to define them first.


  1. Create a new repository:
git init tmgen-guide
  1. Create a new file inside the repo called with the following content, enabling the tmgen feature:
terramate {
  config {
    # Enables the simplified adhoc HCL code generation
    experiments = [
  1. Create a new stack:
terramate create example-stack
  1. Create a new file 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 = ""

  assign_generated_ipv6_cidr_block = true
  1. Create a new file to define the global with the following content:
globals "terraform" "modules" "vpc" {
  version = "2.2.0"
  1. Run terramate generate to generate a file in the example-stack using the file as a blueprint.
terramate generate