❯ Guillaume Laforge

Serverless tip #4 — Discover the full URL of your deployed Cloud Run services with gcloud format flag

Requirements:

  • an existing Google Cloud Platform account
  • you have enabled the Cloud Run service and deployed already a container image

One of the nice things with Cloud Run is that when you deploy your services, you get a URL like https://myservice-8oafjf26aq-ew.a.run.app/, with a certificate, on the run.app domain name, etc.

You see the name of the service: myservice, the region shortcut where it was deployed: ew (Europe West), and then .a.run.app. However, you can’t guess ahead of time what the final URL will be, as there is a randomly generated part in the URL (here: 8oafjf26aq). Let’s see how we can discover this whole URL.

From my terminal, I can request the list of deployed services (here, on the fully managed Cloud Run):

gcloud run services list \
       --platform managed

It’s going to show me something like the following output:

   SERVICE      REGION        URL                                        LAST DEPLOYED BY     LAST DEPLOYED AT
✔  myservice    europe-west1  https://myservice-8oafjf26aq-ew.a.run.app  myself@foobar.com    2019-11-20T15:26:39.442Z

When describing the specific service (I had to specify the region as well, but you can set it by default if needed to avoid repeating yourself):

gcloud run services describe myservice \
       --platform managed \
       --region europe-west1

You’ll see:

✔ Service hello in region europe-west1
https://myservice-8oafjf26aq-ew.a.run.app
Traffic:
  100%               LATEST (currently myservice-00002-dox)
Last updated on 2019-11-20T15:26:39.442Z by myself@foobar.com:
  Revision myservice-00002-dox
  Image:             gcr.io/my-sample-project/my-container-image:latest

So instead of parsing that ourselves somehow, there’s a built-in way to get just the info we want, with the useful --format flag:

gcloud run services describe myservice \
    --format='value(status.url)' \
    --platform managed --region europe-west1 

This time, in output, you’ll get just the URL, which you can then export or reuse with other commands.

https://myservice-8oafjf26aq-ew.a.run.app

The glcoud command provides three useful mechanisms to filter, format, or project the output and values returned. Here, we took advantage of format.

More information: