9.3 CLI Configurations, Local ADC, and Cloud Shell
Key Takeaways
gcloud named configurations select CLI properties; they do not rewrite local ADC.
Service-account impersonation can avoid downloaded private keys when configured for the intended client.
Keep persistent Shell work in its documented home storage; preview and idle limits are separate settings.
Local Development Environments, Cloud Workstations & Cloud Code
Modern cloud-native development requires a fast, reproducible "inner loop"—the iterative cycle of writing code, building containers, running local integration tests, and debugging before committing changes to version control. On Google Cloud, building applications efficiently requires mastering the Google Cloud CLI (gcloud), leveraging browser-based developer environments (Cloud Shell and Cloud Workstations), and utilizing container workflow tools (Cloud Code and Skaffold).
Google Cloud CLI (gcloud) Management & Authorization Deep Dive
The gcloud CLI is the primary command-line tool for provisioning, managing, and automating Google Cloud resources. For professional cloud developers, understanding its multi-account configuration mechanics, authorization boundaries, and programmatic output formatting is essential.
Named Configurations and Multi-Project Switching
Developers frequently work across multiple Google Cloud projects (e.g., development, staging, production) and different corporate or personal Google identities. Rather than repeatedly overriding project flags or re-authenticating, gcloud provides named configurations:
## Create a new configuration for the staging environment
gcloud config configurations create order-service-staging
## Set the active identity, GCP project, and default compute region
gcloud config set account developer@company.com
gcloud config set project order-service-staging-4281
gcloud config set compute/region us-central1
## Switch between configurations instantly
gcloud config configurations activate order-service-dev
gcloud config configurations activate order-service-staging
## List all existing configurations and view the currently active profile
gcloud config configurations list
Environment Variable Overrides
When running automated scripts, CI/CD jobs, or terminal sessions that must temporarily target an alternate project without mutating the persistent global gcloud state, use environment variables:
CLOUDSDK_CORE_PROJECT: Overrides the active GCP project ID.CLOUDSDK_ACTIVE_CONFIG_NAME: Specifies which named configuration to activate for the current shell session.CLOUDSDK_COMPUTE_REGION: Overrides the default Compute Engine/Cloud Run region.
Authorization Architecture: CLI Login vs. Application Default Credentials (ADC)
A critical exam concept and frequent developer stumbling block is the distinction between authorizing the gcloud CLI tool and authorizing application code:
| Authorization Mechanism | Command | Credential Storage Location | Primary Consumer |
|---|---|---|---|
| gcloud User Auth | gcloud auth login | ~/.config/gcloud/credentials.db (SQLite) | gcloud CLI commands (gcloud run deploy, gcloud storage ls) |
| Application Default Credentials (ADC) | gcloud auth application-default login | ~/.config/gcloud/application_default_credentials.json | Google Cloud Client Libraries (@google-cloud/*, google-cloud-*) |
| Service Account Key File | Set GOOGLE_APPLICATION_CREDENTIALS | Absolute path to downloaded .json key | Local code explicitly simulating a specific Service Account |
| Service Account Impersonation | gcloud config set auth/impersonate_service_account | Ephemeral OAuth2 tokens retrieved via IAM API | CLI calls; ADC impersonation is configured separately |
Exam Trap: Running
gcloud auth loginauthenticates thegcloudtool itself. If a developer subsequently runs a Node.js, Python, Java, or Go application locally that uses official Google Cloud client libraries, ADC can fail if no other valid credential source exists. To authorize local application code to access cloud APIs using the developer's identity, the developer must rungcloud auth application-default login.
Service Account Impersonation for Local Development
Downloading long-lived JSON service account keys onto developer laptops creates severe security and exfiltration risks. The Google Cloud recommended practice for local development requiring service account privileges is IAM Service Account Impersonation:
## Grant the developer the Service Account Token Creator role (roles/iam.serviceAccountTokenCreator)
## on the target service account:
gcloud iam service-accounts add-iam-policy-binding backend-sa@my-project.iam.gserviceaccount.com \
--member="user:developer@company.com" \
--role="roles/iam.serviceAccountTokenCreator"
## Configure gcloud to automatically impersonate the service account for all subsequent actions:
gcloud config set auth/impersonate_service_account backend-sa@my-project.iam.gserviceaccount.com
## Generate ADC credentials that automatically impersonate the service account:
gcloud auth application-default login --impersonate-service-account=backend-sa@my-project.iam.gserviceaccount.com
Token Generation for Authenticated Testing
When testing private Cloud Run services or Cloud Functions protected by IAM, developers must send an OpenID Connect (OIDC) ID token in the HTTP Authorization header:
## Generate an OIDC ID token for an authenticated Cloud Run endpoint
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
https://private-order-service-xyz.a.run.app/api/v1/orders
## Print an OAuth2 access token for Google Cloud REST APIs
curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://storage.googleapis.com/storage/v1/b/my-app-bucket/o
Programmatic Output Formatting (--format) and Filtering (--filter)
In automation and shell pipelines, developers should never parse raw human-readable CLI tables. The gcloud CLI provides powerful client-side formatting and server-side filtering:
## Filter Cloud Run services deployed in us-central1 that are in ready status
## and output only their live HTTPS URLs as unadorned text:
gcloud run services list \
--filter="region:us-central1 AND status.conditions[0].status=True" \
--format="value(status.url)"
## Extract the internal IP address of a specific Compute Engine VM instance:
gcloud compute instances list \
--filter="name=order-worker-*" \
--format="value(networkInterfaces[0].networkIP)"
## Output resource metadata as clean, parseable JSON or YAML:
gcloud secrets versions describe latest --secret=db-password --format="json"
Google Cloud Shell: Architecture & Inner-Loop Capabilities
Google Cloud Shell is an interactive, browser-accessible Debian Linux development environment hosted directly within Google Cloud. It eliminates the "works on my machine" friction by providing a standardized, pre-configured development toolchain accessible from any modern web browser.
Cloud Shell Architecture and Storage Guarantees
- Compute Instance: Each user receives an ephemeral Compute Engine VM provisioned on demand. The root filesystem (
/) is ephemeral; when the ephemeral VM is recycled or the session reaches its idle/session limits, the underlying VM is discarded and any changes made outside the home directory are lost. - Persistent Storage: Cloud Shell allocates 5GB of persistent storage mounted at
$HOME. This directory is preserved across sessions and VM restarts. Code, Git repositories, dotfiles (.bashrc), and developer tools installed into$HOMEpersist indefinitely (provided the user accesses Cloud Shell at least once every 120 days). - Built-in Authorization: Cloud Shell is automatically authenticated with the Google credentials used to log into the Google Cloud Console. Both the
gcloudCLI and Application Default Credentials (ADC) are pre-configured without requiringgcloud auth login. - Pre-installed Tooling: Includes Docker,
gcloud,kubectl,terraform, Helm, Skaffold, Minikube, Git, and major language runtimes (Go, Python, Java, Node.js, .NET). - Web Preview Proxy: Provides a built-in HTTP proxy that securely tunnels ports (8080 by default; supported preview ports are documented by Cloud Shell) from the Cloud Shell VM to the user's browser over HTTPS, allowing immediate testing of web servers and microservices.
- Cloud Shell Editor: A full-featured browser IDE based on Theia / VS Code, containing native Git integration, a terminal multiplexer, and the pre-installed Cloud Code extension.
Official references checked October 18, 2026: Google Cloud 1.
A developer activates a gcloud configuration for a staging project. Their Python client still uses an old local ADC identity. What explains this?
Named gcloud configurations and local ADC are separate; inspect and deliberately configure the application credential source.
Activating a gcloud configuration always deletes all other ADC files.
The Python library reads only the gcloud credentials.db file.
Setting compute/region automatically impersonates the runtime service account.
Sections you finish are checked off in the contents.