Google Cloud Shell: How to find your web preview URL

Viewed 1972

When using Google Cloud Shell on the Google Cloud Platform console, clicking on the "web preview" button will redirect you to a URL that is serving your app on port 8080.

So, for example, the URL for your instance may be something like:

https://8080-1234abcd-abcd-1234-abcd-1234abcd.europe-west1.cloudshell.dev/?authuser=0

Is there a way to determine what this URL is going to be from the terminal, without having to click on the "web preview" button?

Note: For those wondering what the use case for this is. I am using the SSH cloud shell access feature that allows you to remote into your cloud shell instance via SSH from any terminal emulator. Unfortunately, doing so means that you no longer have access to the "web preview" button (as you are using your own terminal and not the web based one) and so are unable to know what the URL for your web preview is going to be located.

4 Answers

You can determine what the URL will be from the terminal with the environment variable WEB_HOST, which is preconfigured in Cloud Shell.

The formatted preview URL will look like this:
https://$PORT-$WEB_HOST

See Preview web apps Docs

Cloud Shell is a tool that is primarily intended to provide an environment for managing your GCP resources and is not intended to be used as a development environment (but can be used to test code snippets)

Access to the Cloud Shell from the command line (via ssh on a non-web terminal) is in alpha version, as mentioned in this document.

This functionality can be super useful, but at the moment it is not possible to generate the preview url outside the GCP console (web UI), my recommendation is to open a feature request to allow the creation of a preview URL outside the web terminal.

To create a feature request, you must fill a public issue tracker on this page.

As a workaround, you can generate preview URLs on any Cloud or on-premise server by using a free account of ngrok, this is a software that allows you to generate ephemeral HTTPs URLs pointing to your localhost services on any port (demo endpoints) in the same way as Cloud Shell web preview works.

the rule to generate url hostname is uncertainty,sometimes its [port]-[guid]-[region].cloudshell... ,while sometimes it has a fixed name [port]-[host]-... ,the "host" is the hostname of yr cloud shell vm

Is there a way to determine what this URL is going to be from the terminal, without having to click on the "web preview" button?

Yes, it is possible.

Eg:

PORT="8080"
cd `mktemp -d` \
    && echo '<html><body>Hello World</body></html>' >./index.html \
    && python -m SimpleHTTPServer $PORT &
AUTHUSER="0"
ADDPATH=""
echo "https://shell.cloud.google.com/devshell/proxy?authuser=$AUTHUSER&port=$PORT&environment_id=default$ADDPATH"

After click on link, the proxy endpoint will authenticate the GCP user and redirect to final destination (eg https://8080-cs-215311858653-default.cs-us-east1-vpcf.cloudshell.dev/?authuser=0).

Although it is possible to generate the final URL with the following commands, it is not a good idea to use it as the port is only accessible after the proxy endpoint authenticates the user.

ZONE=$(curl -s -H "Metadata-Flavor: Google" metadata/computeMetadata/v1/instance/zone)
ZONE="${ZONE##*/}"
REGION=${ZONE%-*}
MACHINE=$(hostname)
MACHINE="${MACHINE%-default*}-default"
PORT="8080"
echo "https://${PORT}-${MACHINE}.cs-${REGION}-vpcf.cloudshell.dev/?authuser=0"
Related