Reputation: 1389
I created a Global(multi-region) TCP(proxy) LB on GCP console using
Now same am unable to create with Terraform below is my full code:
Problem: The loadbalancer is not creating with name: in google_compute_target_tcp_proxy its creating with the name: only in google_compute_backend_service if only one backend is passed, If I pass multiple backends passed with count then multiple loadbalancers are creating instead of attaching all backends to one loadbalancer. Can any one suggest how to attach multiple backends to single google_compute_target_tcp_proxy ? Am new to terraform and I didnt find any details in Terraform documentation.
provider "google" {
credentials = file(var.credentials_file)
project = var.project_id
}
provider "google-beta" {
credentials = file(var.credentials_file)
project = var.project_id
}
resource "google_compute_global_forwarding_rule" "default" {
#count = length(var.zones)
name = "frontend-service-mig-test" #We can have single FE IP
#target = google_compute_target_tcp_proxy.default[count.index].id
target = google_compute_target_tcp_proxy.default.id
port_range = "443"
load_balancing_scheme = "EXTERNAL"
}
resource "google_compute_target_tcp_proxy" "default" {
#count = length(var.zones)
name = "test-proxy" # This name wont be visible on gui.
#backend_service = google_compute_backend_service.default[count.index].id
backend_service = google_compute_backend_service.default.id
}
resource "google_compute_backend_service" "default" {
count = length(var.zones)
name = "mig-test-${count.index}-backend-service"
load_balancing_scheme = "EXTERNAL"
protocol = "TCP"
timeout_sec = 10
port_name = "https"
health_checks = [google_compute_health_check.default.id]
backend {
#group = "https://www.googleapis.com/compute/v1/projects/terraform-playground-301207/zones/northamerica-northeast1-a/instanceGroups/mig-test-0"
group = "https://www.googleapis.com/compute/v1/projects/terraform-playground-301207/zones/${var.zones[count.index]}/instanceGroups/mig-test-${count.index}"
balancing_mode = "UTILIZATION"
capacity_scaler = 1
max_utilization = 0.8
}
}
resource "google_compute_health_check" "default" {
count = length(var.zones)
provider = google-beta
name = "health-check-mig-test-${count.index}"
timeout_sec = 5
check_interval_sec = 5
healthy_threshold = 2
unhealthy_threshold = 2
log_config {
enable = false
}
tcp_health_check {
port = "443"
}
}
Upvotes: 1
Views: 729
Reputation: 1389
Solution is adding multiple backends to backend resource itself , i used dynamic feature for same.
resource "google_compute_global_forwarding_rule" "default" {
name = var.fe_name #We can have single FE IP
target = google_compute_target_tcp_proxy.default.id
port_range = var.be_protocol_range
load_balancing_scheme = var.lb_scheme
}
resource "google_compute_target_tcp_proxy" "default" {
name = var.loadbalancername # This name wont be visible on gui.
backend_service = google_compute_backend_service.default.id
}
resource "google_compute_backend_service" "default" {
name = var.loadbalancername
load_balancing_scheme = var.lb_scheme
protocol = var.be_protocol
timeout_sec = var.be_timeout_sec
port_name = var.be_protocol_name
health_checks = [google_compute_health_check.default.id]
dynamic backend {
for_each = var.zones
content {
group = "https://www.googleapis.com/compute/v1/projects/terraform-playground-301207/zones/${backend.value}/instanceGroups/${var.appname}-${var.regions[backend.key]}"
balancing_mode = var.be_balancing_mode
capacity_scaler = var.be_capacity_scaler
max_utilization = var.be_max_utilization
}
}
}
resource "google_compute_health_check" "default" {
provider = google-beta
name = var.be_healthcheck_name
timeout_sec = var.be_healthcheck_timeout_sec
check_interval_sec = var.be_healthcheck_interval_sec
healthy_threshold = var.be_healthcheck_threshold
unhealthy_threshold = var.be_unhealthycheck_threshold
log_config {
enable = var.be_healthycheck_logconfig
}
tcp_health_check {
port = var.be_healthycheck_port
}
}
Upvotes: 1