Launching Your First GCP Compute Engine Instance

Create a Linux VM on Compute Engine from the console or gcloud, connect over SSH or IAP, keep it inside the Free Tier and clean up afterwards.

Cloud

Creating a virtual machine on Compute Engine takes a couple of minutes. The choices you make on the way decide whether it is free or billed, whether port 22 is open to the whole internet, and whether anything is still charging you after you think you’ve finished.

This article walks through one small Linux VM, first in the Google Cloud console and then with the gcloud CLI. It explains what each part of the create form does, the ways to connect, how firewall rules and network tags fit together, and what a stopped VM still costs.

What a Compute Engine VM is made of

A Compute Engine VM, which the console and API call an instance, runs in one zone of one region. Creating it ties together several resources, each with its own lifecycle:

  • A machine type: the vCPUs and memory, such as e2-micro or e2-standard-2.
  • A boot disk: a persistent disk created from a public image, such as Ubuntu or Debian, holding the operating system.
  • A network interface: a connection to a subnet of a VPC network, with an internal IP address and optionally an external one.
  • Firewall rules: defined on the VPC network, not on the VM, and applied to VMs through network tags or service accounts.
  • A service account: the identity software on the VM uses to call Google Cloud APIs.
  • Metadata: key-value pairs, including SSH keys and startup scripts.
flowchart TB
    subgraph Project["Project"]
        subgraph Net["VPC network: default (global)"]
            FW["Firewall rules, matched by tag"]
            subgraph Sub["Subnet in us-central1"]
                VM["VM: e2-micro in us-central1-a"]
            end
        end
        D["Boot disk: 10 GB standard PD"]
        SA["Service account"]
    end
    IMG["Public image: Ubuntu 24.04 LTS"] -->|"copied to"| D
    D --- VM
    SA -->|"identity for API calls"| VM
    FW -->|"filters traffic to"| VM
    U["You"] -->|"SSH, port 22"| FW

The biggest difference from AWS is where the firewall lives. On EC2 you attach a security group to the instance. On Google Cloud, a VPC network is global and its firewall rules apply across all of it; a rule picks its targets by network tag, by service account, or as “all instances in the network”. Ticking “Allow HTTP traffic” on a VM adds a tag to the VM rather than creating something attached to it.

Before you start

A project with billing. Every VM belongs to a project, and the project needs a linked billing account, even during the Free Trial. GCP Free Tier and Billing Overview explains how trial credit, Free Tier allowances and billing accounts fit together, and how to set a budget alert before you build anything.

The Compute Engine API enabled. Opening Compute Engine in the console for the first time offers to enable it. From the CLI:

gcloud services enable compute.googleapis.com

Permission to create VMs. If you own the project you already have it. In a shared project, the Compute Instance Admin (v1) role (roles/compute.instanceAdmin.v1) covers creating and managing VMs. Using a VM’s service account also needs the Service Account User role on that account. GCP Identity and Access Management (IAM) covers roles and how they are granted.

The gcloud CLI, for the second half. Install the Google Cloud CLI (the older name, Cloud SDK, still turns up in guides), then run gcloud init to sign in and choose a default project. Cloud Shell in the console has it preinstalled and already signed in, which is the quickest way to follow along.

Staying inside the Free Tier

The Compute Engine Free Tier covers one non-preemptible e2-micro VM’s worth of hours each month, plus 30 GB-months of standard persistent disk. Three conditions catch people out:

SettingFree Tier needsCommon mistake
Regionus-west1, us-central1 or us-east1Picking a region near you, such as europe-west2, which is billed in full
Machine typee2-microe2-small or e2-medium, which are not covered
Boot disk typeStandard persistent disk, 30 GB or less in totalKeeping the balanced persistent disk the form often suggests
Provisioning modelStandardChoosing Spot, which the allowance excludes

The allowance is counted in hours across the whole billing account, not per VM or per project. Two e2-micro VMs running all month use it twice over. The external IPv4 address and outbound traffic above the small free allowance are priced separately, so check the VM instance pricing and external IP address pricing pages for what currently applies.

An e2-micro is a shared-core machine: 2 vCPUs that together get a quarter of a core sustained, able to burst higher for short periods, and 1 GB of memory. That is enough for a small web server or a lab, not for a build server or database. Older guides, including an earlier version of this article, mention a free f1-micro; that offer was replaced by the e2-micro.

Creating the VM in the console

Open Compute Engine, then VM instances, and choose Create instance. The form is split into sections, listed on the left; the summary on the right shows an estimated monthly price that updates as you change settings. Here is what each section does and what to choose for a first server.

Machine configuration

  • Name: lowercase letters, digits and hyphens, such as first-vm. It must be unique within the project and zone.
  • Region and zone: us-central1 and us-central1-a keep you inside the Free Tier. A VM and its boot disk are zonal resources, so they can only move to another zone by recreating them. GCP Global Infrastructure: Regions and Zones explains which resources are zonal, regional and global.
  • Machine family and series: General purpose, series E2.
  • Machine type: e2-micro.

The estimate on the right will not show the Free Tier discount. It prices the VM as if you were paying for it, and the credit appears on your bill instead.

OS and storage

Choose Change to pick the boot disk image and type:

  • Operating system: Ubuntu, version Ubuntu 24.04 LTS for x86/64. Debian, Rocky Linux, Container-Optimized OS and others are also available. Avoid Ubuntu 20.04, which older tutorials use: it left standard support in 2025.
  • Boot disk type: Standard persistent disk, so the disk falls under the Free Tier.
  • Size: 10 GB is plenty for a lab.

Persistent disks are network block storage, separate from the VM. You can make a boot disk larger later without recreating the VM, but never smaller. Newer machine series such as C4 and N4 use Hyperdisk instead of persistent disk; E2 uses persistent disk.

Disk typeBacked byGood for
Standard persistent disk (pd-standard)HDDFree Tier labs, sequential and cold data
Balanced persistent disk (pd-balanced)SSDMost boot disks and general workloads
SSD persistent disk (pd-ssd)SSDDatabases needing low latency and high IOPS
HyperdiskVaries by typeNewer machine series, with performance set separately from size

Data protection

This section offers snapshot schedules and backups. Neither is needed for a throwaway lab; for anything you care about, a snapshot schedule on the boot disk is cheap insurance. Snapshots are billed for the storage they use, so check what you’re enabling.

Networking

  • Firewall: tick Allow HTTP traffic if you plan to serve a web page. This adds the network tag http-server to the VM, and in the default network a rule named default-allow-http allows TCP port 80 to VMs with that tag. HTTPS works the same way with https-server.
  • Network interface: leave the default network, its subnet and an ephemeral external IPv4 address.

Know what the default network already allows. It comes with rules named default-allow-ssh (TCP 22 from anywhere), default-allow-rdp (TCP 3389 from anywhere), default-allow-icmp and default-allow-internal. Below those sit two implied rules every network has: deny all ingress and allow all egress. So any VM with an external IP in the default network has port 22 reachable from the internet. The IAP section below shows how to close it.

Some organizations disable the default network with an organization policy. If the network list is empty, you need to create a VPC network and its firewall rules first.

Security

The VM gets the Compute Engine default service account unless you pick another. Software on the VM can use that identity to call Google Cloud APIs, within the limits of the account’s IAM roles and the VM’s access scopes. For a lab that doesn’t call any APIs, you can choose no service account at all. For real workloads, create a dedicated service account with only the roles the application needs.

Advanced: a startup script

Under Advanced, the Automation box takes a startup script. Compute Engine stores it as metadata under the key startup-script and runs it as root on every boot, not only the first. That is a difference from EC2 user data, so write scripts that are safe to run twice.

This one installs nginx on Ubuntu:

#!/bin/bash
apt-get update
apt-get install -y nginx
systemctl enable --now nginx

Choose Create. The VM appears in the list, and its status moves from PROVISIONING and STAGING to RUNNING within a minute or so. With HTTP allowed, opening http:// and the external IP in a browser shows the nginx welcome page once the script finishes.

You tick "Allow HTTP traffic" when creating a VM in the default network. What does that change on the VM itself?

  • It attaches a new firewall to the VM's network interface
  • It adds the http-server network tag, which a network-wide rule targets
  • It opens every port on the external IP address

Correct! Firewall rules live on the VPC network; the checkbox adds a tag that the default-allow-http rule matches.

Connecting to the VM

You don’t need to create or download a key pair first. Google Cloud generates one when you connect and places the public half on the VM, either through metadata or, if OS Login is enabled, through your Google identity.

MethodExternal IP neededFirewall source needed for port 22How access is controlled
SSH-in-browserNoGoogle’s ranges (or IAP’s range, without an external IP)IAM permissions on the project and VM
gcloud compute sshYes, unless you add --tunnel-through-iapYour IP, or IAP’s rangeIAM plus the key gcloud manages
Your own SSH clientYesYour IPA key you add to metadata, or OS Login

SSH-in-browser

In the VM list, choose SSH in the Connect column. A browser window opens with a shell. This works with the default rules because default-allow-ssh accepts connections from anywhere. Clipboard paste and file upload and download are built into the window.

gcloud compute ssh

From your terminal or Cloud Shell:

gcloud compute ssh first-vm --zone=us-central1-a

The first run creates a key pair at ~/.ssh/google_compute_engine and adds the public key to the project’s metadata (or your OS Login profile). After that it is a normal OpenSSH session, and gcloud compute scp copies files the same way.

The login user name comes from your local user name when keys are managed through metadata, and from your Google account (for example jo_example_com) under OS Login. Using OS Login is the better habit: access is granted and revoked through IAM roles such as roles/compute.osLogin, instead of by editing keys in metadata.

SSH through IAP without an external IP

Identity-Aware Proxy (IAP) TCP forwarding lets you reach port 22 on a VM that has no external IP. Your connection goes to IAP over HTTPS, IAP checks your IAM permissions, and then connects to the VM from the range 35.235.240.0/20.

sequenceDiagram
    participant You as Your terminal
    participant IAP as Identity-Aware Proxy
    participant FW as Firewall rule
    participant VM as VM (no external IP)
    You->>IAP: Tunnel request over HTTPS
    IAP->>IAP: Check IAM role
    IAP->>FW: TCP 22 from 35.235.240.0/20
    FW->>VM: Allowed by rule
    VM-->>You: SSH session through the tunnel

To switch a lab VM to this model, allow SSH only from IAP’s range, remove the open rule, and connect with the extra flag:

gcloud compute firewall-rules create allow-ssh-from-iap \
  --network=default \
  --direction=INGRESS \
  --action=ALLOW \
  --rules=tcp:22 \
  --source-ranges=35.235.240.0/20

gcloud compute firewall-rules delete default-allow-ssh

gcloud compute ssh first-vm --zone=us-central1-a --tunnel-through-iap

You need the IAP-secured Tunnel User role (roles/iap.tunnelResourceAccessor), which project owners already have. Once SSH works through IAP, the VM no longer needs an external IP for administration, only for serving the public.

A new VM in the default network has an external IP. Before you change anything, who can reach its port 22?

  • Anyone on the internet, because of default-allow-ssh
  • Only your own IP address
  • Nobody, until you create a firewall rule

Correct! The default network includes default-allow-ssh with source 0.0.0.0/0; only the key protects the login.

The same VM with gcloud

The console is the best way to see the options. The CLI is how you make the setup repeatable. These commands create the same VM as above, assuming gcloud init has set your project.

Save the startup script from earlier as startup.sh, then:

gcloud compute instances create first-vm \
  --zone=us-central1-a \
  --machine-type=e2-micro \
  --image-family=ubuntu-2404-lts-amd64 \
  --image-project=ubuntu-os-cloud \
  --boot-disk-type=pd-standard \
  --boot-disk-size=10GB \
  --tags=http-server \
  --metadata-from-file=startup-script=startup.sh
  • --image-family with --image-project picks the newest image in a family that the image publisher maintains, so you never hard-code an image name that will be deprecated next month. Ubuntu images live in the ubuntu-os-cloud project.
  • --tags=http-server matches the default-allow-http rule, if the network has it.
  • The command prints the VM’s internal and external IP when it finishes.

If the default-allow-http rule doesn’t exist yet, because no one has ticked the checkbox in this project, create it:

gcloud compute firewall-rules create default-allow-http \
  --network=default \
  --direction=INGRESS \
  --action=ALLOW \
  --rules=tcp:80 \
  --source-ranges=0.0.0.0/0 \
  --target-tags=http-server

To check the result:

gcloud compute instances list
gcloud compute instances get-serial-port-output first-vm --zone=us-central1-a | tail -n 20

The serial port output is the boot log, including the startup script’s output. It is the first place to look when a VM starts but its service doesn’t.

For anything you create more than a couple of times, describe it in Terraform or Infrastructure Manager so the VM, its firewall rules and its service account are created and deleted together.

Stop, suspend, reset and delete

What you pay for depends on the VM’s state, and the names can mislead: a stopped VM shows the status TERMINATED, even though nothing has been deleted.

flowchart LR
    P["PROVISIONING"] --> S["STAGING"]
    S --> R["RUNNING"]
    R -->|"reset"| R
    R -->|"stop"| T["TERMINATED (stopped)"]
    T -->|"start"| P
    R -->|"suspend"| SU["SUSPENDED"]
    SU -->|"resume"| R
    R -->|"delete"| X["Deleted"]
    T -->|"delete"| X
  • Running: vCPU and memory are billed per second, after a one-minute minimum, along with the disk and the external IP.
  • Reset: a hard restart, like pressing the reset button. The VM keeps its IPs and disks. Memory contents are lost.
  • Stopped (TERMINATED): vCPU and memory charges stop. Persistent disks and any static external IP are still billed. An ephemeral external IP is released, so the VM usually gets a new one when you start it again.
  • Suspended: the memory contents are saved and the VM resumes where it left off. You pay for the disks and for storing the saved memory, but not for vCPUs.
  • Deleted: the VM is gone. Its boot disk is deleted too if auto-delete is on, which is the default for boot disks created with the VM. Additional disks you attached keep their own setting.

For a lab, stop the VM between sessions and delete it when you’re done. If you need a fixed address, reserve a static external IP, and release it afterwards: a reserved address that isn’t attached to anything is billed at a higher rate than one in use.

Your VM shows the status TERMINATED in the console. What is true?

  • The VM and its boot disk have been deleted
  • Nothing about it is billed any more
  • It is stopped; its disk is still billed and you can start it again

Correct! TERMINATED is how Compute Engine reports a stopped VM; the persistent disk keeps its data and its charges.

Choosing a machine type later

The e2-micro is right for learning. When you outgrow it, the decision is mostly about the machine family:

FamilyExample seriesUse it for
General purpose, cost-optimizedE2Web servers, dev environments, small apps
General purpose, balancedN2, N4, C4Production apps and databases with steady load
Compute-optimizedC2, H3CPU-bound work such as HPC and game servers
Memory-optimizedM3Large in-memory databases
Accelerator-optimizedA3, G2GPU training and inference

E2 VMs don’t receive sustained use discounts, which apply automatically only to some older series such as N1 and N2. Committed use discounts and Spot VMs, which replace the older preemptible VMs, are the main ways to pay less. Exploring GCP Compute Engine Instances goes deeper on the families, and Pricing Models: On-Demand, Reserved, Savings Plans, and Spot compares the discount options across the three big providers.

You can change a VM’s machine type after creation, but only while it is stopped.

Cleaning up

Delete everything the exercise created, not just the VM:

gcloud compute instances delete first-vm --zone=us-central1-a
gcloud compute disks list
gcloud compute addresses list
gcloud compute firewall-rules list

The first command also deletes the boot disk, because auto-delete is on. The list commands are there to catch leftovers: an unattached disk, a reserved static IP or a firewall rule you added. Delete any you created with the matching delete command. Firewall rules cost nothing, but an allow rule left in place applies to the next VM that carries the matching tag.

In the console, the same steps are: select the VM in VM instances and choose Delete; then check Disks, and under VPC network, IP addresses and Firewall. A day later, check the Reports page in Billing to confirm the experiment has stopped costing money. If the whole project was only for this exercise, shutting the project down removes everything in it at once.

The same exercise on the other providers is in Creating Your First AWS EC2 Instance and Creating Your First Azure Virtual Machine. The concepts map closely: a security group or network security group plays the part of Google Cloud’s tag-based firewall rules.

Further reading

Key takeaways

  • A Compute Engine VM combines a machine type, a boot disk from a public image, a network interface, tag-matched firewall rules, a service account and metadata.
  • For the Free Tier, use an e2-micro in us-west1, us-central1 or us-east1 with a standard persistent disk, and remember the allowance is shared across the billing account.
  • Pick a current image such as Ubuntu 24.04 LTS through --image-family; Ubuntu 20.04 has left standard support.
  • The default network opens SSH and RDP to the internet. Replace default-allow-ssh with a rule for IAP’s range and connect with --tunnel-through-iap.
  • Startup scripts run on every boot, and their output is in the serial port log.
  • A stopped VM shows as TERMINATED but its disks and static IPs are still billed. Delete the VM and check for leftover disks, addresses and rules when you’re done.

Part 6 of 7 · Exploring Google Cloud Platform (GCP)

  1. 01Introduction to Google Cloud Platform (GCP)
  2. 02Navigating the GCP Console
  3. 03GCP Global Infrastructure
  4. 04GCP Identity and Access Management
  5. 05GCP Free Tier and Billing Overview
  6. 06Launching Your First GCP Compute Engine Instance
  7. 07Navigating the GCP Support Maze: A Guide to Resources
Ready to check your understanding?Take the Fluffy Computing quiz
esc

Type to search all articles · ↑↓ to move · ↵ to open