'constructing reference call with for_each in line
Whats going on?
- I have created a couple of "google_compute_health_check" on GCP
- To re-use the health checks, my variables refer to whatever health_check they need
- I am now creating another resource that needs to refer to this health check
health-check.tf
resource "google_compute_health_check" "tcp-8008-hc" {
name = "tcp-8008-hc"
timeout_sec = 5
check_interval_sec = 5
http_health_check {
port = "8008"
request_path = "/"
}
}
resource "google_compute_health_check" "tcp-80-hc" {
name = "tcp-80-hc"
timeout_sec = 5
check_interval_sec = 5
http_health_check {
port = "80"
request_path = "/"
}
}
variables.tf
variable "applications" {
default = {
"app1" = {
//.....(snipped)...
health_check = "tcp-8008-hc"
//.....(snipped)...
},
"app2" = {
//.....(snipped)...
health_check = "tcp-80-hc"
//.....(snipped)...
},
"app3" = {
//.....(snipped)...
health_check = "tcp-8008-hc"
//.....(snipped)...
},
}
}
main.tf
In this main.tf, I am creating another resource which needs to refer to the health-check based on the value of "health_check" variable of the current loop. I am not sure how do I do that with "each"?
resource "google_compute_backend_service" "backend" {
//.....(snipped)...
for_each = var.applications
health_checks = [google_compute_health_check.each.value["health_check"].id]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
