'In Terraform, what is '${element(aws_subnet.public.*.id, count.index)}'?
Can anyone explain this line.
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
I used this in terraform code for creating one stack
Solution 1:[1]
aws_subnet.public.*.id
return a list of subnet ids, e.g.:
["subnet-3232", "subnet-555", "subnet-6666"]
So element(aws_subnet.public.*.id, count.index)
will pick a single subnet id from the list, depending on the value of count.index
. For example:
element(["subnet-3232", "subnet-555", "subnet-6666"], 0) # pick first one
element(["subnet-3232", "subnet-555", "subnet-6666"], 1) # pick second one
element(["subnet-3232", "subnet-555", "subnet-6666"], 2) # pick last one
element(["subnet-3232", "subnet-555", "subnet-6666"], 3) # pick first one again (aka wrap-around)
Also if count.index
is greater then the number of elements, it element will go back and start from the head of the list (aka "wrap-around"
).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |