7
gcloud Tricks You Probably Didn’t Know
The Cloud SDK includes a set of tools and libraries for interacting with Google Cloud products and services, with the gcloud
CLI tool being the primary command.
Here are a few commands/tricks that I use frequently:
1. Prevent Prompts. Set Defaults.
Setting default configurations for your gcloud
CLI prevents the Y/n
prompt:
gcloud config set compute/region us-west1
gcloud config set run/platform managed
See your existing configs with gcloud config list
or see all configs 📄.
2. Getting / Change Project IDs
When using gcloud
, most commands make API requests using the currently configured project. Some commands require the project name in the command arguments.
I like to keep the current project in an environment variable like such:
Then using the name in a command is easy. For example:
gcloud builds submit --tag gcr.io/$PROJECT/helloworld
3. See if Billing is Enabled
Many Cloud services require billing to be enabled before using them.
Quickly check if your current project in gcloud
has billing enabled:
4. Authenticated Test Cloud Run
A Cloud Run service that wasn’t deployed with --allow-unauthenticated
is not accessible with unauthorized requests. A quick way to test a service is to add a HTTP authorization bearer token with the help of gcloud
:
5. Google Cloud PROJECT_ID 🔁 PROJECT_NUMBER
Sometimes an API requires a project number, but you only have a project id. With a bit of gcloud
fu, you can get one value from the other:
ID to Number
Requires PROJECT_ID
to be set.
Number to ID
Requires PROJECT_NUMBER
to be set.
6. View Raw HTTP requests
Every gcloud
command that calls a service uses a Google API. It’s easy to see exactly which API(s) we’re calling by logging the raw HTTP request.
Here’s an example:
You’ll get a response like this:
7. Interactive Mode
The Cloud SDK has an interactive CLI mode that is a great tool for learning and authoring lightweight commands. This mode shows typeahead autocompletion as well as in-terminal documentation.
Just type gcloud alpha interactive
to start.
That’s “7 gcloud Tricks You Probably Didn’t Know”. You probably did know some, but I hope you learned a new trick or two!
– Grant
If you enjoyed this article, you might also enjoy reading these cheat sheets: