Skip to content

Instantly share code, notes, and snippets.

@joseche
Created July 11, 2022 18:29
Show Gist options
  • Select an option

  • Save joseche/777abccda2010d9360d953128fa2a7cc to your computer and use it in GitHub Desktop.

Select an option

Save joseche/777abccda2010d9360d953128fa2a7cc to your computer and use it in GitHub Desktop.
Terraform - Iterators
#
# Simple lists
#
locals {
ip_addresses = ["10.0.0.1", "10.0.0.2"]
}
resource "example" "example" {
for_each = toset(local.ip_addresses)
ip_address = each.key
}
#
# Lists of objects
#
locals {
virtual_machines = [
{
ip_address = "10.0.0.1"
name = "vm-1"
},
{
ip_address = "10.0.0.1"
name = "vm-2"
}
]
}
resource "example" "example" {
for_each = {
for index, vm in local.virtual_machines:
vm.name => vm # Perfect, since VM names also need to be unique
# OR: index => vm (unique but not perfect, since index will change frequently)
}
name = each.value.name
ip_address = each.value.ip_address
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment