Skip to main content

Command Palette

Search for a command to run...

Automate VM Management with Google Cloud SDK

Automate VM Management with Google Cloud SDK

Published
3 min read

Prerequisites:

  • A Google Cloud Platform (GCP) account.

  • Google Cloud SDK installed on your local machine or access to Cloud Shell.

  1. Set up Google Cloud SDK and Authenticate

    If you're using a local machine with the SDK installed:

    • Login to your GCP account:

        gcloud auth login
      
    • Set your GCP project:

        gcloud config set project YOUR_PROJECT_ID
      

Alternatively, if using Cloud Shell, you don’t need to install or authenticate, as it's already configured.

  1. Create a Virtual Machine (VM)

    Use the gcloud compute instances create command to create a new VM instance. Here's how to do it:

    • Create a basic VM:

        gcloud compute instances create my-vm-instance \
            --zone=us-central1-c \
            --machine-type=n1-standard-1 \
            --image-family=debian-11 \
            --image-project=debian-cloud
      

Options Breakdown:

  • --zone: The region/zone where the VM will be deployed.

  • --machine-type: Specifies the type of machine (e.g., n1-standard-1).

  • --image-family: Defines the OS type (e.g., Debian 11).

  • --image-project: Specifies the project hosting the image (debian-cloud in this case).

    Your VM is created successfully.

  1. List VMs and Check Status

    To view a list of your VMs or check the status of an existing VM, run:

     gcloud compute instances list
    

  2. Start, Stop, or Reboot a VM

    • Stop a VM:

        gcloud compute instances stop my-vm-instance --zone=us-central1-c
      

      As you can see, VM is stopped.

    • Start a VM:

        gcloud compute instances start my-vm-instance --zone=us-central1-c
      

      As you can see, VM is started.

    • Reboot a VM:

        gcloud compute instances reset my-vm-instance --zone=us-central1-c
      

      Instance is rebooted.

  3. SSH into the VM

    To connect to your VM using SSH:

     gcloud compute ssh my-vm-instance --zone=us-central1-c
    
    • Press Y when prompted

    • Press Enter → Enter (or you can also give passphrase)

      You are now connected via SSH.

    • Press exit to come back to cloudshell.

  4. Resize the VM

    If you need to change the machine type (e.g., from n1-standard-1 to n1-standard-2), stop the VM first, then resize it:

     gcloud compute instances stop my-vm-instance --zone=us-central1-c
     gcloud compute instances set-machine-type my-vm-instance \
         --zone=us-central1-c \
         --machine-type=n1-standard-2
     gcloud compute instances start my-vm-instance --zone=us-central1-c
    

    We have successfully resized our instance.

  5. Delete a VM

    To delete a VM when it's no longer needed:

     gcloud compute instances delete my-vm-instance --zone=us-central1-c
    
    • If prompted Yes or No - Press Y

      Instance is deleted.

  6. Automating with Scripts

    To fully automate these tasks, you can write a Bash script containing the gcloud commands. For example:

    • In the Cloud Shell terminal, create a new script file using the nano text editor:

        nano vm-management.sh
      
    • Paste the following script into the file:

    #!/bin/bash

    # Set project
    gcloud config set project YOUR_PROJECT_ID

    # Create a VM
    gcloud compute instances create my-vm-instance \
        --zone=us-central1-c \
        --machine-type=n1-standard-1 \
        --image-family=debian-11 \
        --image-project=debian-cloud

    # Wait for the VM to be ready
    sleep 60

    # SSH into the VM and run a command
    gcloud compute ssh my-vm-instance --zone=us-central1-c --command="uname -a"

    # Stop the VM
    gcloud compute instances stop my-vm-instance --zone=us-central1-c

    # Delete the VM
    gcloud compute instances delete my-vm-instance --zone=us-central1-c --quiet

Save and exit nano by pressing CTRL + X, then press Y and hit Enter.

  • Make the script executable and run it:
    chmod +x vm-management.sh
    ./vm-management.sh

Conclusion

Using the gcloud SDK and scripting, you can automate the entire lifecycle of VM instances in GCP, from creation to deletion.

"Amazing work automating your VM management with Google Cloud SDK! The power of automation is now in your handsstay tuned for more hands-on GCP blogs to keep supercharging your cloud experience!"

GCP VM Series: From Zero to Compute

Part 13 of 14

Get ready for an exciting ride in Google Cloud! Our series takes you through every method of creating virtual machines in GCP, complete with engaging tutorials and practical labs. Elevate your cloud game and become a VM virtuoso!

Up next

Schedule a Task on VM (Using Cronjob)

Set up a cron job to automate recurring tasks on your VM.