how do you access a value in a list of maps?

the value of google_compute_subnetwork.subnetwork.secondary_ip_range looks like this:

[
    {
        ip_cidr_range = 10.1.0.0/16,
        range_name = my-range
    }
]

I cant figure out how to loop over that, this doesnt work:

resource "aws_security_group_rule" "sdfsdfsdf" {
 count = "${length(data.google_compute_subnetwork.mysubnetwork.secondary_ip_range)}"
 type      = "ingress"
 from_port    = 0
 to_port     = 0
 protocol    = "-1"
  cidr_blocks = ["${data.google_compute_subnetwork.mysubnetwork.secondary_ip_range[count.index]}.ip_cidr_range}"]
}
728x90

1 Answers how do you access a value in a list of maps?

the usage of count.index is in this document:

https://www.terraform.io/docs/configuration-0-11/interpolation.html#element-list-index-

element(aws_subnet.foo.*.id, count.index)

So your code can be changed to

resource "aws_security_group_rule" "sdfsdfsdf" {
 count = "${length(data.google_compute_subnetwork.mysubnetwork.secondary_ip_range)}"
 type      = "ingress"
 from_port    = 0
 to_port     = 0
 protocol    = "-1"
  cidr_blocks = ["${element(data.google_compute_subnetwork.mysubnetwork.secondary_ip_range.*.ip_cidr_range, count.index)}"]
}

1 weeks ago