In Terragrunt files, locals are a convenient way to name a value. Used without a reason, they only add a level of indirection.
Define a local only if it is reused (DRY) or if it abstracts complexity. Otherwise, inline its value where it is used.
A local is useful in two cases:
A local that holds a literal or an alias to another value, and is used only once, is neither of those.
locals block and back. For a value used once, inlining is faster to read.Inline values that are used once and need no explanation.
Not recommended ❌
locals {
region = "eu-west-3" # used once, nothing to abstract
}
inputs = {
region = local.region
}
Ok :+1:
inputs = {
region = "eu-west-3"
}
Define a local when the value is reused or hides complexity.
Ok :+1:
locals {
# Reused: defined once, used in several places
environment = basename(get_original_terragrunt_dir())
# Complexity: merges several values behind a meaningful name
tags = merge(
local.root.locals.common_tags,
{ environment = local.environment },
)
}
inputs = {
tags = local.tags
bucket_name = "my-project-${local.environment}"
}
If a value is repeated across several layers rather than within a file, a local is not the answer: move the input up the tree instead (see the Context pattern).