0
0
GCPcloud~5 mins

Creating a VM instance in GCP - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
Sometimes you need a computer in the cloud to run your programs or store data. Creating a VM instance means making a virtual computer that you can use remotely. This helps you run apps without needing your own physical machine.
When you want to test software on a clean computer without affecting your own device
When you need a server to host a website or application accessible from anywhere
When you want to run a database that multiple users can access securely
When you need extra computing power for a short time without buying hardware
When you want to learn cloud computing by practicing on a virtual machine
Commands
This command creates a new virtual machine named 'example-vm' in the 'us-central1-a' zone. It uses a medium-sized machine type and a Debian 11 operating system image. This sets up the VM ready to use.
Terminal
gcloud compute instances create example-vm --zone=us-central1-a --machine-type=e2-medium --image-family=debian-11 --image-project=debian-cloud
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-vm]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-vm us-central1-a e2-medium 10.128.0.2 34.68.123.45 RUNNING
--zone - Specifies the location where the VM will be created
--machine-type - Defines the size and power of the VM
--image-family - Chooses the operating system family for the VM
--image-project - Specifies the project that contains the OS image
This command lists the details of the VM named 'example-vm' to confirm it was created and is running.
Terminal
gcloud compute instances list --filter="name=example-vm"
Expected OutputExpected
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-vm us-central1-a e2-medium 10.128.0.2 34.68.123.45 RUNNING
--filter - Filters the list to show only the VM with the specified name
This command connects you to the VM named 'example-vm' using SSH so you can use it like a remote computer.
Terminal
gcloud compute ssh example-vm --zone=us-central1-a
Expected OutputExpected
WARNING: The SSH command is using OS Login. If you want to disable OS Login, see https://cloud.google.com/compute/docs/oslogin. Welcome to Debian GNU/Linux 11 (bullseye)! example-vm:~$
--zone - Specifies the zone of the VM to connect to
Key Concept

If you remember nothing else from this pattern, remember: creating a VM instance means making a virtual computer in the cloud that you can use remotely.

Common Mistakes
Not specifying the zone when creating or connecting to the VM
The command fails because the cloud needs to know where to create or find the VM
Always include the --zone flag with the correct zone name
Using an invalid or unsupported machine type
The VM creation fails because the machine type does not exist or is not available in the chosen zone
Check available machine types for the zone and use a valid one like e2-medium
Trying to SSH into the VM before it is fully created and running
The SSH connection will fail because the VM is not ready to accept connections
Verify the VM status is RUNNING before connecting with SSH
Summary
Use 'gcloud compute instances create' with proper flags to make a VM instance.
Check the VM status with 'gcloud compute instances list' filtered by name.
Connect to the VM using 'gcloud compute ssh' once it is running.