- Locals
Locals let you define computed, reusable values inside a module. Treat them as module-level constants derived from inputs. Use locals to:
- Consolidate repeated logic (formatting, naming, derived IDs).
- Simplify complex expressions (combinations of
forexpressions and conditional logic). - Reduce duplication to make HCL easier to read and maintain.
Use locals when a value is derived from inputs and reused in multiple places. Locals are evaluated per-module and do not create additional state or resources.
- Dynamic blocks
Dynamic blocks generate repeated nested blocks inside resources or modules, based on input collections. They prevent copy-pasting when the number of nested blocks varies.
ingress rules from a variable list

Avoid overusing dynamic blocks to hide complex logic. If dynamic generation makes the configuration hard to read, prefer an explicit approach or refactor into a smaller module with clear inputs.
- Built-in functions
Terraform’s built-in functions help you inspect and transform data safely. Common ones include:
join(separator, list)— join list elementsconcat(list1, list2)— concatenate listscoalesce(val1, val2, ...)— return the first non-empty valuelookup(map, key, default)— safe map lookupjsonencode(value)— encode an HCL value as JSON
- Decision-making and best practices
Apply these constructs to improve readability and maintainability, not to obscure intent. Key guidelines:
- Compute values locally when they’re derived from module inputs and used in several places.
- Keep module APIs (inputs/outputs) explicit; avoid hiding important behavior inside complex locals or dynamic generation.
- Favor small, focused modules that are easy to test and reason about.
- Use built-in functions to write defensive expressions that tolerate optional or missing inputs.
- Are derived values used more than once? Use
locals. - Is the nested block count variable (and simple)? Use
dynamic. - Does complexity harm readability? Consider refactoring into a module.
- Can you write expressions that survive missing inputs? Use functions like
coalesceandlookup.
- Terraform: Locals
- Terraform: Dynamic Blocks
- Terraform: Functions
- Terraform best practices and module design patterns