Working with multiple environments in gcloud CLI

Daniel Megyesi
Infrastructure adventures
1 min readAug 19, 2018

This article is part of my “short notes for myself, maybe useful for others too” series.

…aka how to manage different environments and projects in Google Cloud and switch between them quickly, each with their own settings and defaults.

Login and create contexts

# First time logingcloud init
# Create contextsgcloud config configurations create prodgcloud config configurations create staginggcloud config configurations create sandbox

Configure each environment

Either you can do gcloud init and gcloud config … for every environment one by one:

Version 1. The elegant way

You need to login to your Google account for each environment and configure project name, etc.

# Select environment
gcloud config configurations activate sandbox
# Login
gcloud init

# Defaults
gcloud config set project <PROJECT_NAME>
gcloud config set compute/region europe-west3gcloud config set compute/zone europe-west1-b

Version 2. The quick way

Create the config file for each environment by hand; change:

  • file name (config_sandbox)
  • project ID
  • account address
# file contents of: config_sandboxcat > ~/.config/gcloud/configurations/config_sandbox << EOF[core]account = daniel@megye.siproject = <PROJECT NAME>[compute]zone = europe-west1-bregion = europe-west1EOF

Validate

gcloud config configurations list

Switch between environments

gcloud config configurations activate sandbox

--

--