gcloud compute instances create hoge-vm --zone=us-west1-b --scopes=compute-rw --metadata startup-script='#!/bin/bash
NAME=$(curl metadata.google.internal/computeMetadata/v1/instance/name -H "Metadata-Flavor: Google")
ZONE=$(curl metadata.google.internal/computeMetadata/v1/instance/zone -H "Metadata-Flavor: Google" | cut -d/ -f4)
gcloud compute instances delete $NAME --zone=$ZONE --quiet'
インスタンスを作成して即削除できます。他に実行したい処理があれば、startup-scriptに書いてください
--scopes=compute-rw
がないと権限ないと怒られる
hoge-vm login: Apr 24 01:23:07 hoge-vm google_metadata_script_runner[481]: startup-script: ERROR: (gcloud.compute.instances.delete) Could not fetch resource:
Apr 24 01:23:07 hoge-vm google_metadata_script_runner[481]: startup-script: - Request had insufficient authentication scopes.
Apr 24 01:23:07 hoge-vm google_metadata_script_runner[481]: startup-script:
Apr 24 01:23:07 hoge-vm google_metadata_script_runner[481]: startup-script exit status 1
Apr 24 01:23:07 hoge-vm google_metadata_script_runner[481]: Finished running startup scripts.
参考