Setup and Switch Between Google Cloud Projects in the SDK

Warrick
Google Cloud - Community
3 min readMay 27, 2020

Providing a quick overview on how to setup and switch between Google Cloud projects with the SDK on a single machine. This is helpful when working with multiple projects (especially when collaborating) and you are using Cloud SDK.

Setup & Authenticate

The following steps are needed whether creating a project for the first time or a project already exists and you are logging into it off a computer where you will do local development. This assumes you will run all commands from a terminal.

Create a new gcloud configuration for your project on the machine you will use to access it.

gcloud config configurations create [NAME]

It will automatically set it as the active account unless you pass a flag to not make this active in the command. Replace the [NAME] placeholder with a name to use when switching between Google Cloud projects. You can use the project id as a name to keep it simple or go with what works best.

Set the Google Cloud project id to the active gcloud configuration.

gcloud config set project [PROJECT-ID]

Setup a project in Google Cloud if you haven’t already to get the project id.

Authorize the Google account that has some type of access/ownership of this project.

gcloud auth login

Login to that account if you haven’t already. Note, the above and below commands will open a browser window so you can login for the gmail account and authenticate. There is a way to do this if you don’t have a browser by passing the — no-launch-browser flag in the command line.

Acquire new user credentials to use for Application Default Credentials which will be used in calling Google APIs.

gcloud auth application-default login

Use the above command if you are developing code in something like a local development environment and it would be easier to use user credentials than setup a service account.

Add quota to the project to avoid quota exceeded or API not enabled errors.

gcloud auth application-default set-quota-project [PROJECT_ID]

Review & Activate

The commands in the section are how you can see what your current configurations are and how to change between different projects on one machine.

Review all the configurations that exist on your machine.

gcloud config configurations list

Change default configuration that is active to switch between projects.

gcloud config configurations activate [NAME]

Review only the active project.

gcloud projects list

Review the details of the current active configuration such as the name, region, account.

gcloud config list 

Set an attribute of the current active configuration

gcloud config set [ATTRIBUTE] [NAME of ATTRIBUTE]

List the authenticate user ids and the currently active one, which will have a * next to what shows in the terminal.

gcloud auth list

Above should show the current active account that you’ve configured with your current active project.

Wrap Up

This is a quick reference to share how to configure, check and change the active Google Cloud project configuration in the gcloud SDK. Use How to Manage SDK Configuration for more details and commands. Go forth and add all the projects to your SDK.

 by the author.

--

--