Work safely with gcloud configurations and Cloud Shell
Switch gcloud configurations, confirm the active context, run a create-and-verify loop and explain user login versus application credentials.
By Invicly Tech, , in Google Cloud Engineering and Services. About 10 minutes to read.
In short
The gcloud CLI acts on whatever account, project and region its active configuration says, so most mistakes come from running a correct command in the wrong context. Named configurations let you switch context on purpose, and a check-then-act habit keeps you safe. Separately, gcloud auth login signs in the CLI itself, while gcloud auth application-default login gives your own code credentials.
Think of it like this: Named configurations are like separate keyrings for home and office: you pick up one keyring on purpose instead of trying every key in your pocket.
Explained simply
The gcloud CLI is the command-line tool for Google Cloud. Every command runs as some account, against some project, often in some region. Those three values come from the active configuration, which is a named bundle of settings. If the configuration points at the wrong project, a perfectly good command does damage in the wrong place.
So build a habit: check, then act. Run gcloud config list to see who you are and where you are. Then list what already exists. Only then create or delete something, and list again to confirm it worked.
Cloud Shell is a free terminal in your browser with gcloud already installed and signed in as you. It gives you 5 GB of persistent home storage. The machine behind it is temporary and is recycled after about an hour of inactivity, but your home folder stays.
There are two separate logins. gcloud auth login lets the CLI act as you. gcloud auth application-default login creates credentials that your own programs, using Google client libraries, can find automatically. That second mechanism is called Application Default Credentials, or ADC.
Press Play on a diagram to walk through it one step at a time.
- 1Check context
gcloud config list
- 2Create
One resource, labelled
- 3Verify
describe or list shows it
- 4Delete
Remove it and list again
- Back to step 1
- Credentials for gcloud commands
- Stored in gcloud's own config
- Used when you run gcloud
- Not read by client libraries
- Credentials for your code
- Writes application_default_credentials.json
- Read by Google client libraries via ADC
- Can impersonate a service account
- core
- account = ana@example.com
- project = orders-lab-4821
- compute
- region = europe-west2
- zone = europe-west2-a
- auth (optional)
- impersonate_service_account
In depth
Named configurations
gcloud ships with one configuration called default. Create others with gcloud config configurations create NAME, which also activates the new one. Set properties inside it: core/account, core/project, compute/region and compute/zone are the usual four. Switch with gcloud config configurations activate NAME.
Two overrides avoid switching altogether. The --configuration=NAME flag applies a configuration to one command, and the CLOUDSDK_ACTIVE_CONFIG_NAME environment variable applies it to one terminal session. That is handy when you keep a dev terminal and a prod terminal side by side. Many teams also put the project in the shell prompt so the context is always visible.
- gcloud config configurations create orders-lab
- gcloud config configurations activate default
- gcloud config configurations list
- --configuration=NAME for one command
- CLOUDSDK_ACTIVE_CONFIG_NAME for one terminal
A read-only inventory
Before changing a project you did not build, take stock with commands that only read. They are safe to run and they show what you might break. Start with identity and context, then enabled services, then the main resource types. Cloud Asset Inventory can search across many resource types at once, though its API must be enabled first.
Use --format to keep output short, such as --format="table(name,location)", and --filter to narrow it, such as --filter="labels.env=lab". Save the output to a file in your home folder so you can compare before and after a change.
- gcloud auth list and gcloud config list
- gcloud services list --enabled
- gcloud storage buckets list
- gcloud run services list
- gcloud asset search-all-resources --scope=projects/ID
The create, verify, delete loop
Every change follows the same four steps. Check the context. Create one resource with clear labels. Verify it with describe or list, looking for the exact fields you set, such as location and labels. Delete it and list again to confirm it is gone. Writing the expected evidence down before you run the command turns a guess into a test.
Using a bucket as the example: create it with an explicit location and uniform bucket-level access, describe it and check location and labels, then remove it with gcloud storage rm --recursive, which deletes the objects and the bucket. If a command fails halfway, list again before retrying so you do not create duplicates.
- 1Context
config list shows account and project
- 2Expect
Write down what success looks like
- 3Create
One labelled resource
- 4Verify
describe shows the expected fields
- 5Clean up
Delete and list again
CLI credentials versus Application Default Credentials
gcloud auth login opens a browser, signs you in and stores credentials that only gcloud uses. Client libraries for Python, Go, Java or Node.js do not read them. They look for ADC instead, in this order: a file named by the GOOGLE_APPLICATION_CREDENTIALS environment variable, then the file written by gcloud auth application-default login, then the service account attached to the runtime through the metadata server.
On Linux and macOS that local ADC file is $HOME/.config/gcloud/application_default_credentials.json. Running gcloud auth application-default login with --impersonate-service-account makes your code act as a service account instead of as you, which is the safest way to test with production-like permissions. In Cloud Shell, client libraries can use your signed-in identity without extra steps.
- You to gcloud CLI: gcloud auth login1gcloud auth login
- gcloud CLI to Google APIs: CLI calls use your user token2CLI calls use your user token
- You to Your Python app: gcloud auth application-default login3gcloud auth application-default login
- Your Python app to Google APIs: Client library finds ADC file and calls API4Client library finds ADC file and calls API
Learn it in 5 short steps
About 25m each. Track them with a timer in your plan.
- 1Create named gcloud configurations~25m
- 2Check account, project and region~25m
- 3Take a read-only inventory~25m
- 4Create, verify and delete a resource~25m
- 5Compare gcloud auth login and ADC login~25m
Key points
- Create one configuration per project or environment with gcloud config configurations create and activate.
- Run gcloud config list before any change; it shows account, project and default region.
- Use read-only list and describe commands to take inventory before you create or delete.
- Cloud Shell is a free browser terminal with gcloud preinstalled and 5 GB of persistent home storage.
- CLI login and application credentials are separate; logging in to one does not set up the other.
Key terms
Tap a card to flip it. 0/5 known
Examples
gcloud config configurations create orders-lab
gcloud config set account ana@example.com
gcloud config set project orders-lab-4821
gcloud config set compute/region europe-west2
gcloud config list # confirm before acting
gcloud services list --enabled # read-only inventory
gcloud config configurations activate defaultSet up and check a named configuration.
Two projects, two terminals
You work on orders-dev and look at orders-prod. Step 1: create a configuration for each. Step 2: in the prod terminal, set CLOUDSDK_ACTIVE_CONFIG_NAME so that terminal always talks to prod, and in the dev terminal do the same for dev. Step 3: run gcloud config list in each to confirm. Expected evidence: each terminal shows its own project, and switching in one does not change the other.
gcloud config configurations create orders-dev
gcloud config set project orders-dev-1111
gcloud config configurations create orders-prod
gcloud config set project orders-prod-2222
# terminal 1
export CLOUDSDK_ACTIVE_CONFIG_NAME=orders-dev
# terminal 2
export CLOUDSDK_ACTIVE_CONFIG_NAME=orders-prod
gcloud config listCreate, verify and delete a bucket
Prerequisites: a lab project with billing and the right configuration active. Step 1: check context. Step 2: create a bucket in europe-west2 with uniform access and a label. Step 3: describe it and check location and labels. Step 4: remove it and list again. Expected evidence: describe shows location EUROPE-WEST2, and the final list no longer shows the bucket. Bucket names are global, so add a suffix.
gcloud config list
B=gs://orders-lab-4821-receipts
gcloud storage buckets create $B --location=europe-west2 \
--uniform-bucket-level-access
gcloud storage buckets update $B --update-labels=env=lab
gcloud storage buckets describe $B --format="yaml(location,labels)"
gcloud storage rm --recursive $B
gcloud storage buckets list --format="value(name)"Quick reference
- gcloud config list: show active account, project and region
- gcloud config configurations create | activate | list NAME
- --configuration=NAME applies to one command; CLOUDSDK_ACTIVE_CONFIG_NAME to one shell
- gcloud auth login = CLI; gcloud auth application-default login = your code
- ADC order: GOOGLE_APPLICATION_CREDENTIALS, local ADC file, attached service account
- Cloud Shell: 5 GB persistent $HOME; VM recycled after inactivity
- Loop: check context, create, verify, delete, list again
Common mistakes
- Switching projects with gcloud config set project in the default configuration and forgetting to switch back.
- Expecting a Python script to work after gcloud auth login; the script needs ADC.
- Saving important files outside $HOME in Cloud Shell; the VM is discarded after the session ends.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create a configuration called lab with your lab project and region europe-west2, then show it.
- Exercise 2medium
Write a read-only inventory script that saves account, project, enabled APIs, buckets and Cloud Run services to a file.
- Exercise 3hard
Make your local Python code act as a service account named orders-reader without any key file, and prove which identity it uses.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is Cloud Shell free?
- Yes, Cloud Shell itself is free with a weekly usage limit, but resources you create from it are billed normally.
- What does gcloud init do?
- It walks you through signing in and setting the account, project and region for the current configuration.
- Should I use a service account key for local testing?
- No. Use your own ADC login or impersonate a service account; downloaded keys are long-lived secrets that leak easily.
- How do I sign out?
- gcloud auth revoke removes CLI credentials, and gcloud auth application-default revoke removes the local ADC file.