There are times when you’re going to need to know the URL of a Cloud Run service.

Once you first deploy a service, then you’ll know the URL. The URL is based on the name of the service, the region, and a hash based on the project. So all services are unique, but unless you know the hash of the project ahead of time, then you won’t know the URL.

You could deploy a service with the sample container and parse it from there. Or you could deploy this once, then update your service’s environment variables with the value, then update the service with your actual code.

Or, your service could self-identify.

The URL of a Cloud Run service is accessible from the Cloud Run API:


But you need to know the project_id, region, and service_name. You also need your service to have access to this API.

The Project ID can be retrieved from the Google Auth package:

import google.auth
_, project_id = google.auth.default()

The region can be retrieved directly from the metadata service:

import httpx
resp = httpx.get(
            headers={"Metadata-Flavor": "Google"},
region = resp.text.split("/")[-1]

And the service can be retrieved from the environment variable that comes for free in Cloud Run:

import os
service_name = os.environ.get("K_SERVICE")

From there, you have all the values you need.

You’ll also have to give the service account running your Cloud Run service the permission. This can be achieved by granting Cloud Run Viewer, or a number of other roles. Use the permission within the IAM Roles to find all the roles which include this permission.

You can see a full implementation of this in the file in avocano.