Murchie85
Murchie85

Reputation: 865

Gcloud overriding region, zone and machine-type in a single command

I'm working my way through GCP ACE material, and when creating a compute instance you can do the following:

gcloud compute instance create --machine-type=f1-micro mylovelyVM

This is all good, but I want to know how to override region and zone in the same command, I know I can just do config set the region and zone but would be nice to know if it's possible in the one line command.

I have tried the following to no avail :

gcloud compute instances create --machine-type=f1-micro mylovelyVM --region us-west2 --zone us-west2-b

gcloud compute instances create --region us-west2 --zone us-west2-b --machine-type=f1-micro mylovelyVM

I have tried variations on this.

I have been using the -h and --help commands to try and work out at what point to specify the region, but so far no luck.

Error message is --region (did you mean '--reservation'?) for every iteration, which leads me to think its not expecting --region flag at that point.

Documentation states the flow should be gcloud <global flags> <service/product> <group/area> <command> <flags> <parameters>

In which case the command should be

gcloud compute --region us-west2 --zone us-west2-b instances create --machine-type=f1-micro mylovelyVM 

Is there a limit on chaining overrides or am I doing this wrong?

Upvotes: 1

Views: 710

Answers (2)

Murchie85
Murchie85

Reputation: 865

Marking Daz's answer as correct, though I did find a way without specifying project :

gcloud compute instances create my-lovelyVM --zone=us-west2-b --machine-type=f1-micro

Upvotes: 0

DazWilkin
DazWilkin

Reputation: 40061

You create instances in zones not regions. Regions comprise multiple zones and, while some other services are regional, (compute engine) instances must be placed in zones.

So, use only --zone=.. and not --region=...

The documentation is good but gcloud compute instances create has many flags and so it can be confusing:

https://cloud.google.com/sdk/gcloud/reference/compute/instances/create

Using Cloud Console, you can try permutations of the command and, at the bottom, there's an option to have Console show you the equivalent REST or Cloud SDK (gcloud) command. This is helpful.

IMO, it is good practice to specify e.g. --zone, --project and other flags on the command-line rather than use config. It's more typing but it's more explicit and can avoid errors that result from implicit (assumed) values from config.

Best wishes for your learning!

Update

gcloud compute instances create mylovelyvm \
--zone=us-west2-b \
--machine-type=f1-micro \
--project=${PROJECT}
Created [https://www.googleapis.com/compute/v1/projects/${PROJECT}/zones/us-west2-b/instances/mylovelyvm].
NAME        ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
mylovelyvm  us-west2-b  f1-micro                   10.168.0.2   35.236.23.189  RUNNING

Upvotes: 3

Related Questions