The lets block β
Use the lets block in Terramate to define context-based variables local to the parent block where you declare them. Utilize these variables in surrounding attributes or blocks, such as generate_hcl, generate_file, and script blocks. The lets block achieves cleaner code by preventing global namespace clutter with locally scoped variables.
Arguments β
<variable-name>: The name of the variable to be defined.<expression>: The expression to be evaluated and assigned to the variable.
Syntax β
hcl
lets {
<variable-name> = <expression>
}Contexts β
You can use the lets block in the following contexts:
generate_hclblocksgenerate_fileblocksscriptblocks
Key Differences from Global Variables β
letsvariables are only available within the block they are defined in and do not carry over to child directories.letsvariables cannot use labels.letsvariables can be accessed using the let namespace in expressions.
Examples β
Using lets in a generate_file Block β
In this example, a JSON-encoded object is created using a lets variable and used within the generate_file block.
hcl
generate_file "file.json" {
lets {
# let.json is available in the current generate_file block only
json = tm_jsonencode({ "hello" = "world" })
}
content = let.json
}Defining Temporary Variables β
Here, a temporary variable is defined within the lets block and used in other arguments within the generate_file block.
hcl
generate_file "example.txt" {
lets {
temp_a_plus_b = global.a + global.b
}
content = let.temp_a_plus_b
}Using lets in a generate_hcl Block β
In this example, a lets block is used within a generate_hcl block to define a temporary variable.
hcl
generate_hcl "file.tf" {
lets {
greeting = "Hello, World!"
}
content {
message: let.greeting
}
}