Define Terraform for_each with module instances using a map of objects variable to provision per-environment infrastructure with distinct configurations
Declare a variable of type map(object(...)) in the root module with each key representing an environment name and each value containing the environment-specific configuration attributes
Call the child module using for_each on the variable and pass each attribute from each.value to the corresponding module input variable
Ensure the child module uses outputs that can be referenced from the root module using module.<name>[each.key].output_name syntax in dependent resources or outputs
Run terraform plan to verify the expected number of module instances are planned and that each instance has the correct configuration values shown in the plan output
Use terraform state list to confirm the state addresses follow the module.<name>["key"] convention and understand how to target individual instances with -target for partial applies
Known gotchas
for_each on modules requires the map keys to be known at plan time; if keys are derived from resource attributes not yet created, Terraform errors with a plan-time unknown value error — use explicit string keys in the variable rather than computed values
Adding or removing a key from the map variable destroys and recreates the corresponding module instance; if the instance manages stateful resources like databases, use lifecycle prevent_destroy or plan carefully before removing a key
The each.key and each.value variables are only available in expressions that Terraform evaluates per-instance; using them in a provider configuration block which is evaluated before for_each expansion causes an error
Give your agent this knowledge — and 200+ more routes
One MCP install gives any agent live access to the full route map, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp