· Subodh Gupta · Cloud Computing

Your Go-To Guide: Managing Google Cloud Virtual Machines (GCP VM Cheatsheet)

GCP VM Cheatsheet: Essential gcloud commands for Instance, Disk, & Network Management. A cheatsheet for GCP Cloud Engineers.

GCP VM Cheatsheet: Essential gcloud commands for Instance, Disk, & Network Management. A cheatsheet for GCP Cloud Engineers.

Your Go-To Guide: Managing Google Cloud Virtual Machines (GCP VM Cheatsheet)

This cheatsheet provides a quick reference for working with Virtual Machines (VMs) on Google Cloud Platform (GCP) using the gcloud command-line interface and key concepts.

Core Concepts:

  • Compute Engine: GCP’s Infrastructure-as-a-Service (IaaS) offering for creating and managing VMs.
  • Instance: A virtual machine hosted on Google’s infrastructure.
  • Zone: A deployment area within a region (e.g., us-central1-a). Provides fault isolation within a region.
  • Region: A geographical area consisting of one or more zones (e.g., us-central1).
  • Machine Type: Defines the virtualized hardware resources available to an instance (CPU, memory, etc.). Examples: n1-standard-1, e2-medium.
  • Image: A template used to create instances. Can be public (Google-provided), custom, or marketplace images.
  • Disk: Persistent storage attached to an instance. Can be standard persistent disks (HDD) or SSD persistent disks.
  • Network: A virtual network that provides connectivity for your instances.
  • Firewall Rules: Control network traffic to and from your instances.
  • IP Addresses: Instances can have ephemeral (temporary) or static (reserved) external and internal IP addresses.
  • Metadata: Key/value pairs that can be configured on instances and accessed from within the VM. Used for configuration and management.
  • Service Account: A special type of Google account that belongs to your application or VM, rather than to an individual end user. Used for authentication and authorization.

gcloud compute Commands:

Instance Management:

  • Create an instance:

    gcloud compute instances create INSTANCE_NAME \
        --zone=ZONE \
        --machine-type=MACHINE_TYPE \
        --image-family=IMAGE_FAMILY \
        --image-project=IMAGE_PROJECT \
        --boot-disk-size=SIZE \
        --boot-disk-type=DISK_TYPE
    • INSTANCE_NAME: Name of your VM instance.
    • ZONE: GCP zone where you want to create the instance (e.g., us-central1-a).
    • MACHINE_TYPE: Predefined machine type (e.g., n1-standard-1) or custom machine type (custom-CPU-MEMORY).
    • IMAGE_FAMILY: Family of the OS image (e.g., debian-11).
    • IMAGE_PROJECT: Project containing the image (e.g., debian-cloud).
    • SIZE: Boot disk size in GB (e.g., 50GB).
    • DISK_TYPE: Type of boot disk (pd-standard, pd-ssd).
  • List instances:

    gcloud compute instances list --zone=ZONE
    gcloud compute instances list --region=REGION
    gcloud compute instances list --project=PROJECT_ID
  • Describe an instance:

    gcloud compute instances describe INSTANCE_NAME --zone=ZONE
  • Start an instance:

    gcloud compute instances start INSTANCE_NAME --zone=ZONE
  • Stop an instance:

    gcloud compute instances stop INSTANCE_NAME --zone=ZONE
  • Reset an instance (hard reset):

    gcloud compute instances reset INSTANCE_NAME --zone=ZONE
  • Delete an instance:

    gcloud compute instances delete INSTANCE_NAME --zone=ZONE
    • Add --keep-disks=boot,data to preserve disks.
  • Connect to an instance using SSH:

    gcloud compute ssh INSTANCE_NAME --zone=ZONE
  • Copy files to an instance:

    gcloud compute scp LOCAL_FILE INSTANCE_NAME:/REMOTE_PATH --zone=ZONE
    gcloud compute scp --recurse LOCAL_DIR INSTANCE_NAME:/REMOTE_PATH --zone=ZONE
  • Copy files from an instance:

    gcloud compute scp INSTANCE_NAME:/REMOTE_FILE LOCAL_PATH --zone=ZONE
    gcloud compute scp --recurse INSTANCE_NAME:/REMOTE_DIR LOCAL_PATH --zone=ZONE

Disk Management:

  • Create a disk:

    gcloud compute disks create DISK_NAME \
        --zone=ZONE \
        --size=SIZE \
        --type=DISK_TYPE
  • List disks:

    gcloud compute disks list --zone=ZONE
    gcloud compute disks list --region=REGION
  • Describe a disk:

    gcloud compute disks describe DISK_NAME --zone=ZONE
  • Attach a disk to an instance:

    gcloud compute instances attach-disk INSTANCE_NAME \
        --disk=DISK_NAME \
        --zone=ZONE \
        --device-name=DEVICE_NAME \
        --mode=READ_WRITE
    • DEVICE_NAME: Name of the device within the VM (e.g., sdb).
  • Detach a disk from an instance:

    gcloud compute instances detach-disk INSTANCE_NAME \
        --disk=DISK_NAME \
        --zone=ZONE
  • Delete a disk:

    gcloud compute disks delete DISK_NAME --zone=ZONE

Image Management:

  • List available images:

    gcloud compute images list --project=IMAGE_PROJECT
    gcloud compute images list --filter="family=IMAGE_FAMILY" --project=IMAGE_PROJECT
  • Create a custom image from a disk:

    gcloud compute images create IMAGE_NAME \
        --source-disk=DISK_NAME \
        --source-disk-zone=ZONE
  • Delete an image:

    gcloud compute images delete IMAGE_NAME

Network & Firewall:

  • List networks:

    gcloud compute networks list
  • Describe a network:

    gcloud compute networks describe NETWORK_NAME
  • List firewall rules:

    gcloud compute firewall-rules list --network=NETWORK_NAME
  • Create a firewall rule:

    gcloud compute firewall-rules create FIREWALL_RULE_NAME \
        --network=NETWORK_NAME \
        --allow=PROTOCOL:PORT[,PROTOCOL:PORT,...] \
        --source-ranges=IP_RANGE[,IP_RANGE,...] \
        --target-tags=TAG[,TAG,...]
    • PROTOCOL: e.g., tcp, udp, icmp.
    • PORT: e.g., 80, 22-23.
    • IP_RANGE: e.g., 0.0.0.0/0 (any IP).
    • TAG: Network tags applied to instances.
  • Delete a firewall rule:

    gcloud compute firewall-rules delete FIREWALL_RULE_NAME --network=NETWORK_NAME

IP Addresses:

  • List addresses:

    gcloud compute addresses list --region=REGION
  • Reserve a static external IP address:

    gcloud compute addresses create ADDRESS_NAME --region=REGION
  • Assign a static external IP address to an instance:

    gcloud compute instances add-network-interface INSTANCE_NAME \
        --zone=ZONE \
        --network-interface-name=nic0 \
        --address=STATIC_IP_ADDRESS
    • Or during instance creation using --network-interface.
  • Release a static external IP address:

    gcloud compute addresses delete ADDRESS_NAME --region=REGION

Service Accounts:

  • Create a service account:

    gcloud iam service-accounts create SERVICE_ACCOUNT_NAME \
        --display-name="DISPLAY_NAME"
  • Add a service account to an instance:

    gcloud compute instances update INSTANCE_NAME --zone=ZONE \
        --service-account=SERVICE_ACCOUNT_EMAIL \
        --scopes=SCOPE1,SCOPE2,...
    • SCOPE: Defines the permissions granted to the service account (e.g., https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/compute.readonly).

Metadata:

  • Set instance metadata during creation:

    gcloud compute instances create INSTANCE_NAME --zone=ZONE \
        --metadata=key1=value1,key2=value2
  • Set instance metadata on an existing instance:

    gcloud compute instances update INSTANCE_NAME --zone=ZONE \
        --metadata=key1=new_value1,key3=value3
  • Get instance metadata:

    gcloud compute instances describe INSTANCE_NAME --zone=ZONE --format='value(metadata.items)'
    • Access metadata from within the VM using curl "http://metadata.google.internal/computeMetadata/v1/" -H "Metadata-Flavor: Google".

Snapshots:

  • Create a snapshot of a disk:

    gcloud compute snapshots create SNAPSHOT_NAME --disk=DISK_NAME --disk-zone=ZONE
  • List snapshots:

    gcloud compute snapshots list
  • Create a disk from a snapshot:

    gcloud compute disks create DISK_NAME --source-snapshot=SNAPSHOT_NAME --zone=ZONE
  • Delete a snapshot:

    gcloud compute snapshots delete SNAPSHOT_NAME

Templates:

  • Create an instance template:

    gcloud compute instance-templates create TEMPLATE_NAME \
        --machine-type=MACHINE_TYPE \
        --image-family=IMAGE_FAMILY --image-project=IMAGE_PROJECT \
        --boot-disk-size=SIZE --boot-disk-type=DISK_TYPE \
        --network=NETWORK_NAME \
        --tags=TAG1,TAG2
  • List instance templates:

    gcloud compute instance-templates list
  • Create an instance from a template:

    gcloud compute instances create INSTANCE_NAME --source-instance-template=TEMPLATE_NAME --zone=ZONE
  • Delete an instance template:

    gcloud compute instance-templates delete TEMPLATE_NAME

Important Notes:

  • Replace placeholders like INSTANCE_NAME, ZONE, etc., with your actual values.
  • Use --help after any gcloud compute command to see more options and detailed usage.
  • Refer to the official Google Cloud documentation for the most up-to-date information and advanced configurations.
  • Consider using Infrastructure as Code (IaC) tools like Terraform or Deployment Manager for managing your GCP resources at scale.

This cheatsheet provides a starting point for working with Compute Engine. Explore the gcloud compute command-line official documentation for more advanced features and options.

Back to Blog

Related Posts

View All Posts »