GCP Managed Instance Group (MIG)

Note: If you delete resources before creating them to make your script idempotent, you need to put a wait time for the resources to be fully deleted before creating them again. Otherwise, you will get an error that the resource already exists.

Create MIG

ENV_MIG_NAME=whisper-worker-mig
ENV_INSTANCE_TEMPLATE_NAME=whisper-worker-template


instance_template_name=${ENV_INSTANCE_TEMPLATE_NAME}
# Create instance-templates for whisper.cpp worker VMs on GCP.
    gcloud compute instance-templates create ${instance_template_name} \
        --project=cosmic-kiln-99999 \
        --machine-type=e2-medium \
        --provisioning-model=SPOT \
        --image-family=debian-12 \
        --image-project=debian-cloud \
        --scopes=cloud-platform \
        --tags="whisper-worker"

mig_name=${ENV_MIG_NAME}
# Create a managed instance group (MIG) using the instance template with zero instance.
    gcloud compute instance-groups managed create ${mig_name} \
        --project=cosmic-kiln-99999 \
        --zone=us-central1-a \
        --template=${instance_template_name} \
        --size=0

Resize instances

# Increase to 3 instances in the MIG.
    mig_name="whisper-worker-mig"
    gcloud compute instance-groups managed resize ${mig_name} \
        --zone=us-central1-a \
        --project=cosmic-kiln-99999 \
        --size=3

# List instances created in the MIG.
    mig_name="whisper-worker-mig"
    gcloud compute instance-groups managed list-instances ${mig_name} \
        --zone=us-central1-a \
        --project=cosmic-kiln-99999

View the instance output

# Replace INSTANCE_NAME with the actual name returned from the previous command (e.g., whisper-worker-mig-xxxx)
instance_name=whisper-worker-mig-dnv1
gcloud compute instances get-serial-port-output ${instance_name} \
    --zone=us-central1-a \
    --project=cosmic-kiln-99999 \
    --port=1

Delete all

ENV_MIG_NAME=whisper-worker-mig
ENV_INSTANCE_TEMPLATE_NAME=whisper-worker-template

# Delete the Managed Instance Group (MIG)
    mig_name=${ENV_MIG_NAME}
    gcloud compute instance-groups managed delete ${mig_name} \
        --zone=us-central1-a \
        --project=cosmic-kiln-99999 \
        --quiet

# Delete All Instance Templates
    instance_template_name=${ENV_INSTANCE_TEMPLATE_NAME}
    gcloud compute instance-templates delete ${instance_template_name} \
        --project=cosmic-kiln-99999 \
        --quiet

# Verify Everything Is Gone: # Should return 0 results
    gcloud compute instance-groups managed list --project=cosmic-kiln-99999
    gcloud compute instance-templates list --project=cosmic-kiln-99999

Debug

# Get info of mig
    mig_name="whisper-worker-mig"
    gcloud compute instance-groups managed describe ${mig_name} \
        --zone=us-central1-a \
        --project=cosmic-kiln-99999