This document shows you how to deploy a regional internal Application Load Balancer with Cloud Run. To set this up, you use a serverless NEG backend for the load balancer.
Although this document describes a Cloud Run configuration, a serverless NEG for Cloud Run can point to either a Cloud Run resource or a Cloud Run functions (2nd gen) resource.
Before you try this procedure, make sure you are familiar with the following topics:
This document shows you how to configure an Application Load Balancer that proxies requests to a serverless NEG backend.
Serverless NEGs let you use Cloud Run services with your load balancer. After you configure a load balancer with the serverless NEG backend, requests to the load balancer are routed to the Cloud Run backend.
Install the Google Cloud CLI tool. For conceptual and installation information about the gcloud CLI, see gcloud CLI overview.
If you haven't run the gcloud CLI previously, first run
gcloud init to initialize your gcloud CLI
directory.
The instructions on this page assume you already have a Cloud Run service running.
For the example on this page, you can use any of the Cloud Run quickstarts to deploy a Cloud Run service.
The serverless NEG, the load balancer, and any client VMs must be in the same region as the Cloud Run service.To prevent access to the Cloud Run service from the
internet, restrict network ingress to internal. Traffic
from the internal Application Load Balancer is considered internal traffic.
gcloud run deploy CLOUD_RUN_SERVICE_NAME \
--platform=managed \
--allow-unauthenticated \
--ingress=internal \
--region=REGION \
--image=IMAGE_URL
Note the name of the service that you create. The rest of this page shows you how to set up a load balancer that routes requests to this service.
To follow this guide, you need to create a serverless NEG and create a load balancer in a project. You must be either a project owner or editor, or you have the following Compute Engine IAM roles and permissions:
| Task | Required role |
|---|---|
| Create load balancer and networking components | Compute Network Admin (roles/compute.networkAdmin) |
| Create and modify NEGs | Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) |
| Create and modify SSL certificates | Security Admin (roles/iam.securityAdmin) |
To configure the network and its subnets, perform the following tasks:
Create a custom mode VPC network, then the subnets that you want within a region.
In the Google Cloud console, go to the VPC networks page.
Click Create VPC network.
For Name, enter lb-network.
For Subnet creation mode, select Custom.
In the New subnet section, specify the following configuration parameters for a subnet:
lb-subnet.10.1.2.0/24.Click Create.
Create the custom VPC network by using the gcloud compute
networks create command:
gcloud compute networks create lb-network --subnet-mode=custom
Create a subnet in the lb-network network. This example uses an
IP address range of 10.1.2.0/24 for the subnet. You can configure
any valid subnet range.
gcloud compute networks subnets create lb-subnet \
--network=lb-network \
--range=10.1.2.0/24 \
--region=REGION
Create a proxy-only subnet for all regional Envoy-based load
balancers in a
specific region of the lb-network network.
In the Google Cloud console, go to the VPC networks page.
Click the name of the Shared VPC network that you want to add the proxy-only subnet to.
Click Add subnet.
In the Name field, enter proxy-only-subnet.
Select a Region.
Set Purpose to Regional Managed Proxy.
Enter an IP address range as 10.129.0.0/23.
Click Add.
Create the proxy-only subnet by using the gcloud compute networks subnets
create command.
This example uses an IP address range of 10.129.0.0/23 for the
proxy-only subnet. You can configure any valid subnet
range.
gcloud compute networks subnets create proxy-only-subnet \
--purpose=REGIONAL_MANAGED_PROXY \
--role=ACTIVE \
--region=REGION \
--network=lb-network \
--range=10.129.0.0/23
In the following diagram, the load balancer uses a serverless NEG backend to direct requests to a serverless Cloud Run service.
Traffic going from the load balancer to the serverless NEG backends uses special routes defined outside your VPC that aren't subject to firewall rules. Therefore, if your load balancer only has serverless NEG backends, you don't need to create firewall rules to allow traffic from the proxy-only subnet to the serverless backend.
In the Google Cloud console, go to the Load balancing page.
serverless-lb.lb_network.443.For Choose certificate repository, select Classic Certificates.
The following example shows you how to create Compute Engine SSL certificates:
If you want to test this process without setting up an SSL certificate resource, you can set up an HTTP load balancer.
80.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 the Rate limiting overview.
None in the Cloud Armor backend security policy list.1 and 10,000.Routing rules determine how your traffic is directed. You can direct traffic to a backend service or a Kubernetes service. Any traffic not explicitly matched with a host and path matcher is sent to the default service.
gcloud compute network-endpoint-groups create SERVERLESS_NEG_NAME \
--region=REGION \
--network-endpoint-type=serverless \
--cloud-run-service=CLOUD_RUN_SERVICE_NAME
--protocol to
HTTP. This parameter is ignored but it is required because the
--protocol otherwise defaults to TCP.
gcloud compute backend-services create BACKEND_SERVICE_NAME \
--load-balancing-scheme=INTERNAL_MANAGED \
--protocol=HTTP \
--region=REGION
gcloud compute backend-services add-backend BACKEND_SERVICE_NAME \
--region=REGION \
--network-endpoint-group=SERVERLESS_NEG_NAME \
--network-endpoint-group-region=REGION
gcloud compute url-maps create URL_MAP_NAME \
--default-service=BACKEND_SERVICE_NAME \
--region=REGION
You can create either Compute Engine or Certificate Manager certificates. Use any of the following methods to create certificates using Certificate Manager:
Regional Google-managed certificates. Certificate maps aren't supported.
The following types of regional Google-managed certificates are supported by Certificate Manager:
After you create certificates, attach the certificate directly to the target proxy.
To create a regional self-managed SSL certificate resource:
gcloud compute ssl-certificates create SSL_CERTIFICATE_NAME \
--certificate CRT_FILE_PATH \
--private-key KEY_FILE_PATH \
--region=REGION
gcloud compute target-http-proxies create TARGET_HTTP_PROXY_NAME \
--url-map=URL_MAP_NAME \
--region=REGION
gcloud compute target-https-proxies create TARGET_HTTPS_PROXY_NAME \
--ssl-certificates=SSL_CERTIFICATE_NAME \
--url-map=URL_MAP_NAME \
--region=REGION
lb-subnet).
gcloud compute forwarding-rules create HTTP_FORWARDING_RULE_NAME \
--load-balancing-scheme=INTERNAL_MANAGED \
--network=lb-network \
--subnet=lb-subnet \
--target-http-proxy=TARGET_HTTP_PROXY_NAME \
--target-http-proxy-region=REGION \
--region=REGION \
--ports=80
gcloud compute forwarding-rules create HTTPS_FORWARDING_RULE_NAME \
--load-balancing-scheme=INTERNAL_MANAGED \
--network=lb-network \
--subnet=lb-subnet \
--target-https-proxy=TARGET_HTTPS_PROXY_NAME \
--target-https-proxy-region=REGION \
--region=REGION \
--ports=443
Now that you have configured your load balancer, you can start sending traffic to the load balancer's IP address.
This example creates a client VM (vm-client) in the same region as the
load balancer. The client is used to validate the load balancer's configuration
and demonstrate expected behavior.
The client VM can be in any zone in the same region as the load balancer, and it can use any subnet in the same VPC network.
gcloud compute instances create vm-client \
--image-family=debian-12 \
--image-project=debian-cloud \
--tags=allow-ssh \
--network=lb-network \
--subnet=lb-subnet \
--zone=ZONE
This example requires the following firewall rule for the test client VM:
fw-allow-ssh. An ingress rule, applicable to the test client VM,
that allows incoming SSH connectivity on TCP port 22 from any
address. You can choose a more restrictive source IP address range for this
rule; for example, you can specify just the IP address ranges of the system from
which you initiate SSH sessions. This example uses the target tag allow-ssh.
allow-sshlb-networkallow-ssh0.0.0.0/022 for the port number.Create the fw-allow-ssh firewall rule to allow SSH connectivity to
VMs with the network tag allow-ssh. When you omit source-ranges,
Google Cloud interprets the rule to mean any
source.
gcloud compute firewall-rules create fw-allow-ssh \ --network=lb-network \ --action=allow \ --direction=ingress \ --target-tags=allow-ssh \ --rules=tcp:22
It might take a few minutes for the load balancer configuration to propagate after you first deploy it.
Connect to the client instance using SSH.
gcloud compute ssh vm-client \ --zone=ZONE
Verify that the load balancer is serving the Cloud Run service homepage as expected.
For HTTP testing, run:
curl IP_ADDRESS
For HTTPS testing, run:
curl -k -s 'https://TEST_DOMAIN_URL:443' --connect-to TEST_DOMAIN_URL:443:IP_ADDRESS:443
Replace TEST_DOMAIN_URL with the domain associated with your
application. For example, test.example.com.
The -k flag causes curl to skip certificate validation.
This section expands on the configuration example to provide alternative and additional configuration options. All of the tasks are optional. You can perform them in any order.
When creating a serverless NEG, instead of selecting a specific Cloud Run service, you can use a URL mask to point to multiple services serving at the same domain. A URL mask is a template of your URL schema. The serverless NEG uses this template to extract the service name from the incoming request's URL and map the request to the appropriate service.
URL masks are particularly useful if your service is mapped to a custom domain rather than to the default address that Google Cloud provides for the deployed service. A URL mask lets you target multiple services and versions with a single rule even when your application is using a custom URL pattern.
If you haven't already done so, make sure you read the Serverless NEGS overview: URL Masks.
To construct a URL mask for your load balancer, start with the URL of your
service. This example uses a sample serverless app running at
https://example.com/login. This is the URL where the app's login service
is served.
http or https from the URL. You are left with example.com/login.<service>. If the Cloud Run
service has a tag associated with it,
replace the tag name with the placeholder <tag>.
In this example, the URL mask you are left with is example.com/<service>.Optional: If the service name can be
extracted from the path portion of the URL, the domain can be omitted. The
path part of the URL mask is distinguished
by the first slash (/) character. If a slash (/) is not present in the
URL mask, the mask is understood to represent the host only. Therefore, for
this example, the URL mask can be reduced to /<service>.
Similarly, if <service> can be extracted from the host part of the
URL, you can omit the path altogether from the URL mask.
You can also omit any host or subdomain components that come before the first placeholder as well as any path components that come after the last placeholder. In such cases, the placeholder captures the required information for the component.
Here are a few more examples that demonstrate these rules:
This table assumes that you have a custom domain called example.com and
all your Cloud Run services are being mapped
to this domain.
| Service, Tag name | Cloud Run custom domain URL | URL mask |
|---|---|---|
| service: login | https://login-home.example.com/web | <service>-home.example.com |
| service: login | https://example.com/login/web | example.com/<service> or /<service> |
| service: login, tag: test | https://test.login.example.com/web | <tag>.<service>.example.com |
| service: login, tag: test | https://example.com/home/login/test | example.com/home/<service>/<tag> or /home/<service>/<tag> |
| service: login, tag: test | https://test.example.com/home/login/web | <tag>.example.com/home/<service> |
For a new load balancer, you can use the same end-to-end process as described previously in this document. When configuring the backend service, instead of selecting a specific service, enter a URL mask.
If you have an existing load balancer, you can edit the backend configuration and have the serverless NEG point to a URL mask instead of a specific service.
To add a URL mask-based serverless NEG to an existing backend service, do the following:helloworld-serverless-neg.To create a serverless NEG with a sample URL mask of
example.com/<service>:
gcloud compute network-endpoint-groups create SERVERLESS_NEG_MASK_NAME \
--region=REGION \
--network-endpoint-type=serverless \
--cloud-run-url-mask="example.com/<service>"
To update the client HTTP keepalive timeout, use the following instructions.
In the Google Cloud console, go to the Load balancing page.
For an HTTP load balancer, update the target HTTP proxy by using the
gcloud compute target-http-proxies update command.
gcloud compute target-http-proxies update TARGET_HTTP_PROXY_NAME \
--http-keep-alive-timeout-sec=HTTP_KEEP_ALIVE_TIMEOUT_SEC \
--region=REGION
For an HTTPS load balancer, update the target HTTPS proxy by using the
gcloud compute target-https-proxies update command.
gcloud compute target-https-proxies update TARGET_HTTP_PROXY_NAME \
--http-keep-alive-timeout-sec=HTTP_KEEP_ALIVE_TIMEOUT_SEC \
--region REGION
Replace the following:
TARGET_HTTP_PROXY_NAME: the name of the target HTTP proxy.TARGET_HTTPS_PROXY_NAME: the name of the target HTTPS proxy.HTTP_KEEP_ALIVE_TIMEOUT_SEC: the HTTP keepalive timeout value
from 5 to 600 seconds.A network endpoint group cannot be deleted if it is attached to a backend service. Before you delete a NEG, ensure that it is detached from the backend service.
To remove a serverless NEG from a backend service, you must specify the region where the NEG was created.
gcloud compute backend-services remove-backend BACKEND_SERVICE_NAME \
--network-endpoint-group=SERVERLESS_NEG_NAME \
--network-endpoint-group-region=REGION \
--region=REGION
To delete the serverless NEG:
gcloud compute network-endpoint-groups delete SERVERLESS_NEG_NAME \
--region=REGION
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-06-09 UTC.