terraform working with for and for_each

terraform "for" is used for data transformation while for_each is used to create resource

for_each 


locals {
  servers = {
    "web-1" = { cpu = 2 }
    "web-2" = { cpu = 4 }
  }
}

resource "null_resource" "servers" {
  for_each = local.servers  # Creates 2 resources
  triggers = { cpu = each.value.cpu }
}



for loop usage can be seen here



locals {
  names = ["alice", "bob", "charlie"]
 
  # Transform to uppercase (just data)
  upper_names = [for name in local.names : upper(name)]
 
  # Create map (just data)
  name_map = {for name in local.names : name => upper(name)}
 
  # Filter (just data)
  short_names = [for name in local.names : name if length(name) < 5]
}

output "result" {
  value = local.upper_names  # Just prints ["ALICE", "BOB", "CHARLIE"]
}


Both for_each and for can be used with map and set. for_each do not like list and null. for on the other hand not like null



locals {
 
  nullItem = null
 
  emptyList = [] # for_Each don't like

  emptySet = {}  # for_Each like it

  emptyMap = {}  # for_Each like it

  mapItems = { x = 1, y = 2, z = 3 }

  items = ["a", "b", "c"]
 
  setItems = toset(["a", "b", "c"])

  services = {
    "api"  = { port = 8080 }
    "web"  = { port = 80 }
    "db"   = { port = 5432 }
  }

}

# Outputs with set
# + "my_key"   = "c"
# + "my_value" = "c"
resource "null_resource" "set" {
  for_each = local.setItems
   triggers = {
    my_key   = each.key      # Print the key
    my_value = each.value    # Print the value
  }
}

# Outputs with set
# + "my_key"   = "x"
# + "my_value" = "1"
resource "null_resource" "map" {
  for_each = local.mapItems
   triggers = {
    my_key   = each.key      # Print the key
    my_value = each.value    # Print the value
  }
}

# for_each don't like null
# resource "null_resource" "null" {
#   for_each = null
#    triggers = {
#     my_key   = each.key      # Print the key
#     my_value = each.value    # Print the value
#   }
# }

# for_each like empty set
resource "null_resource" "empty_set" {
  for_each = {}
   triggers = {
    my_key   = each.key      # Print the key
    my_value = each.value    # Print the value
  }
}

# for_each static list - don't like
# resource "null_resource" "static_list" {
#   for_each = []
#    triggers = {
#     my_key   = each.key      # Print the key
#     my_value = each.value    # Print the value
#   }
# }

output "print_list" {
  value = [
    for key, value in local.services :
    "Service: ${key} on port ${value.port}"
  ]
}


output "print_list_conditonal" {
  value = [
    for key, value in local.services :
    "Service: ${key} on port ${value.port}" if key == "api"
  ]
}

# for also don't like null
# output "print_list_null" {
#   value = [
#     for key, value in null :
#     "Service: ${key} on port ${value.port}"
#   ]
# }

output "print_list_list" {
  value = [
    for key, value in [] :
    "Service: ${key} on port ${value.port}"
  ]
}


output "print_list_set" {
  value = [
    for key, value in {} :
    "Service: ${key} on port ${value.port}"
  ]
}






Comments

Popular posts from this blog

Windows SSH: Permissions for 'private-key' are too open

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20