MARTIN OMANDER: Hey, Grant.
GRANT TIMMERMAN: Oh hey, Martin.
MARTIN OMANDER: Hey, I've been using the gcloud command line
tool from the Cloud SDK a lot recently,
and I'd like to get a bit more productive with it.
Do you have any tips or tricks?
GRANT TIMMERMAN: I sure do.
I have six tricks up my sleeve to help you
with your productivity when using gcloud.
MARTIN OMANDER: Cool.
Tell me more.
[MUSIC PLAYING]
As you know, I'm trying to learn the gcloud command line
tool a bit more.
And there are new flags that I'm unfamiliar with,
like all the new features of Cloud Run.
Is there a way I can quickly learn these new features
and flags?
GRANT TIMMERMAN: Yes, there is.
So here's a fun one.
gcloud has an interactive mode where you can see documentation
as you type.
To start interactive mode, run gcloud beta interactive.
This will start a session in your terminal providing
autocompletion and inline documentation as you type.
So for example, Cloud Run has a lot of feature flags
you can specify.
If you type gcloud run deploy --,
you can go and toggle through all the flags.
MARTIN OMANDER: Oh, cool.
Yeah, that's --max-instances.
That's the one I was looking for.
Cool.
All right.
Great.
Thank you for that tip.
For number two, here's a different issue.
When I deploy to Cloud Functions or to Cloud Run,
I keep getting the same prompts in gcloud
when running those commands.
So like it asks me for my location for my Cloud Run
service, or it asks me to specify
I want to deploy to Cloud Run Managed or to Kubernetes.
So I have to write these large scripts with many command line
flags for this.
How do I avoid that?
GRANT TIMMERMAN: Oh, do you know about gcloud configs?
MARTIN OMANDER: No, I don't.
GRANT TIMMERMAN: Oh, let me show you.
So gcloud configs allow you to save configuration options
so you don't need to get prompted for them
and you don't need to repeat them.
So the general syntax is gcloud config set, and then
your property and value.
So for example, when deploying to Cloud Run,
you could run the command of gcloud run deploy myapp,
and then you provide your --image.
But then you might notice, oh, you get prompted, OK,
do you want Cloud Run fully managed
and which region do you want?
MARTIN OMANDER: Right.
GRANT TIMMERMAN: Right.
So let's go ahead and cancel that.
Or you could go and set your config.
So you can set your config with gcloud config set
run/platform managed, then maybe your run/region
to europe-west3.
So if I do that, then it'll update the properties
and I can simply rerun the previous command.
And you'll notice now, I'm not prompted--
automatically just working.
MARTIN OMANDER: Oh, nice.
I like fewer prompts.
OK, cool.
For number three, sometimes, I need to include the project ID
with a command.
I also switch between projects often.
And sometimes, I use the wrong project.
And I have actually, in the past,
sometimes deployed code to the wrong project.
My manager did not like that.
Is there an easier way to manage the projects?
GRANT TIMMERMAN: Yeah, there is.
So I've faced this, too.
It's a little annoying specifying your project name
everywhere.
And I don't really like to mix up deploying
between different projects.
That's sort of scary.
But with gcloud, you can get and set your current project.
So in your scripts, you don't need
to hardcode your project ID.
So in the command line, you can set the project
with gcloud config set project and then your project name.
Once you have done that, then that
will be your default project with all the gcloud commands.
And you can also get your current project
and store it in an environment variable.
How do you do that?
Well, you can go and type--
you can go and type--
let's say you want your project.
Here's a little script to set your project
inside this environment variable.
Then you can use this environment variable anywhere,
like when using gcloud.
So here's a little example.
When using gcloud build submit, you can provide your tag
and you always need your project.
And so it'll just recognize that and work.
So I use this all the time in my scripts
when deploying to Cloud Run.
MARTIN OMANDER: Oh, nice.
No more deploying code to the wrong project for me.
So for number four, I switch around
between a few different projects,
and I sometimes forget which projects have billing enabled.
Many Cloud Services require billing
to be enabled before using them.
So how can I check that, Grant?
GRANT TIMMERMAN: Oh, that's a good question, Martin.
So with gcloud, there's a relatively new command
you can use to go and check this.
You can run the following gcloud beta billing command.
So after you run this, you get your project.
You describe it.
And you'll get a Boolean value of True or False.
And so this will indicate if billing's
enabled for that project.
MARTIN OMANDER: Oh, cool.
Now I can check if billing is enabled for all my projects
without having to go to the web browser and the Cloud Console
over there.
GRANT TIMMERMAN: Yep.
MARTIN OMANDER: All right.
So for number five, when I've deployed a private Cloud Run
service, I can't really test it using curl.
If I try to curl it, I get the response, error, forbidden.
How do I fix that?
GRANT TIMMERMAN: Yep.
So a Cloud Run service that wasn't deployed
with --allow-unathenticated is not
accessible with unauthorized requests.
So for example, we can go and try
curling a Cloud Run service, and we'll see an error.
For example, like 403 forbidden.
Not anybody on the public web can test our proprietary Cloud
Run service.
MARTIN OMANDER: That's what I'm getting, yep.
GRANT TIMMERMAN: Yep.
But that's really very difficult to test.
It's great that we have security,
but we want to be able to test locally.
So by adding our HTTP auth bearer token when curling,
we can go and fix this issue.
And so here, we'll just provide a header with our authorization
token from gcloud.
And you can see, Heyo, Martin!
MARTIN OMANDER: It works.
Nice.
I'll use that, for sure.
So but, Grant, so this worked, but whose access level
was used now for this command?
GRANT TIMMERMAN: That's a really great question.
So this command uses the same authorization
as the person who logged into that machine
with gcloud auth login.
MARTIN OMANDER: Ah, nice.
All right.
For number six, sometimes when using Google Cloud,
I'm supposed to use a project number rather than a project
ID.
This is, for example, when I'm using service accounts
or creating service accounts.
Is there a way to get that project number
if I have the project ID?
GRANT TIMMERMAN: Oh, yeah.
So well, with a little bit of gcloud-fu,
you can go and get one value from the other.
So with this little command--
I use this all the time--
you can go and get your project number
if you have your project ID.
So what it does in the background
is it lists your projects and gets your project ID
and then formats it just for that number.
So you might use this with different credentials,
or especially like with Cloud Storage.
And likewise, let's say you have that project number,
but you want to get your ID.
And so you can do the reverse.
And you can go list your projects
and get that project ID.
And there, you get your project ID.
MARTIN OMANDER: All right.
Thanks for showing me these six incredibly useful tips, Grant.
I didn't know you can do all of that stuff
from the command line.
GRANT TIMMERMAN: Oh, you're quite welcome, Martin.
I keep discovering new features and new tips to myself.
I guess you learn something new every day
MARTIN OMANDER: Oh, yes.
I've learned six new things today.
Thank you, everyone, for watching.
We hope you found these tips useful.
And please enter any questions or comments below.
See you next time.
[MUSIC PLAYING]