Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Terraform variable type list with for_each for loop examples

In this article I have explored all the examples on how to use list of strings with for_each for loop in Terraform resource and output block.

Microsoft Azure Terraform toset() for_each map list of string objects resource null_resource output variable block example.png

In this first block of example I have defined  variable named marvel, it's type is list and value has string names of superheroes inside [ ] square bracket as shown in below example.

variable "marvel" {
  type = list(any)
  default = ["IronMan","Thor","Hulk","HawkEye","SpiderMan"]
}

In the first demo to use list with for_each loop inside resource block I have simply converted it to set with toset() function. To iterate through the list I am using each.value which has the content of each string element from list. You can also use each.key instead. 

resource "null_resource" "avengers_captainmarvel" {
  for_each = toset(var.marvel)
  provisioner "local-exec" {
    command = "echo ${each.value} is an Avenger!"
  }
}

Following in the second example I am using for loop with for_each to create key=value pair map from variable list type. Key and value both will be the same in this case. You can use each.key or each.value to iterate through the list.

resource "null_resource" "avengers_blackwidow" {
  for_each = { for avenger in var.marvel : avenger => avenger }
  provisioner "local-exec" {
    command = "echo ${each.value} is an Avenger!"
  }
}
Check this out may be useful to you: Terraform variable multiple validation advanced blocks example

In this final example, we'll explore how to utilize list within a resource block using the count and length() functions. Unlike previous examples, all the loop logic is encapsulated within the for_each loop. By leveraging count to retrieve the length of the list, we can use the count index to iterate through the list elements. This approach enables efficient and concise resource creation based on list values.
In this example, count is set to the length of var.marvel, allowing us to access each element using count.index. This technique streamlines resource creation and management, making it a valuable addition to your Terraform toolkit.
resource "null_resource" "avengers_scarletwitch" {
  count = length(var.marvel)
  provisioner "local-exec" {
    command = "echo ${var.marvel[count.index]} is an Avenger!"
  }
}

Apart from resource block, This is how output block is implemented to show list. To display list you can directly use variable name in the value to show the array contents.

output "avengers" {
  value = var.marvel
}

Another approach to achieve this is by using a for loop to create a map from the list. In this map, each key-value pair has the same name for both the key and the value. Note I am using { } curly braces for the same.

output "avengers_infinity_wars" {
  value = { for avenger in var.marvel : avenger => avenger }
}

In this final example I am simply using another for loop variation to show the value from list variable, Note I am using [ ] square brackets.

output "avengers_secret_wars" {
  value = [ for avenger in var.marvel : avenger ]
}

Below is the entire script which you can download Terraform_for_each_list_resources_and_output.tf here or it is also available on the github.com.

 

variable "marvel" {
  type = list(any)
  default = ["IronMan","Thor","Hulk","HawkEye","SpiderMan"]
}

resource "null_resource" "avengers_captainmarvel" {
  for_each = toset(var.marvel)
  provisioner "local-exec" {
    command = "echo ${each.value} is an Avenger!"
  }
}

resource "null_resource" "avengers_blackwidow" {
  for_each = { for avenger in var.marvel : avenger => avenger }
  provisioner "local-exec" {
    command = "echo ${each.value} is an Avenger!"
  }
}

resource "null_resource" "avengers_scarletwitch" {
  count = length(var.marvel)
  provisioner "local-exec" {
    command = "echo ${var.marvel[count.index]} is an Avenger!"
  }
}

output "avengers" {
  value = var.marvel
}

output "avengers_infinity_wars" {
  value = { for avenger in var.marvel : avenger => avenger }
}

output "avengers_endgame" {
  value = toset(var.marvel)
}

output "avengers_secret_wars" {
  value = [ for avenger in var.marvel : avenger ]
}

In the Below is the output after running terraform apply command. Check and verify how it shows the output for resources and output block.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
terraform apply --auto-approve  

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.avengers_blackwidow["HawkEye"] will be created
  + resource "null_resource" "avengers_blackwidow" {
      + id = (known after apply)
    }

  # null_resource.avengers_blackwidow["Hulk"] will be created
  + resource "null_resource" "avengers_blackwidow" {
      + id = (known after apply)
    }

  # null_resource.avengers_blackwidow["IronMan"] will be created
  + resource "null_resource" "avengers_blackwidow" {
      + id = (known after apply)
    }

  # null_resource.avengers_blackwidow["SpiderMan"] will be created
  + resource "null_resource" "avengers_blackwidow" {
      + id = (known after apply)
    }

  # null_resource.avengers_blackwidow["Thor"] will be created
  + resource "null_resource" "avengers_blackwidow" {
      + id = (known after apply)
    }

  # null_resource.avengers_captainmarvel["HawkEye"] will be created
  + resource "null_resource" "avengers_captainmarvel" {
      + id = (known after apply)
    }

  # null_resource.avengers_captainmarvel["Hulk"] will be created
  + resource "null_resource" "avengers_captainmarvel" {
      + id = (known after apply)
    }

  # null_resource.avengers_captainmarvel["IronMan"] will be created
  + resource "null_resource" "avengers_captainmarvel" {
      + id = (known after apply)
    }

  # null_resource.avengers_captainmarvel["SpiderMan"] will be created
  + resource "null_resource" "avengers_captainmarvel" {
      + id = (known after apply)
    }

  # null_resource.avengers_captainmarvel["Thor"] will be created
  + resource "null_resource" "avengers_captainmarvel" {
      + id = (known after apply)
    }

  # null_resource.avengers_scarletwitch[0] will be created
  + resource "null_resource" "avengers_scarletwitch" {
      + id = (known after apply)
    }

  # null_resource.avengers_scarletwitch[1] will be created
  + resource "null_resource" "avengers_scarletwitch" {
      + id = (known after apply)
    }

  # null_resource.avengers_scarletwitch[2] will be created
  + resource "null_resource" "avengers_scarletwitch" {
      + id = (known after apply)
    }

  # null_resource.avengers_scarletwitch[3] will be created
  + resource "null_resource" "avengers_scarletwitch" {
      + id = (known after apply)
    }

  # null_resource.avengers_scarletwitch[4] will be created
  + resource "null_resource" "avengers_scarletwitch" {
      + id = (known after apply)
    }

Plan: 15 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + avengers               = [
      + "IronMan",
      + "Thor",
      + "Hulk",
      + "HawkEye",
      + "SpiderMan",
    ]
  + avengers_endgame       = [
      + "HawkEye",
      + "Hulk",
      + "IronMan",
      + "SpiderMan",
      + "Thor",
    ]
  + avengers_infinity_wars = {
      + HawkEye   = "HawkEye"
      + Hulk      = "Hulk"
      + IronMan   = "IronMan"
      + SpiderMan = "SpiderMan"
      + Thor      = "Thor"
    }
  + avengers_secret_wars   = [
      + "IronMan",
      + "Thor",
      + "Hulk",
      + "HawkEye",
      + "SpiderMan",
    ]
null_resource.avengers_captainmarvel["HawkEye"]: Creating...
null_resource.avengers_captainmarvel["Hulk"]: Creating...
null_resource.avengers_blackwidow["HawkEye"]: Creating...
null_resource.avengers_blackwidow["IronMan"]: Creating...
null_resource.avengers_blackwidow["Hulk"]: Creating...
null_resource.avengers_scarletwitch[2]: Creating...
null_resource.avengers_captainmarvel["IronMan"]: Creating...
null_resource.avengers_blackwidow["HawkEye"]: Provisioning with 'local-exec'...
null_resource.avengers_captainmarvel["Hulk"]: Provisioning with 'local-exec'...
null_resource.avengers_captainmarvel["SpiderMan"]: Creating...
null_resource.avengers_blackwidow["SpiderMan"]: Creating...
null_resource.avengers_blackwidow["Thor"]: Creating...
null_resource.avengers_scarletwitch[2]: Provisioning with 'local-exec'...
null_resource.avengers_captainmarvel["IronMan"]: Provisioning with 'local-exec'...
null_resource.avengers_blackwidow["IronMan"]: Provisioning with 'local-exec'...
null_resource.avengers_captainmarvel["SpiderMan"]: Provisioning with 'local-exec'...
null_resource.avengers_scarletwitch[2] (local-exec): Executing: ["cmd" "/C" "echo Hulk is an Avenger!"]
null_resource.avengers_blackwidow["SpiderMan"]: Provisioning with 'local-exec'...
null_resource.avengers_captainmarvel["IronMan"] (local-exec): Executing: ["cmd" "/C" "echo IronMan is an Avenger!"]
null_resource.avengers_captainmarvel["SpiderMan"] (local-exec): Executing: ["cmd" "/C" "echo SpiderMan is an Avenger!"]
null_resource.avengers_blackwidow["Hulk"]: Provisioning with 'local-exec'...
null_resource.avengers_blackwidow["SpiderMan"] (local-exec): Executing: ["cmd" "/C" "echo SpiderMan is an Avenger!"]
null_resource.avengers_blackwidow["Thor"]: Provisioning with 'local-exec'...
null_resource.avengers_captainmarvel["Hulk"] (local-exec): Executing: ["cmd" "/C" "echo Hulk is an Avenger!"]
null_resource.avengers_blackwidow["Thor"] (local-exec): Executing: ["cmd" "/C" "echo Thor is an Avenger!"]
null_resource.avengers_blackwidow["HawkEye"] (local-exec): Executing: ["cmd" "/C" "echo HawkEye is an Avenger!"]
null_resource.avengers_captainmarvel["HawkEye"]: Provisioning with 'local-exec'...
null_resource.avengers_blackwidow["IronMan"] (local-exec): Executing: ["cmd" "/C" "echo IronMan is an Avenger!"]
null_resource.avengers_blackwidow["Hulk"] (local-exec): Executing: ["cmd" "/C" "echo Hulk is an Avenger!"]
null_resource.avengers_captainmarvel["HawkEye"] (local-exec): Executing: ["cmd" "/C" "echo HawkEye is an Avenger!"]
null_resource.avengers_scarletwitch[2] (local-exec): Hulk is an Avenger!
null_resource.avengers_captainmarvel["SpiderMan"] (local-exec): SpiderMan is an Avenger!
null_resource.avengers_captainmarvel["SpiderMan"]: Creation complete after 0s [id=9192055606425142333]
null_resource.avengers_scarletwitch[2]: Creation complete after 0s [id=7063158102767413277]
null_resource.avengers_captainmarvel["Hulk"] (local-exec): Hulk is an Avenger!
null_resource.avengers_scarletwitch[3]: Creating...
null_resource.avengers_captainmarvel["Thor"]: Creating...
null_resource.avengers_captainmarvel["Hulk"]: Creation complete after 0s [id=7770242689606045898]
null_resource.avengers_scarletwitch[3]: Provisioning with 'local-exec'...
null_resource.avengers_scarletwitch[3] (local-exec): Executing: ["cmd" "/C" "echo HawkEye is an Avenger!"]
null_resource.avengers_captainmarvel["IronMan"] (local-exec): IronMan is an Avenger!
null_resource.avengers_captainmarvel["IronMan"]: Creation complete after 0s [id=1981800038228562201]
null_resource.avengers_captainmarvel["Thor"]: Provisioning with 'local-exec'...
null_resource.avengers_captainmarvel["Thor"] (local-exec): Executing: ["cmd" "/C" "echo Thor is an Avenger!"]
null_resource.avengers_scarletwitch[4]: Creating...
null_resource.avengers_blackwidow["SpiderMan"] (local-exec): SpiderMan is an Avenger!
null_resource.avengers_scarletwitch[1]: Creating...
null_resource.avengers_scarletwitch[4]: Provisioning with 'local-exec'...
null_resource.avengers_scarletwitch[4] (local-exec): Executing: ["cmd" "/C" "echo SpiderMan is an Avenger!"]
null_resource.avengers_scarletwitch[1]: Provisioning with 'local-exec'...
null_resource.avengers_blackwidow["SpiderMan"]: Creation complete after 0s [id=5302315061282559139]
null_resource.avengers_scarletwitch[1] (local-exec): Executing: ["cmd" "/C" "echo Thor is an Avenger!"]
null_resource.avengers_blackwidow["Thor"] (local-exec): Thor is an Avenger!
null_resource.avengers_blackwidow["Thor"]: Creation complete after 0s [id=845785058948536104]
null_resource.avengers_scarletwitch[0]: Creating...
null_resource.avengers_scarletwitch[0]: Provisioning with 'local-exec'...
null_resource.avengers_blackwidow["HawkEye"] (local-exec): HawkEye is an Avenger!
null_resource.avengers_scarletwitch[0] (local-exec): Executing: ["cmd" "/C" "echo IronMan is an Avenger!"]
null_resource.avengers_blackwidow["HawkEye"]: Creation complete after 0s [id=7106059011530237974]
null_resource.avengers_blackwidow["IronMan"] (local-exec): IronMan is an Avenger!
null_resource.avengers_blackwidow["IronMan"]: Creation complete after 0s [id=865390357854072773]
null_resource.avengers_blackwidow["Hulk"] (local-exec): Hulk is an Avenger!
null_resource.avengers_blackwidow["Hulk"]: Creation complete after 0s [id=6432483903170596779]
null_resource.avengers_captainmarvel["HawkEye"] (local-exec): HawkEye is an Avenger!
null_resource.avengers_captainmarvel["HawkEye"]: Creation complete after 0s [id=3649154194512024566]
null_resource.avengers_scarletwitch[3] (local-exec): HawkEye is an Avenger!
null_resource.avengers_scarletwitch[3]: Creation complete after 0s [id=2121556317005986060]
null_resource.avengers_captainmarvel["Thor"] (local-exec): Thor is an Avenger!
null_resource.avengers_captainmarvel["Thor"]: Creation complete after 0s [id=381719455332942953]
null_resource.avengers_scarletwitch[1] (local-exec): Thor is an Avenger!
null_resource.avengers_scarletwitch[1]: Creation complete after 0s [id=6117933928361479405]
null_resource.avengers_scarletwitch[4] (local-exec): SpiderMan is an Avenger!
null_resource.avengers_scarletwitch[4]: Creation complete after 0s [id=5375947507644832841]
null_resource.avengers_scarletwitch[0] (local-exec): IronMan is an Avenger!
null_resource.avengers_scarletwitch[0]: Creation complete after 0s [id=288149830994279046]

Apply complete! Resources: 15 added, 0 changed, 0 destroyed.

Outputs:

avengers = tolist([
  "IronMan",
  "Thor",
  "Hulk",
  "HawkEye",
  "SpiderMan",
])
avengers_endgame = toset([
  "HawkEye",
  "Hulk",
  "IronMan",
  "SpiderMan",
  "Thor",
])
avengers_infinity_wars = {
  "HawkEye" = "HawkEye"
  "Hulk" = "Hulk"
  "IronMan" = "IronMan"
  "SpiderMan" = "SpiderMan"
  "Thor" = "Thor"
}
avengers_secret_wars = [
  "IronMan",
  "Thor",
  "Hulk",
  "HawkEye",
  "SpiderMan",
]

Useful Articles
Terraform Azure function app with private endpoint and storage account
Hashicorp Terraform map and object inside module and variable example
Terraform one module deploy null or multiple resources based on input
Terraform A reference to a resource type must be followed by at least one attribute access, specifying the resource name
Terraform fore_each for loop filter with if condition example
Terraform remote-exec provisioner with ssh connection in null_resource
Terraform count vs for_each for examples with map of objects
Terraform one module deploy null or multiple resources based on input (nested for loop) Example of Terraform functions flatten() and coalesce()
Terraform Azure Create Private Endpoint to existing Storage Account with Custom Private DNS zone record link
Creating a Private Endpoint for Azure Storage Account with required sub services using Terraform Example Terraform functions of lookup() and lower()
Using element function with count meta argument example Terraform Azure subnets Example Terraform functions of element()count() and sum()
Terraform create Azure Virtual Network subnets from map of object and show name in the header Header name change in the output
Creating a Private Endpoint for Azure Storage Account with Terraform example 2 Example of for_each with toset() function
Creating a Private Endpoint for Azure Storage Account with Terraform example 3

Go Back

Comment

Blog Search

Page Views

12086144

Follow me on Blogarama