Bird
Raised Fist0
GCPcloud~7 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the command gcloud compute instances list do?
easy
A. It shows all virtual machines in your project.
B. It creates a new virtual machine.
C. It deletes a virtual machine.
D. It attaches a disk to a virtual machine.

Solution

  1. Step 1: Understand the command purpose

    The command gcloud compute instances list is used to display existing virtual machines in your project.
  2. Step 2: Compare with other options

    Creating, deleting, or attaching disks require different commands, not this one.
  3. Final Answer:

    It shows all virtual machines in your project. -> Option A
  4. Quick Check:

    List instances = show VMs [OK]
Hint: List commands show current resources, not create or delete [OK]
Common Mistakes:
  • Confusing list with create or delete commands
  • Not specifying resource type
  • Assuming it modifies resources
2. Which of the following is the correct syntax to create a new VM instance named web-server in zone us-central1-a?
easy
A. gcloud instances create web-server --zone=us-central1-a
B. gcloud create instance web-server zone us-central1-a
C. gcloud compute instances create web-server --zone=us-central1-a
D. gcloud compute create instance web-server --zone us-central1-a

Solution

  1. Step 1: Identify correct command structure

    The correct command starts with gcloud compute instances create followed by the instance name and zone flag.
  2. Step 2: Check option syntax

    gcloud compute instances create web-server --zone=us-central1-a matches the correct syntax exactly with proper flags and order.
  3. Final Answer:

    gcloud compute instances create web-server --zone=us-central1-a -> Option C
  4. Quick Check:

    Create instance syntax = gcloud compute instances create web-server --zone=us-central1-a [OK]
Hint: Remember: 'gcloud compute instances create' + name + --zone [OK]
Common Mistakes:
  • Mixing order of words in command
  • Omitting 'compute' or 'instances'
  • Using wrong flag format
3. What will be the output of the command gcloud compute disks list --filter="zone:(us-central1-a)" if there are two disks named disk1 and disk2 in zone us-central1-a and one disk named disk3 in zone us-east1-b?
medium
A. Lists disk1, disk2, and disk3
B. Shows an error due to filter syntax
C. Lists only disk3
D. Lists only disk1 and disk2

Solution

  1. Step 1: Understand the filter usage

    The filter limits results to disks in zone us-central1-a only.
  2. Step 2: Apply filter to disk list

    Disks disk1 and disk2 are in us-central1-a, so they appear; disk3 is in a different zone and is excluded.
  3. Final Answer:

    Lists only disk1 and disk2 -> Option D
  4. Quick Check:

    Filter by zone shows matching disks only [OK]
Hint: Filter narrows results to matching zone disks only [OK]
Common Mistakes:
  • Assuming filter includes all disks
  • Misreading filter syntax
  • Expecting syntax error incorrectly
4. You run gcloud compute instances delete my-vm but get an error saying the zone is missing. What is the best fix?
medium
A. Add --project=PROJECT_ID flag instead.
B. Add the flag --zone=ZONE_NAME with the correct zone.
C. Run the command without any flags again.
D. Use gcloud delete instances my-vm instead.

Solution

  1. Step 1: Identify missing required parameter

    The error indicates the zone is not specified, which is required to delete an instance.
  2. Step 2: Correct the command by adding zone

    Adding --zone=ZONE_NAME specifies the location of the instance to delete.
  3. Final Answer:

    Add the flag --zone=ZONE_NAME with the correct zone. -> Option B
  4. Quick Check:

    Zone flag required for instance delete [OK]
Hint: Always specify zone when deleting or managing instances [OK]
Common Mistakes:
  • Ignoring zone requirement
  • Using wrong command syntax
  • Assuming project flag fixes zone error
5. You want to create a new persistent disk named data-disk of size 100GB in zone europe-west1-b and attach it to an existing instance app-server. Which sequence of commands is correct?
hard
A. 1) gcloud compute disks create data-disk --size=100GB --zone=europe-west1-b
2) gcloud compute instances attach-disk app-server --disk=data-disk --zone=europe-west1-b
B. 1) gcloud compute instances attach-disk app-server --disk=data-disk --zone=europe-west1-b
2) gcloud compute disks create data-disk --size=100GB --zone=europe-west1-b
C. 1) gcloud compute disks create data-disk --zone=europe-west1-b
2) gcloud compute instances attach-disk app-server --disk=data-disk
D. 1) gcloud compute disks create data-disk --size=100 --zone=europe-west1-b
2) gcloud compute instances attach-disk app-server --disk=data-disk --zone=europe-west1-b

Solution

  1. Step 1: Create the disk first with correct size and zone

    The disk must be created before attaching. 1) gcloud compute disks create data-disk --size=100GB --zone=europe-west1-b
    2) gcloud compute instances attach-disk app-server --disk=data-disk --zone=europe-west1-b uses correct size format '100GB' and specifies zone.
  2. Step 2: Attach the created disk to the instance with zone specified

    Attaching requires the disk and instance zone flags; 1) gcloud compute disks create data-disk --size=100GB --zone=europe-west1-b
    2) gcloud compute instances attach-disk app-server --disk=data-disk --zone=europe-west1-b includes these correctly.
  3. Final Answer:

    First create disk with size and zone, then attach with disk and zone flags. -> Option A
  4. Quick Check:

    Create disk before attach, specify size with GB [OK]
Hint: Create disk before attach; size needs units like GB [OK]
Common Mistakes:
  • Attaching disk before creating it
  • Omitting size units (GB)
  • Missing zone flags on commands