When you maintain Terraform configuration, an expression can do more than assign a value. It can decide whether Terraform creates a resource, filter input, or reshape data. An expression is a reference or calculation Terraform evaluates to produce a value. Add a comment when the code does not show why the expression behaves that way, what constraint it must meet, or what tradeoff it makes. Skip comments that just put the syntax into words.
HashiCorp’s Terraform style guide recommends clear code and comments that help maintainers understand complex expressions. A short # comment beside an expression can explain why someone chose it without cluttering the configuration.
Comment the reason, not the visible behavior
A conditional expression uses condition ? true_value : false_value: Terraform checks the condition and returns one of the two values. A comment that repeats this behavior adds little.
resource "example_metrics_endpoint" "this" {
# Keep the separate endpoint optional for deployments that do not run a metrics server.
count = var.enable_metrics ? 1 : 0
}
The comment gives a reason for this condition in the configuration. The version below only describes what the expression already shows:
resource "example_metrics_endpoint" "this" {
# Create one endpoint when enabled, or none when disabled.
count = var.enable_metrics ? 1 : 0
}
The first reason is an example, not a Terraform rule. Replace it with the actual constraint or intent in your configuration. If you have no accurate explanation to add, the expression is clear enough without a comment.
The same distinction applies to a for expression, which turns a collection into a tuple or object and can use an if clause to leave out some elements. This example keeps only administrator accounts:
locals {
# The access policy grants these bindings only to administrator accounts.
admins = {
for name, user in var.users : name => user
if user.is_admin
}
}
“Filter users where is_admin is true” would only restate the condition. A useful comment explains why the condition belongs here. Terraform’s for expression documentation explains how the expression transforms and filters the collection; the configuration owner explains the policy behind it.
Explain a constraint or surprising behavior
Comments help when the expression does not make Terraform’s behavior clear. For example, both sides of a conditional expression need compatible types. Terraform can convert values to a common type in some cases, so the expression below returns a string even though its first value is a number:
var.example ? 12 : "hello"
A comment that says # Return a string. names the result but does not explain the conversion. When you intend to convert the number to a string, show that conversion in the expression:
var.example ? tostring(12) : "hello"
If a less obvious constraint requires the conversion, explain that reason beside it. HashiCorp’s conditional expression guidance recommends explicit conversions when you are unsure of the expected type.
Terraform’s [*] splat expressions also work on one optional value: a value other than null becomes a tuple with one element, while null becomes an empty tuple. A dynamic block uses that tuple to create one nested block when the optional object exists, or none when it does not:
dynamic "website" {
# Adapt the optional object to zero or one values for this dynamic block.
for_each = var.website_setting[*]
content {
index_document = website.value.index_document
}
}
Here the comment explains why the expression uses a splat. “Iterate over website_setting” would only describe what the expression looks like. The splat expression documentation explains how it works on one value, and the dynamic block documentation explains how Terraform uses a collection to create nested blocks.
A comment also helps when the expression’s structure hides a constraint. An object-producing for expression creates an object from its results. Duplicate keys cause an error unless you enable grouping mode with .... If several users can share a role, explain that reason beside the grouping expression:
locals {
users_by_role = {
for name, user in var.users : user.role => name...
}
}
A comment such as “Group users by role” repeats the code. A useful comment would explain that several users can share a role, so the result needs to keep all their names.
Keep the comment close and accurate
Put a short # comment directly above the expression or argument it explains. Terraform also supports // and block comments, but HashiCorp’s configuration syntax documentation and style guidance identify # as the default, standard form.
A comment should describe the intent or constraint that remains true if someone changes how the expression works. For example, if an access policy limits which users receive a binding, state that policy instead of describing the current if clause. When the policy changes, update or remove the comment along with the expression.
Do not promise behavior the expression does not guarantee. For example, a for expression that converts a set of non-string values does not preserve a stable order. Terraform documents arbitrary ordering for those values, so a comment should not claim that the result always appears in the same order. Comment on ordering only when the configuration requires a particular order and the expression provides it.