Launching a Compute Instance Using the Google Cloud Console and Cloud SDK (gcloud)

Google Cloud Platform (GCP) offers two primary methods for launching Compute Engine virtual machines (VMs): the Google Cloud Console (web interface) and the Cloud SDK (gcloud command-line tool). This guide demonstrates a hybrid approach, leveraging both tools for streamlined and customizable instance deployment.

Prerequisites

  1. Active GCP Project: Ensure you have an active Google Cloud Platform project.
  2. SSH Key Pair:
    • If needed, generate an SSH key pair on your local machine using ssh-keygen.
    • Add the public key to your project’s metadata:
      • In the Cloud Console, navigate to Compute Engine > Metadata > SSH Keys.
      • Click “Edit,” then “Add Item,” and paste your public key.
  3. Firewall Rule: Configure a firewall rule permitting ingress SSH traffic (port 22) from your authorized IP address(es).

Step 1: Initial Configuration (Google Cloud Console)

  1. Open the Cloud Console and navigate to Compute Engine > VM instances.

  2. Click Create Instance.

  3. Provide the following details:

    • Name: A descriptive name for your instance.
    • Region/Zone: The desired geographical location for your instance.
    • Machine Type: Select the appropriate vCPU and memory configuration for your workload.
    • Boot Disk:
      • Image: Choose your preferred operating system (e.g., Ubuntu, Debian).
      • Boot disk type: Typically, “Standard Persistent Disk (pd-standard)” is suitable.
      • Size: Specify the desired storage capacity.
    • Firewall: Enable “Allow HTTP traffic” and “Allow HTTPS traffic” if required.
    • Networking: Adjust network settings if you have specific requirements.
    • Advanced Options (Optional):
      • Preemptibility: If cost optimization is a priority, consider preemptible instances.
      • Availability Policy: For high availability, configure a regional policy.
  4. Click “Create” to initiate instance creation.

Step 2: Advanced Configuration (Cloud SDK)

  1. Authenticate: Ensure you are authenticated with your GCP project:

    gcloud auth login
    gcloud config set project your-project-id 
    
  2. Create Instance: Execute the following gcloud command, replacing placeholders with your specific values:

    gcloud compute instances create instance-name \
        --zone=your-zone \
        --machine-type=machine-type \
        --image=image-name \
        --image-project=image-project \
        --boot-disk-size=disk-sizeGB \
        --boot-disk-type=pd-balanced \
        --metadata-from-file=startup-script=gs://your-bucket/startup.sh \
        --tags=http-server,https-server \
        --maintenance-policy=maintenance-policy \ 
        --preemptible  # (Optional) 
    
  3. Additional Disks (Optional): To attach additional disks, use:

    gcloud compute instances attach-disk instance-name \
       --disk=disk-name \
       --zone=your-zone
    

Step 3: Connect via SSH:

gcloud compute ssh instance-name --zone=your-zone

Leave a Comment