A cloud administrator regularly manages infrastructure across separate development, staging, and production Google Cloud projects from a single workstation using the gcloud CLI. Currently, the administrator manually re-authenticates or overrides environment variables whenever switching target environments, which increases the risk of executing administrative commands against the wrong project. Which approach aligns with Google Cloud best practices for managing and switching between multiple environment settings cleanly when using the gcloud CLI?
- Create separate named gcloud configurations for each environment using gcloud config configurations create, define project and account settings within each configuration, and activate the required profile using gcloud config configurations activate.Answer
- BGenerate long-lived JSON service account key files for each project and write a wrapper shell script that dynamically exports GOOGLE_APPLICATION_CREDENTIALS pointing to the respective key file prior to running gcloud commands.
- CAssign the primitive Owner IAM role to the administrator's personal account across all target projects so that gcloud commands automatically inherit administrative context without explicitly defining configurations.
- DPerform all resource changes for staging and production manually via the Google Cloud Console, reserving gcloud CLI usage exclusively for the local development environment.
Answer
Create and activate separate named gcloud configurations for each target project environment using the gcloud config configurations command group.
The gcloud CLI provides native support for multiple named configurations via `gcloud config configurations`. Each named configuration maintains its own set of properties, including default project ID, active user account, region, and zone. Activating a specific named configuration instantly switches the active operational context, preventing accidental execution against unintended projects without requiring static credential files.
Step-by-Step Solution
Key Concept
gcloud Named Configurations for Environment Management