How to Change the Project in GCP using GCloud CLI commands – Definitive Guide

A project in Google Cloud organizes all your Google Cloud resources in one account. While working with the gcloud command line interface, you need to set the project accordingly to interact with the resources in the project.

You can change the project in GCP using the CLI command using the command gcloud config set project project_id.

Basic Example

gcloud config set project YOUR_PROJECT_ID

This tutorial teaches you how to find the Project ID, change the current project using the GCloud CLI, and work with the environment variables to change the projects.

Finding the Project ID Using GCloud CLI

To change the project in the GCloud SDK, you need to know the project ID or the Project Number. You cannot do it with the Project name.

  • The Project Number is generated automatically by Google and is not changeable.
  • The Project ID is a human-readable format of the Project Number(Alias for the Project Number). You can either accept the generated project ID or create your own id while creating the project. However, you cannot change the Project ID after creating the project.

To know the project ID directly from the gcloud, use the gcloud projects list command as shown below

Code

gcloud projects list 

All the projects available in the currently authenticated account will be displayed.

Output

PROJECT_ID      NAME            PROJECT_NUMBER

testproject1        mytestproject       34685865164

testproject2  mytestproject  55265474586

Changing the Project Using GCloud CLI Command

You have your project’s Project_ID and the Project_Number.

To change the project using the Gcloud CLI command, use the gcloud config set project command along with the Project ID or the Project Number as shown below.

Code

gcloud config set project <project_id>
  • <Project_ID> – Use your project ID or the Project number in highlighted place

Output

Updated property [core/project].

The project is changed, and it’ll be the default project even if you restart the terminal.

Change to A Project in a Different Account Using GCloud CLI

Sometimes, you might use different Google Cloud projects on the same computer. In that case, you might need to switch between the projects available in other accounts.

To change to a project in a different account using GCloud CLI, first, you must set the current account in the gcloud.

Use the auth list command to find all the configured accounts, as shown below.

gcloud auth list

Output

All the configured accounts are listed, and the currently active account is denoted using *.

Credentialed Accounts

ACTIVE  ACCOUNT

*             abe@mail.com

               hhhdd@mail.com

If you do not see the desired account, you can add the account using the gcloud auth login command.

If the other account is available/added, you can change to your desired account using the set account command.

gcloud config set account hhhdd@mail.com

Output

If you enter the proper account name, the current account will be updated, and the following message will be shown.

Updated property [core/account].

Once the account is set, you can use the gcloud config set project command to set the project from the newly set account.

gcloud config set project <project_id>`

This is how you can change the project in GCP using the GCloud SDK CLI commands.

If you have any questions, feel free to comment below.

Categories GCP

Leave a Comment