2021年3月31日星期三

alternative way, since for_each and count cannot be in same resource

I've config that need to use count and for_each at the same time. here's the config block

variable "all_zone" {    type    = list(any)    default = ["asia-southeast1-a", "asia-southeast1-b", "asia-southeast1-c"]  }    variable "backends" {    description = "Map backend indices to list of backend maps."    type = map(object({      neg_name  = string     }))  }    data "google_compute_network_endpoint_group" "get_neg" {    for_each = var.backends    count   = length(var.all_zone)    zone    = var.all_zone[count.index]    name    = lookup(each.value, "neg_name")      }    resource "google_compute_backend_service" "default" {  . . .  dynamic "backend" {      for_each = [for b in data.google_compute_network_endpoint_group.get_neg[*].id : b]      content {        group = backend.value      }    }  }  

so in basically this is to get resource data (google_compute_network_endpoint_group) nested.

here's the logic:

var neg_name = ['name-1', 'name-2]  var all_zone = ['location-1, location-2, location-3]    data "google_compute_network_endpoint_group" "name1" {        count   = length(var.all_zone)        zone    = var.all_zone[count.index]        name    = 'name1'      }    data "google_compute_network_endpoint_group" "name2" {        count   = length(var.all_zone)        zone    = var.all_zone[count.index]        name    = 'name2'      }  

since for_each and count cannot be in same resource, is there anyway to do this?

thank you

https://stackoverflow.com/questions/66897617/alternative-way-since-for-each-and-count-cannot-be-in-same-resource April 01, 2021 at 10:15AM

没有评论:

发表评论