concept
For_each and stable keys
for_each associates instances with distinct map keys or string set members. each.key and each.value expose the identity and corresponding setting; reference a single instance with a quoted key.
Formal Syntax & Structure
terraform syntaxterraform
For_each and stable keysWorking Example
Example Snippetterraform
variable "environments" { type = map(string) nullable = false default = { dev = "small" test = "medium" } validation { condition = length(var.environments) <= 5 && alltrue([for k, v in var.environments : v != null]) error_message = "Supply at most five environments with non-null labels." }}
resource "terraform_data" "environment" { for_each = var.environments input = each.value}
output "labels" { value = { for k, item in terraform_data.environment : k => item.output }}