Created
July 11, 2022 18:29
-
-
Save joseche/777abccda2010d9360d953128fa2a7cc to your computer and use it in GitHub Desktop.
Terraform - Iterators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # 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