Script to delete oldest VM in a project.
$ python3 cleanup.py --project_id GOOGLE_CLOUD_PROJECT_ID_NAME \
--zone GCP_ZONE \
--service_name_label SERVICE_TAG
Example:
$ python3 cleanup.py --project_id my-gcp-project123456789 \
--zone us-central1-a \
--service_name_label my-service
--project_id
Google Cloud Project ID.
--zone
Google Cloud zone in which the VM exists in.
--service_name_label
A label on the Compute VM in the format of service: LABEL
.
So if the label is service: database
then use the field --service_name_label database
.
Make sure to use the label feature on the VM and not the tag.
- Make a Service Account with the capability to read, write, and delete Compute VMs.
- When creating the VMs, attach the Service Account you created to the VM.
- Make a Service Account with the capability to read, write, and delete Compute VMs.
- Authenticate on local machine.
a. Generate the API keys and runexport GOOGLE_APPLICATION_CREDENTIALS=location of your API KEY file
Make sure to use the label feature on the VM and not the tag.