Automating the backend components of an internal HTTP Load Balancer

Hi all,

I'm working on a scenario where the networking team wants to decouple the creation of the load balancer (internal HTTP Load Balancer) itself and its backend components so that once the Load Balancer is created it keeps the same (static) and the team can create only the backend services and NEGs and reference/attach to the existing internal HTTP Load Balancer.

Is it possible to automate only the backend services of an HTTP Load Balancer instance? For example, having a Terraform module only for backends.
Or whenever necessary, would we have to add the backends to the same module that creates the other Load Balancer  components (forwarding rule, target proxy, URL map, etc)?

Thanks in advance for any help provided.

5 6 221
6 REPLIES 6

Hi @brunosilva311,

Do you mean to say that there is already a load balancer existing prior to the creation of the backend? Or you mean otherwise?

You can decouple the creation of the load balancer and its backend components (such as Compute Engine instance, functions, services) by:

  1. First, creating backend components and then creating the load balancer after.
  2. You can then attach to the load balancer so that it can the distribute the incoming traffic. 
  3. Make sure that you configure the health checks to monitor the health of your backend services.

It is also possible to automate this process using a Terraform module.

It's possible to use GCP Navite tools such as Google Cloud Deployment Manager, Google Cloud SDK(gcloud), Google Cloud Client Libraries to automate this process. 

Let me know if this is helpful.

Hello @Rfelizardo 

Thank you for your response.

In this case, I mean an already existing Load Balancer.
So the idea is to have a Terraform module to create only the backend services and related components and attach them to the existing Load balancer (decoupling the backends from the load balancer).
I haven't found a way to reference the Load Balancer in the backend service resource in Terraform.
Is there any way to achieve that?

Thank you

You can use the data source referencing of the existing load balancer in your configuration.
This would then allow you to retrieve the information of your load balancer and then use the information of the load balancer in your backend service configuration.

Here are some sample configuration you might want to use as reference:

 

# Define data source to reference existing load balancer
data "google_compute_forwarding_rule" "existing_forwarding_rule" {
  name        = "your-existing-forwarding-rule-name"
  target      = "your-existing-target-pool-self-link"
}

# Define backend service resource
resource "google_compute_backend_service" "backend_service" {
  name        = "your-backend-service-name"
  # Other configuration for your backend service...

  # Attach backend service to the existing load balancer
  backend {
    group = "your-backend-group-self-link"
  }

  # Use existing forwarding rule
  backend {
    group   = google_compute_backend_service.backend_service.self_link
    balancing_mode = "UTILIZATION"
    max_utilization = 0.8
    capacity_scaler = 1.0
    timeout_sec = 10
  }

  # Reference existing forwarding rule
  forwarding_rule {
    target = data.google_compute_forwarding_rule.existing_forwarding_rule.name
  }
}

 

Let me know if this helps you. 

 

Thank you for the instructions.

I tried it but got an error stating that the forwarding_rule block argument is not supported/expected in the google_compute_backend_service resource.

brunosilva311_0-1714000872572.png

Good day @brunosilva311 ,

Have you checked your blocks if you've missed any closing brace or opening brace? If your issue persists, I would suggest logging an issue to Terraform. They can help you in creating a configuration file to tailor your needs.

Hope this helps you. Thank you

Hi @Rfelizardo,

Appreciate your help.

And yes, I checked it. I also checked in the Terraform documentation that neither the resource google_compute_backend_service nor google_compute_region_backend_service has no forwarding_rule config block.

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_backend_servi...

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_region_backen... 


So I'm still trying to figure out if it's possible to make this reference from the backend to the load balancer.