Set up a classic Application Load Balancer with a managed instance group backend Stay organized with collections Save and categorize content based on your preferences.
For general concepts, see theExternal Application Load Balancer overview.
If you are an existing user of the classic Application Load Balancer, make sure that youreviewMigrationoverview when you plan a new deployment with the global external Application Load Balancer.
Load balancer topologies
For an HTTPS load balancer, you create the configuration shown inthe following diagram.
For an HTTP load balancer, you create the configuration shown inthe following diagram.
The sequence of events in the diagrams are as follows:
- A client sends a content request to the external IPv4 address defined in theforwarding rule.
For an HTTPS load balancer, the forwarding rule directs the request to thetarget HTTPS proxy.
For an HTTP load balancer, the forwarding rule directs the request to thetarget HTTP proxy.
The target proxy uses the rule in theURLmap to determine that thesingle backend service receives all requests.
The load balancer determines that thebackendservice has only one instancegroup and directs the request to a virtual machine (VM) instance in thatgroup.
The VM serves the content requested by the user.
Before you begin
Complete the following steps before you create the load balancer.
Set up an SSL certificate resource
For an HTTPS load balancer, create an SSL certificate resource as described in thefollowing:
We recommend using a Google-managed certificate.
This example assumes that you already have an SSL certificate resource namedwww-ssl-cert.
Set up permissions
To complete the steps in this guide, you must have permission to createCompute Engine instances, firewall rules, and reserved IP addresses in aproject. You must have either a projectowner or editorrole, or you must have the followingCompute Engine IAM roles.
| Task | Required role |
|---|---|
| Create instances | Instance Admin |
| Add and remove firewall rules | Security Admin |
| Create load balancer components | Network Admin |
| Create a project (optional) | Project Creator |
For more information, see the following guides:
Optional: Use BYOIP addresses
With bring your own IP (BYOIP), you can import your own public addresses toGoogle Cloud to use the addresses with Google Cloud resources. Forexample, if you import your own IPv4 addresses, you can assign one to theforwarding rule when you configure your load balancer. When you follow theinstructions in this document toset up the load balancer, provide the BYOIP address as theIP address.
For more information about using BYOIP, seeBring your own IP addresses.
Configure the network and subnets
To create the example network and subnet, follow these steps.
Console
In the Google Cloud console, go to theVPC networks page.
ClickCreate VPC network.
Enter aName for the network.
For theSubnet creation mode, chooseCustom.
In theNew subnet section, configure the following fields:
- Provide aName for the subnet.
- Select aRegion.
- ForIP stack type, selectIPv4 (single-stack).
- Enter anIP address range. This is theprimary IPv4range for the subnet.
ClickDone.
To add a subnet in a different region, clickAdd subnet and repeatthe previous steps.
ClickCreate.
gcloud
Create the custom mode VPC network:
gcloud compute networks createNETWORK \ --subnet-mode=custom
Within the network, create a subnet for backends:
gcloud compute networks subnets createSUBNET \ --network=NETWORK \ --stack-type=IPV4_ONLY \ --range=10.1.2.0/24 \ --region=REGION
Replace the following:
NETWORK: a name for the VPC network.SUBNET: a name for the subnet.REGION: the name of the region.
Create a managed instance group
To set up a load balancer with a Compute Engine backend, your VMs needto be in an instance group. This guide describes how to create a managedinstance group with Linux VMs that have Apache running, and then set up loadbalancing. A managed instance group creates each of its managed instances basedon the instance templates that you specify.
The managed instance group provides VMs running the backend servers ofan external HTTP(S) load balancer. For demonstration purposes, backendsserve their own hostnames.
Before you create a managed instance group, create an instance template.
Console
To supportIPv4 traffic, use the following steps:
In the Google Cloud console, go to theInstance templates page.
ClickCreate instance template.
ForName, enter
lb-backend-template.Ensure that the Boot disk is set to a Debian image, such asDebian GNU/Linux 10 (buster). These instructions use commands thatare only available on Debian, such as
apt-get.ExpandAdvanced options.
ExpandNetworking and configure the following fields:
- ForNetwork tags, enter
allow-health-check. - In theNetwork interfaces section, clickEdit and make thefollowing changes:
- Network:
NETWORK - Subnet:
SUBNET - IPv4 traffic:IPv4 (single-stack)
- Network:
- ClickDone.
- ForNetwork tags, enter
ExpandManagement. In theStartup script field, enter thefollowing script:
#! /bin/bashapt-get updateapt-get install apache2 -ya2ensite default-ssla2enmod sslvm_hostname="$(curl -H "Metadata-Flavor:Google" \http://metadata.google.internal/computeMetadata/v1/instance/name)"echo "Page served from: $vm_hostname" | \tee /var/www/html/index.htmlsystemctl restart apache2
ClickCreate.
gcloud
To supportIPv4 traffic, run the following command:
gcloud compute instance-templates createTEMPLATE_NAME \ --region=REGION \ --network=NETWORK \ --subnet=SUBNET \ --stack-type=IPV4_ONLY \ --tags=allow-health-check \ --image-family=debian-10 \ --image-project=debian-cloud \ --metadata=startup-script='#! /bin/bash apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://metadata.google.internal/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html systemctl restart apache2'
Terraform
To create the instance template, use thegoogle_compute_instance_templateresource.
resource "google_compute_instance_template" "default" { name = "lb-backend-template" disk { auto_delete = true boot = true device_name = "persistent-disk-0" mode = "READ_WRITE" source_image = "projects/debian-cloud/global/images/family/debian-11" type = "PERSISTENT" } labels = { managed-by-cnrm = "true" } machine_type = "n1-standard-1" metadata = { startup-script = "#! /bin/bash\n sudo apt-get update\n sudo apt-get install apache2 -y\n sudo a2ensite default-ssl\n sudo a2enmod ssl\n vm_hostname=\"$(curl -H \"Metadata-Flavor:Google\" \\\n http://169.254.169.254/computeMetadata/v1/instance/name)\"\n sudo echo \"Page served from: $vm_hostname\" | \\\n tee /var/www/html/index.html\n sudo systemctl restart apache2" } network_interface { access_config { network_tier = "PREMIUM" } network = "global/networks/default" subnetwork = "regions/us-east1/subnetworks/default" } region = "us-east1" scheduling { automatic_restart = true on_host_maintenance = "MIGRATE" provisioning_model = "STANDARD" } service_account { email = "default" scopes = ["https://www.googleapis.com/auth/devstorage.read_only", "https://www.googleapis.com/auth/logging.write", "https://www.googleapis.com/auth/monitoring.write", "https://www.googleapis.com/auth/pubsub", "https://www.googleapis.com/auth/service.management.readonly", "https://www.googleapis.com/auth/servicecontrol", "https://www.googleapis.com/auth/trace.append"] } tags = ["allow-health-check"]}Create the managed instance group and select the instance template.
Console
In the Google Cloud console, go to theInstance groups page.
ClickCreate instance group.
On the left, chooseNew managed instance group (stateless).
ForName, enter
lb-backend-example.UnderLocation, selectSingle zone.
ForRegion, select your preferred region.
ForZone, select a zone.
UnderInstance template, select the instance template
lb-backend-template.ForAutoscaling mode, selectOn: add and remove instances to thegroup.
SetMinimum number of instances to
2, and setMaximum number ofinstances to2or more.To create the new instance group, clickCreate.
gcloud
Create the managed instance group based on the template.
gcloud compute instance-groups managed create lb-backend-example \ --template=TEMPLATE_NAME --size=2 --zone=ZONE_A
Terraform
To create the managed instance group, use thegoogle_compute_instance_group_managerresource.
resource "google_compute_instance_group_manager" "default" { name = "lb-backend-example" zone = "us-east1-b" named_port { name = "http" port = 80 } version { instance_template = google_compute_instance_template.default.id name = "primary" } base_instance_name = "vm" target_size = 2}To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
Add a named port to the instance group
For your instance group, define an HTTP service and map a port nameto the relevant port. The load balancing service forwardstraffic to the named port. For more information, seeNamed ports.
Console
In the Google Cloud console, go to theInstance groups page.
Clicklb-backend-example.
On the instance group'sOverview page, clickEdit.
In thePort mapping section, clickAdd port.
- For the port name, enter
http. For the port number, enter80.
- For the port name, enter
ClickSave.
gcloud
Use thegcloud compute instance-groupsset-named-portscommand.
gcloud compute instance-groups set-named-ports lb-backend-example \ --named-ports http:80 \ --zoneZONE_A
Terraform
Thenamed_port attribute is included in themanaged instance group sample.
Configure a firewall rule
In this example, you create thefw-allow-health-check firewall rule.This is an ingress rule that allows traffic from the Google Cloud healthchecking systems (130.211.0.0/22 and35.191.0.0/16). This example uses thetarget tagallow-health-check to identify the VMs.
Console
In the Google Cloud console, go to theFirewall policies page.
ClickCreate firewall rule to create the firewall rule.
ForName, enter
fw-allow-health-check.Select aNetwork.
UnderTargets, selectSpecified target tags.
Populate theTarget tags field with
allow-health-check.SetSource filter toIPv4 ranges.
SetSource IPv4 ranges to
130.211.0.0/22and35.191.0.0/16.UnderProtocols and ports, selectSpecified protocols and ports.
Select theTCP checkbox, and then type
80for the portnumbers.ClickCreate.
gcloud
gcloud compute firewall-rules create fw-allow-health-check \ --network=NETWORK \ --action=allow \ --direction=ingress \ --source-ranges=130.211.0.0/22,35.191.0.0/16 \ --target-tags=allow-health-check \ --rules=tcp:80
Terraform
To create the firewall rule, use thegoogle_compute_firewallresource.
resource "google_compute_firewall" "default" { name = "fw-allow-health-check" direction = "INGRESS" network = "global/networks/default" priority = 1000 source_ranges = ["130.211.0.0/22", "35.191.0.0/16"] target_tags = ["allow-health-check"] allow { ports = ["80"] protocol = "tcp" }}To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
Reserve an external IP address
Now that your instances are up and running, set up aglobal static external IPaddress that your customers useto reach your load balancer.
Console
In the Google Cloud console, go to theExternal IP addresses page.
To reserve an IPv4 address, clickReserve external static IP address.
ForName, enter
lb-ipv4-1.SetNetwork Service Tier toPremium.
SetIP version toIPv4.
SetType toGlobal.
ClickReserve.
gcloud
gcloud compute addresses create lb-ipv4-1 \ --ip-version=IPV4 \ --network-tier=PREMIUM \ --global
Note the IPv4 address that was reserved:
gcloud compute addresses describe lb-ipv4-1 \ --format="get(address)" \ --global
Terraform
To reserve the IP address, use thegoogle_compute_global_addressresource.
resource "google_compute_global_address" "default" { name = "lb-ipv4-1" ip_version = "IPV4"}To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
Set up the load balancer
In this example, you are using HTTPS (frontend) between the client and the loadbalancer. For HTTPS, you need one or moreSSL certificate resourcesto configure the proxy. We recommend using a Google-managed certificate.
Even if you're using HTTPS on the frontend, you can use HTTP on the backend.Googleautomatically encrypts traffic between Google Front Ends (GFEs) and yourbackendsthat reside within Google Cloud VPC networks.
Console
Select the load balancer type
In the Google Cloud console, go to theLoad balancing page.
- ClickCreate load balancer.
- ForType of load balancer, selectApplication Load Balancer (HTTP/HTTPS) and clickNext.
- ForPublic facing or internal, selectPublic facing (external) and clickNext.
- ForGlobal or single region deployment, selectBest for global workloads and clickNext.
- ForLoad balancer generation, selectClassic Application Load Balancer and clickNext.
- ClickConfigure.
Basic configuration
For the load balancerName, enter something likeweb-map-https orweb-map-http.
Frontend configuration
- ClickFrontend configuration.
- SetProtocol toHTTPS.
- Select
IPv4for IPv4 traffic. SetIP address tolb-ipv4-1, which you created earlier. - SetPort to443.
- ClickCertificate, and select your primary SSL certificate.
- Optional: Create an SSL policy:
- In theSSL policy list, selectCreate a policy.
- Set the name of the SSL policy to
my-ssl-policy. - ForMinimum TLS Version, selectTLS 1.0.
- ForProfile, selectModern. TheEnabled features andDisabled features are displayed.
- ClickSave.
- Optional: Select theEnable HTTP to HTTPS Redirect checkbox to enable redirects.
Enabling this checkbox creates an additional partial HTTP load balancer that uses the same IP address as your HTTPS load balancer and redirects incoming HTTP requests to your load balancer's HTTPS frontend.
This checkbox can only be selected when the HTTPS protocol is selected and a reserved IP address is used.
- ClickDone.
Backend configuration
- ClickBackend configuration.
- UnderBackend services & backend buckets, selectCreate a backend service.
- Add a name for your backend service, such as
web-backend-service. - Go to theSecurity section, and then selectEnable IAP to secure access to your applications.
Cloud CDN and IAP are not compatible. If you have Cloud CDN enabled, and you select to enable IAP, Cloud CDN is automatically disabled.
Optional: Configure a default backend security policy. The default security policy throttles traffic over a user-configured threshold. For more information about default security policies, see theRate limiting overview.
- To opt out of the Cloud Armor default security policy, select
Nonein theCloud Armor backend security policy list. - To configure the Cloud Armor default security policy, selectDefault security policy in theCloud Armor backend security policy list.
- In thePolicy name field, accept the automatically generated name or enter a name for your security policy.
- In theRequest count field, accept the default request count or enter an integer between
1and10,000. - In theInterval field, select an interval.
- In theEnforce on key field, choose one of the following values:All,IP address, orX-Forwarded-For IP address. For more information about these options, seeIdentifying clients for rate limiting.
- To opt out of the Cloud Armor default security policy, select
- Retain the other default settings.
- ClickCreate.
Host and path rules
ForHost and path rules, retain the default settings.
Review and finalize
- ClickReview and finalize.
- Review your load balancer configuration settings.
- Optional: ClickEquivalent code to view the REST API request that will be used to create the load balancer.
- ClickCreate.
Wait for the load balancer to be created.
If you created an HTTPS load balancer and selected theEnable HTTP to HTTPS Redirect checkbox, you will also seean HTTP load balancer created with a-redirect suffix.
- Click the name of the load balancer.
- On theLoad balancer details screen, note theIP:Port for your load balancer.
gcloud
- Create a health check.
gcloud compute health-checks create http http-basic-check \ --port 80
- Create a backend service.
gcloud compute backend-services create web-backend-service \ --load-balancing-scheme=EXTERNAL \ --protocol=HTTP \ --port-name=http \ --health-checks=http-basic-check \ --global
- Add your instance group as the backend to the backend service.
gcloud beta compute backend-services add-backend web-backend-service \ --instance-group=lb-backend-example \ --instance-group-zone=ZONE_A \ --global
- For HTTP, create a URL map to route the incoming requests to the default backendservice.
gcloud beta compute url-maps create web-map-http \ --default-service web-backend-service
- For HTTPS, create a URL map to route the incoming requests to thedefault backend service.
gcloud beta compute url-maps create web-map-https \ --default-service web-backend-service
Set up an HTTPS frontend
Skip this section for HTTP load balancers.
- For HTTPS, if you haven't already done so, create the global SSLcertificate resource, as shown in the following sections:
For HTTPS, create a target HTTPS proxy to route requests to your URL map. The proxy is the portion of the load balancer that holds the SSL certificatefor an HTTPS load balancer, so you also load your certificate in this step.
gcloud compute target-https-proxies create https-lb-proxy \ --url-map=web-map-https \ --ssl-certificates=www-ssl-cert
- For HTTPS, create a global forwarding rule to route incoming requests tothe proxy.
gcloud compute forwarding-rules create https-content-rule \ --load-balancing-scheme=EXTERNAL \ --network-tier=PREMIUM \ --address=lb-ipv4-1 \ --global \ --target-https-proxy=https-lb-proxy \ --ports=443
- Optional: For HTTPS, create a global SSL policy and attach it to the HTTPS proxy.
To create a global SSL policy: To attach the SSL policy to the global target HTTPS proxy:gcloud compute ssl-policies create my-ssl-policy \ --profile MODERN \ --min-tls-version 1.0
gcloud compute target-https-proxies update https-lb-proxy \ --ssl-policy my-ssl-policy
Set up an HTTP frontend
Skip this section for HTTPS load balancers.
- For HTTP, create a target HTTP proxy to route requests to your URL map.
gcloud compute target-http-proxies create http-lb-proxy \ --url-map=web-map-http
- For HTTP, create a global forwarding rule to route incoming requests to the proxy.
gcloud compute forwarding-rules create http-content-rule \ --load-balancing-scheme=EXTERNAL \ --address=lb-ipv4-1 \ --global \ --target-http-proxy=http-lb-proxy \ --ports=80
Terraform
To create the health check, use the
google_compute_health_checkresource.resource "google_compute_health_check" "default" { name = "http-basic-check" check_interval_sec = 5 healthy_threshold = 2 http_health_check { port = 80 port_specification = "USE_FIXED_PORT" proxy_header = "NONE" request_path = "/" } timeout_sec = 5 unhealthy_threshold = 2}To create the backend service, use the
google_compute_backend_serviceresource.This example uses
load_balancing_scheme="EXTERNAL_MANAGED", which sets up a global external Application Load Balancer withadvanced traffic management capability. To create a classic Application Load Balancer, make sure you change theload_balancing_schemetoEXTERNALbefore running the script.resource "google_compute_backend_service" "default" { name = "web-backend-service" connection_draining_timeout_sec = 0 health_checks = [google_compute_health_check.default.id] load_balancing_scheme = "EXTERNAL_MANAGED" port_name = "http" protocol = "HTTP" session_affinity = "NONE" timeout_sec = 30 backend { group = google_compute_instance_group_manager.default.instance_group balancing_mode = "UTILIZATION" capacity_scaler = 1.0 }}To create the URL map, use the
google_compute_url_mapresource.resource "google_compute_url_map" "default" { name = "web-map-http" default_service = google_compute_backend_service.default.id}To create the target HTTP proxy, use the
google_compute_target_http_proxyresource.resource "google_compute_target_http_proxy" "default" { name = "http-lb-proxy" url_map = google_compute_url_map.default.id}To create the forwarding rule, use the
google_compute_global_forwarding_ruleresource.This example uses
load_balancing_scheme="EXTERNAL_MANAGED", which sets up a global external Application Load Balancer withadvanced traffic management capability. To create a classic Application Load Balancer, make sure you change theload_balancing_schemetoEXTERNALbefore running the script.resource "google_compute_global_forwarding_rule" "default" { name = "http-content-rule" ip_protocol = "TCP" load_balancing_scheme = "EXTERNAL_MANAGED" port_range = "80-80" target = google_compute_target_http_proxy.default.id ip_address = google_compute_global_address.default.id}
To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.
Connect your domain to your load balancer
After the load balancer is created, note the IP address that is associated withthe load balancer—for example,30.90.80.100. To point your domain to yourload balancer, create anA record by using your domain registration service. Ifyou added multiple domains to your SSL certificate, you must add anA recordfor each one, all pointing to the load balancer's IP address. For example, tocreateA records forwww.example.com andexample.com, use the following:
NAME TYPE DATAwww A 30.90.80.100@ A 30.90.80.100
If you use Cloud DNS as your DNS provider, seeAdd, modify, and delete records.
Test traffic sent to your instances
Now that the load balancing service is running, you can sendtraffic to the forwarding rule and watch the traffic be dispersed to differentinstances.
Console
In the Google Cloud console, go to theLoad balancing page.
- Click the load balancer that you just created.
In theBackend section, confirm that the VMs are healthy. TheHealthy column should be populated, indicating that both VMsare healthy (
2/2). If you see otherwise, first try reloadingthe page. It can take a few moments for the Google Cloud console toindicate that the VMs are healthy. If the backends do not appear healthyafter a few minutes, review the firewall configuration and the network tagassigned to your backend VMs.- For HTTPS, if you are using a Google-managed certificate, confirm that yourcertificate resource's status is ACTIVE. For more information, seeGoogle-managed SSL certificate resource status.
- After the Google Cloud console shows that the backend instances arehealthy, you can test your load balancer using a web browser by going to
https://IP_ADDRESS(orhttp://IP_ADDRESS). ReplaceIP_ADDRESSwith theload balancer's IP address. - If you used a self-signed certificate for testing HTTPS, your browserdisplays a warning. You must explicitly instruct your browser to accept a self-signed certificate.
- Your browser should render a page with content showing the name of the instance that served the page, along with its zone (for example,
Page served from: lb-backend-example-xxxx). If your browser doesn't render this page, review the configuration settings in this guide.
gcloud
gcloud compute addresses describe lb-ipv4-1 \ --format="get(address)" \ --global
After a few minutes have passed, you can test the setup by running the followingcurl command.
curl http://IP_ADDRESS
-OR-
curl https://HOSTNAME
What's next
- To set up an HTTP-to-HTTPS redirect for your classic Application Load Balancer,seeSetting up an HTTP-to-HTTPS redirect forclassic Application Load Balancers
- For a complex example with IPv6 and multi-region loadbalancing, seeSetting up an HTTPS loadbalancer.
- Clean up the load balancer setup.
- To enable IAP, seeEnabling IAP on theexternal Application Load Balancer.
- To use a Terraform module, seeTerraform module examples forexternal Application Load Balancers.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.