This is my first post in a series where I’ll be documenting useful features of Google Cloud Platform while studying for the Google Associate Cloud Engineer certification. Having used GCP for projects here and there, primarily the App Engine and Cloud Storage services, over the past year, I decided it would be useful to dive deeper into the wide variety of services on offer and their tradeoffs in order to make more meaningful design and architecture decisions. Today, I’ll just start out by looking at a couple of basic, useful Cloud Shell commands to get things started. Cloud Shell offers a platform to interact with all of your GCP projects and services via a convenient online shell environment. The general features of Cloud Shell are:

  • A free, temporary Compute Engine VM.
  • Command-line access to the instance via a browser.
  • 5GB of persistent disk storage in the home directory.
  • Pre-installed Cloud SDK and other tools:
    • gcloud: for working with Google Compute Engine and many Google Cloud services.
    • gsutil: for working with Cloud Storage.
    • kubectl: for working with Google Container Engine and Kubernetes.
    • bq: for working with BigQuery.
  • Language support for Java, Go, Python, Node.js, PHP, and Ruby.
  • Web deployment preview functionality.
  • Built-in authorization for access to resources and instances. I like to work in Cloud Shell for admin tasks when possible as it allows you to be local environment agnostic, and it gets even easier once you’ve set up a few persistent configuration variables, which we’ll look at now.

After spinning up a Cloud Shell instance, start by creating a config file in a new directory that you’ll use for your current project.

mkdir basic_project
touch basic_project/config

Now, let’s add something to the config file. How about setting a default region enviroment variable? First, list all available regions:

gcloud compute regions list

Pick an appropriate region and send it to your config file.

echo MY_REGION=[your selected region] >> ~/basic_project/config

And we can do the same thing with the project id.

echo MY_PROJECT_ID=$DEVSHELL_PROJECT_ID >> ~/basic_project/config

And so forth for any project specific enviromental variables you might need. Now, to load the variables for a given project, all you have to do is source the corresponding file.

source ~/basic_project/config
# You can also add the source command to your '.profile' file for environmental variables that are project independent, that way they'll be loaded whenever Cloud Shell restarts.
echo "source ~/basic_project/config" >> ~/.profile

With that small step, you’ve taken a lot of tedium out of using the Cloud Shell and made loading environmental variables a breeze, so you can focus on using the GCP services.