0
0
GCPcloud~7 mins

Compute commands (instances, disks) in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Creating and managing virtual machines and their storage disks in the cloud can be confusing. These commands help you start, check, and stop virtual machines and their disks easily on Google Cloud Platform.
When you want to create a new virtual machine to run your application.
When you need to attach extra storage to your virtual machine.
When you want to check the status of your virtual machines and disks.
When you want to stop a virtual machine to save costs.
When you want to delete a disk that is no longer needed.
Commands
This command creates a new virtual machine named 'example-instance' in the 'us-central1-a' zone with a medium machine type and Debian 11 operating system.
Terminal
gcloud compute instances create example-instance --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-instance]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-instance us-central1-a e2-medium 10.128.0.2 34.68.123.45 RUNNING
--zone - Specifies the zone where the instance will be created.
--machine-type - Defines the hardware resources for the instance.
--image-family - Selects the OS image family for the instance.
--image-project - Specifies the project that contains the OS image.
This command creates a new persistent disk named 'example-disk' with 50GB size in the same zone as the instance.
Terminal
gcloud compute disks create example-disk --size=50GB --zone=us-central1-a
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/disks/example-disk].
--size - Sets the size of the disk.
--zone - Specifies the zone where the disk will be created.
This command attaches the previously created disk 'example-disk' to the virtual machine 'example-instance'.
Terminal
gcloud compute instances attach-disk example-instance --disk=example-disk --zone=us-central1-a
Expected OutputExpected
Attached disk [example-disk] to instance [example-instance].
--disk - Specifies the disk to attach.
--zone - Specifies the zone of the instance.
This command lists all virtual machines in the 'us-central1-a' zone to check their status and details.
Terminal
gcloud compute instances list --zones=us-central1-a
Expected OutputExpected
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-instance us-central1-a e2-medium 10.128.0.2 34.68.123.45 RUNNING
--zones - Filters the list to a specific zone.
This command stops the virtual machine 'example-instance' to save costs when it is not needed.
Terminal
gcloud compute instances stop example-instance --zone=us-central1-a
Expected OutputExpected
Stopped [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-instance].
--zone - Specifies the zone of the instance to stop.
Key Concept

If you remember nothing else from this pattern, remember: use gcloud commands with the right zone and resource names to create, manage, and check your virtual machines and disks.

Common Mistakes
Not specifying the zone when creating or managing instances and disks.
The command fails or affects resources in the wrong zone, causing confusion or errors.
Always include the --zone flag with the correct zone for your resources.
Trying to attach a disk that is in a different zone than the instance.
Disks and instances must be in the same zone to attach; otherwise, the command fails.
Create disks and instances in the same zone before attaching.
Forgetting to stop instances when not in use.
Running instances incur costs even if idle.
Stop instances with gcloud compute instances stop to save money.
Summary
Create virtual machines with 'gcloud compute instances create' specifying zone, machine type, and OS image.
Create and attach persistent disks to instances in the same zone using 'gcloud compute disks create' and 'gcloud compute instances attach-disk'.
List instances to check their status and stop them when not needed to save costs.