How to set up a custom domain and HTTPS redirects on Cloud Run
I'll show you how to set up a custom domain for a Cloud Run service using Cloud Load
Balancing instead of the default *.run.app URL. Other options include
Firebase Hosting or a third-party CDN (Cloud Run's built-in domain mapping is in preview
and not recommended for production).
An external Application Load Balancer doesn't have a free tier, but it lets you add Cloud CDN caching, Google Cloud Armor, URL routing across multiple services, and Google-managed SSL certificates.
Here's the architecture you're going to build. First, enable the required APIs, reserve a static IP, and update your DNS records. Then, start the provisioning process of a managed SSL certificate. While that's running, deploy a Cloud Run service, and set up the load balancer in front of it, including injected HSTS headers and the HTTP-to-HTTPS redirect.
Prerequisites
Before you start, make sure you have:
- A domain name you control.
gcloudCLI installed and authenticated.- A Google Cloud project with an attached billing account.
1. Enable the required APIs
First, set your Google Cloud project ID and domain name as environment variables in your
shell. DOMAIN_NAME can be either an apex domain (example.com)
or a subdomain (app.example.com):
export PROJECT_ID="YOUR_PROJECT_ID"
export DOMAIN_NAME="example.com"
Enable the Compute Engine (for the load balancer) and Cloud Run APIs in your Google Cloud project:
gcloud services enable compute.googleapis.com run.googleapis.com \
--project=$PROJECT_ID
Operation "operations/acf.p2-1234567890-e0583fe5-c5b0-4007-a6c8-dadf54698f09" finished successfully.
2. Reserve a static IP and update DNS
The next step is to reserve a global static IP address and point your domain to it.
Reserve a static IP address
Create a global static external IPv4 address named lb-ip:
gcloud compute addresses create lb-ip \
--global \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/addresses/lb-ip].
Retrieve your reserved IP address:
gcloud compute addresses describe lb-ip \
--global \
--format='get(address)' \
--project=$PROJECT_ID
136.68.xxx.xxx
Update your DNS
Go to your DNS provider and point an A record for
$DOMAIN_NAME (using @ for an apex domain like
example.com, or the subdomain prefix like app for
app.example.com) to the static IP address you reserved. Pointing your
domain to the load balancer's IP proves to Google that you control the domain, allowing
Google Cloud to issue the certificate.
Note that Google's validation service needs to reach your load balancer's IP address directly. Intermediate layers such as a third-party CDN can cause the certificate provisioning to fail.
If load balancer authorization doesn't work for you, there's an alternative way to prove ownership that requires creating a CNAME record for validation but offers more flexibility for complex configurations.
3. Provision a managed SSL certificate
To secure your traffic with HTTPS, you need an SSL certificate. Google Cloud handles provisioning and renewal for you:
gcloud compute ssl-certificates create lb-cert \
--domains=$DOMAIN_NAME \
--global \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/sslCertificates/lb-cert].
NAME TYPE CREATION_TIMESTAMP EXPIRE_TIME REGION MANAGED_STATUS
lb-cert MANAGED 2026-10-27T09:32:33.026-07:00 PROVISIONING
example.com: PROVISIONING
The certificate stays in a PROVISIONING state until the load balancer is
live (you'll create it soon) and DNS is propagated. It typically takes up to 60 minutes
to become ACTIVE once the configuration is complete.
4. Deploy an example Cloud Run service
Now, deploy a web service. I'm using the official NGINX image as an example here. It's a web server, which works great to prove this configuration works. You can deploy your own app later.
Unlike the global static IP address and SSL certificate you created earlier, Cloud Run
services are regional. Pick a region close to your users (this example uses
europe-west1 in Belgium) and use that same region when you create the
serverless network endpoint group in the next step:
gcloud run deploy run-service \
--image=nginx \
--region=europe-west1 \
--no-invoker-iam-check \
--ingress=internal-and-cloud-load-balancing \
--port=80 \
--project=$PROJECT_ID
Deploying container to Cloud Run service [run-service] in project [YOUR_PROJECT_ID] region [europe-west1]
Deploying new service... Done.
Creating Revision...done
Routing traffic...done
Done.
Service [run-service] revision [run-service-00001-jrm] has been deployed and is serving 100 percent of traffic.
Service URL: https://run-service-1234567890.europe-west1.run.app
-
--no-invoker-iam-checkdisables the Cloud Run Invoker Identity and Access Management (IAM) check so the service accepts public requests without modifying IAM policies (unlike--allow-unauthenticated, this works even in organizations that restrict what members can go in an IAM policy). -
--port=80tells Cloud Run to send requests to port 80 (where the NGINX container listens) instead of the default port 8080. -
--ingress=internal-and-cloud-load-balancingblocks external requests to the default*.run.appservice URL (even thoughgcloudstill prints aService URLin the output) so traffic can only enter through the load balancer or from internal sources on your Virtual Private Cloud (VPC) network.
5. Configure a serverless backend
A serverless backend is a global resource that connects Cloud Load Balancing to your regional Cloud Run service. In this section, you create a global backend service, wrap the Cloud Run service in a regional serverless network endpoint group (NEG), attach the NEG to the backend service, and create a URL map to route incoming requests.
Set up the backend service
The backend service is a central configuration point where you enable features like Cloud CDN caching, Cloud Armor, and custom headers.
gcloud compute backend-services create backend-service \
--load-balancing-scheme=EXTERNAL_MANAGED \
--custom-response-header='Strict-Transport-Security: max-age=31536000; includeSubDomains; preload' \
--global \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/backendServices/backend-service].
NAME BACKENDS PROTOCOL
backend-service HTTP
The --load-balancing-scheme=EXTERNAL_MANAGED flag selects Google Cloud's
global external Application Load Balancer (rather than the legacy
EXTERNAL classic load balancer). The
--custom-response-header flag instructs the load balancer to automatically
inject the Strict-Transport-Security HTTP header into all responses sent
back to the client. This security header tells browsers to never use HTTP to make
requests, preventing
downgrade attacks.
Notice that the command omits the --protocol flag. When configuring an
HTTPS load balancer, it's tempting to pass --protocol=HTTPS. However,
backend services attached to serverless NEGs don't support port names. Passing
--protocol=HTTPS sets a default port name (https), which
causes gcloud compute backend-services add-backend to fail when you attach
the serverless NEG:
ERROR: (gcloud.compute.backend-services.add-backend) Could not fetch resource:
- Invalid value for field 'resource.portName': 'https'. Port name is not supported for a
backend service with Serverless network endpoint groups.
Omit --protocol so the backend service uses its default setting without
assigning a port name. Google Cloud's internal network still encrypts traffic between
the load balancer and Cloud Run.
Create a serverless network endpoint group
Now that you have created a backend service, you can connect the Cloud Run service to it. Cloud Load Balancing requires a regional configuration object for it, a serverless NEG. This object represents the Cloud Run service within Cloud Load Balancing.
gcloud compute network-endpoint-groups create run-neg \
--region=europe-west1 \
--network-endpoint-type=serverless \
--cloud-run-service=run-service \
--project=$PROJECT_ID
Created
[https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/regions/europe-west1/networkEndpointGroups/run-neg].
Created network endpoint group [run-neg].
Add the serverless NEG to the backend service
Plug in the Cloud Run service to the backend using the NEG.
gcloud compute backend-services add-backend backend-service \
--global \
--network-endpoint-group=run-neg \
--network-endpoint-group-region=europe-west1 \
--project=$PROJECT_ID
Updated [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/backendServices/backend-service].
Create the URL map
The URL map is the router for your load balancer. It inspects the hostname and path of incoming requests and directs them to the appropriate backend service. This configuration specifies to forward all requests to the backend service.
gcloud compute url-maps create url-map \
--default-service=backend-service \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/urlMaps/url-map].
NAME DEFAULT_SERVICE
url-map backendServices/backend-service
As your application grows, you can add path rules to this URL map to direct different
URL paths to different services (for example, sending /api/* to a backend
service and all other traffic to a frontend web application).
6. Set up the HTTPS frontend
The frontend configuration defines how clients reach your service over HTTPS. It consists of a target proxy and a forwarding rule.
Create the target HTTPS proxy
The target HTTPS proxy handles the SSL connection and decrypts incoming HTTPS traffic using the certificate you created in 3. Provision a managed SSL certificate.
gcloud compute target-https-proxies create https-proxy \
--ssl-certificates=lb-cert \
--url-map=url-map \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/targetHttpsProxies/https-proxy].
NAME SSL_CERTIFICATES URL_MAP REGION CERTIFICATE_MAP
https-proxy lb-cert url-map
Create the HTTPS forwarding rule
The forwarding rule connects your static IP address and port 443 (HTTPS) to the target proxy.
gcloud compute forwarding-rules create https-rule \
--load-balancing-scheme=EXTERNAL_MANAGED \
--address=lb-ip \
--target-https-proxy=https-proxy \
--global \
--ports=443 \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/forwardingRules/https-rule].
7. Set up the HTTP-to-HTTPS redirect
The Strict-Transport-Security (HSTS) response header you configured on the
backend service in
5. Configure a serverless backend
tells browsers to always use HTTPS, but a browser only sees that header after its first
HTTPS visit. You still need a redirector on port 80 to catch and upgrade a client's
first unencrypted HTTP request. In this section, you import a URL map that returns an
HTTP 301 redirect, attach it to a target HTTP proxy, and bind a port 80
forwarding rule to the same lb-ip address.
Create the redirect URL map
Import a URL map that returns a 301 Moved Permanently redirect. Passing
--source /dev/stdin <<EOF feeds the YAML redirect specification
directly from standard input without needing a temporary file, and
MOVED_PERMANENTLY_DEFAULT sets the HTTP status code to 301:
gcloud compute url-maps import redirect-map \
--global \
--project=$PROJECT_ID \
--source /dev/stdin <<EOF
name: redirect-map
defaultUrlRedirect:
redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
httpsRedirect: True
EOF
Creating URL map...
.......done.
Create the target HTTP proxy
Create a target HTTP proxy that uses redirect-map to handle incoming HTTP
requests:
gcloud compute target-http-proxies create http-proxy \
--url-map=redirect-map \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/targetHttpProxies/http-proxy].
NAME URL_MAP
http-proxy redirect-map
Create the HTTP forwarding rule
This rule uses the same static IP as the HTTPS rule, listens on port 80, and targets the HTTP proxy you created in the previous step.
gcloud compute forwarding-rules create http-rule \
--load-balancing-scheme=EXTERNAL_MANAGED \
--address=lb-ip \
--target-http-proxy=http-proxy \
--global \
--ports=80 \
--project=$PROJECT_ID
Created [https://www.googleapis.com/compute/v1/projects/YOUR_PROJECT_ID/global/forwardingRules/http-rule].
Verification
Once you have completed the configuration, verify that the certificate is active and the
redirect and HSTS header work as expected. You'll check the certificate status with
gcloud and then inspect the HTTP and HTTPS response headers with
curl.
Check certificate status
Google proves your domain ownership by attempting to reach your load balancer from
multiple locations across the internet. Monitor the overall certificate status and
per-domain validation status with gcloud (when provisioning completes, both
statuses report ACTIVE):
gcloud compute ssl-certificates describe lb-cert \
--global \
--format="get(managed.status,managed.domainStatus)" \
--project=$PROJECT_ID
ACTIVE example.com=ACTIVE
If you see FAILED_NOT_VISIBLE, it usually means DNS has not yet propagated
to all of Google's validation vantage points, or your load balancer isn't yet reachable
on port 443.
Test the HTTP-to-HTTPS redirect and the HTTPS endpoint
Once the certificate is ACTIVE, test the HTTP redirect with
curl -I (which fetches response headers only):
curl -I http://$DOMAIN_NAME
HTTP/1.1 301 Moved Permanently
Cache-Control: private
Location: https://example.com:443/
Content-Length: 0
Date: Tue, 27 Oct 2026 16:38:55 GMT
Content-Type: text/html; charset=UTF-8
Next, verify that the HTTPS endpoint returns 200 OK along with the
strict-transport-security header (even after the certificate reports
ACTIVE, it can take a few additional minutes to propagate to Google's edge
proxies, during which curl may return SSL_ERROR_SYSCALL):
curl -I https://$DOMAIN_NAME
HTTP/2 200
content-type: text/html
last-modified: Wed, 21 Oct 2026 14:24:51 GMT
etag: "6ad8cb33-380"
accept-ranges: bytes
x-cloud-trace-context: 81fc4441e269f2572c699cc56580819f;o=1
content-length: 896
date: Tue, 27 Oct 2026 16:47:39 GMT
server: Google Frontend
via: 1.1 google
strict-transport-security: max-age=31536000; includeSubDomains; preload
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
What this setup costs
Unlike Cloud Run, an external Application Load Balancer doesn't have a free tier.
Pricing varies by region and can change over time, so check the
Cloud Load Balancing pricing
page for current rates in your region. As of October 2026, here is what to expect in
North America and Europe (including europe-west1):
- The base load balancer costs around $18 per month ($0.025 per hour), plus $0.008 per GiB of data processed.
-
That base hourly rate covers up to five forwarding rules, so adding the second
forwarding rule (
http-ruleon port 80) for the HTTP-to-HTTPS redirect doesn't cost extra alongsidehttps-rule. -
Google-managed SSL certificates (
lb-cert) are free, and the reserved static IP address (lb-ip) is free while attached to a forwarding rule (unattached static IPs cost $0.005 per hour).
Let me know if you run into any questions setting this up!