0
0
GCPcloud~5 mins

Instance states (running, stopped, terminated) in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create a virtual machine in the cloud, it can be in different states like running, stopped, or terminated. These states tell you if the machine is working, paused, or deleted. Understanding these states helps you manage your cloud resources efficiently.
When you want to start a virtual machine to run your applications.
When you need to pause a virtual machine to save costs but keep its data.
When you want to permanently delete a virtual machine you no longer need.
When you want to check the current status of your virtual machines.
When you want to troubleshoot why a virtual machine is not responding.
Commands
This command creates a new virtual machine named 'example-instance' in the 'us-central1-a' zone. The instance will start in the running state automatically.
Terminal
gcloud compute instances create example-instance --zone=us-central1-a
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-instance].
--zone - Specifies the zone where the instance will be created.
This command lists the instance named 'example-instance' and shows its current state, so you can verify it is running.
Terminal
gcloud compute instances list --filter="name=example-instance" --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.194.64 RUNNING
--filter - Filters the list to show only the instance with the specified name.
--zones - Limits the list to the specified zone.
This command stops the running instance named 'example-instance'. The instance state changes to stopped, which means it is not running but still exists with its data.
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.
Check the instance state again to confirm it is now stopped.
Terminal
gcloud compute instances list --filter="name=example-instance" --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.194.64 TERMINATED
--filter - Filters the list to show only the instance with the specified name.
--zones - Limits the list to the specified zone.
This command deletes the instance named 'example-instance' permanently. The instance is removed and cannot be started again.
Terminal
gcloud compute instances delete example-instance --zone=us-central1-a --quiet
Expected OutputExpected
Deleted [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-instance].
--zone - Specifies the zone of the instance to delete.
--quiet - Skips confirmation prompt to delete the instance immediately.
Key Concept

If you remember nothing else from this pattern, remember: running means the instance is active, stopped means it is paused but still exists, and terminated means it is deleted permanently.

Common Mistakes
Trying to start an instance that has been deleted (terminated).
A terminated instance no longer exists and cannot be started again.
Create a new instance instead of trying to start a deleted one.
Confusing 'stopped' with 'terminated' and deleting data unintentionally.
Stopping an instance keeps its data, but terminating deletes it permanently.
Use 'stop' to pause and keep data; use 'delete' only when you want to remove the instance completely.
Not specifying the correct zone when managing instances.
Commands fail or affect the wrong instance if the zone is incorrect or missing.
Always specify the correct zone with the --zone flag for instance commands.
Summary
Create an instance to start it in the running state.
Use the list command to check the instance's current state.
Stop the instance to pause it without deleting data.
Delete the instance to remove it permanently.