Reputation: 6139
I want to stop a vm instance running on a particular project. I use
gcloud compute instances stop
I can stop a instance for a project. But when i change the current project to another project, i cant stop the instance running using the same command. Its showing the error,
gcloud compute instances stop instance-1-2
ERROR: (gcloud.compute.instances.start) Could not fetch resource: - The resource 'projects/myproject/zones/asia-southeast1-a/instances/instance-1-2' was not found
I set the project using : gcloud compute set project projectname
Upvotes: 0
Views: 4573
Reputation: 5930
I wanted to stop or start multiple instances regardless of the zone they are located in and had the same error.
my solution was to create a tag and run the following command with the sub-command output that returns the format of projects/<project-name>/zones/<zone>/instances/<instance-name>
Stop multiple instances by tag
gcloud compute instances stop $(gcloud compute instances list --project ${PROJECT} --filter="tags.items=(autoshutdown)" --format="value(selfLink.scope(v1))") --project ${PROJECT}
Start multiple instances by tag
gcloud compute instances start $(gcloud compute instances list --project ${PROJECT} --filter="tags.items=(autoshutdown)" --format="value(selfLink.scope(v1))") --project ${PROJECT}
Upvotes: 0
Reputation: 6139
If you run the command gcloud compute instances stop , it will stop the instances in the zone which you had already set.
You can set the zone using : gcloud config set compute/zone ZONE
and region using : gcloud config set compute/region REGION
where ZONE=zone which you want to assign and REGION=region you want to assign
For the above scenario , do :
gcloud config set compute/zone ZONE_OF_instance-1-2
then
gcloud compute instance stop instance-1-2
Upvotes: 3
Reputation: 8980
When you run
gcloud compute instances stop instance-1-2
this command is underscpecified, instance-1-2
is lacks project and zone. Since that was not specified on command line project (and zone) is deduced from properties.
Note that the command to set project property is NOT gcloud compute set project projectname
, but gcloud config set project projectname
.
If you want this command to work regardless of property settings you can use full path to resource
gcloud compute instances stop projects/projectname/zones/asia-southeast1-a/instances/instance-1-2
Or use command line flags
gcloud compute instances stop instance-1-2 --project projectname --zone asia-southeast1-a
Upvotes: 4