Bird
Raised Fist0
GCPcloud~10 mins

Scripting with gcloud in GCP - Step-by-Step Execution

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
Process Flow - Scripting with gcloud
Write gcloud commands in script
Run script in terminal
gcloud CLI processes commands
Commands execute on GCP services
Output shown in terminal
Script ends or loops for next command
This flow shows how a script with gcloud commands is written, run, and executed step-by-step to interact with Google Cloud services.
Execution Sample
GCP
gcloud config set project my-project

gcloud compute instances list

gcloud compute instances create my-vm --zone=us-central1-a
This script sets the active project, lists compute instances, then creates a new VM instance.
Process Table
StepCommandActionResultOutput
1gcloud config set project my-projectSet active projectProject set to my-projectUpdated property [core/project].
2gcloud compute instances listList VM instancesRetrieved list of instancesNo instances found.
3gcloud compute instances create my-vm --zone=us-central1-aCreate VM instanceInstance creation startedCreated [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/my-vm].
4End of scriptScript finishesAll commands executedScript execution complete.
💡 All commands executed successfully; script ends.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
ACTIVE_PROJECTnonemy-projectmy-projectmy-projectmy-project
INSTANCE_LISTemptyemptyemptyemptyempty
INSTANCE_CREATEDfalsefalsefalsetruetrue
Key Moments - 2 Insights
Why does the instance list show 'No instances found' even after setting the project?
Because the project was just set and no VM instances exist yet, as shown in step 2 of the execution_table.
What happens if the project is not set before running compute commands?
gcloud commands will fail or use a default project, causing errors or unexpected results. Setting the project first (step 1) avoids this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after running 'gcloud compute instances list'?
AProject set to my-project.
BInstance created successfully.
CNo instances found.
DError: project not set.
💡 Hint
Check row 2, Output column in the execution_table.
At which step does the script create a new VM instance?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the Command column and Action column in the execution_table.
If the project was not set in step 1, how would the output of step 3 likely change?
AInstance creation would succeed as normal.
BAn error about missing project would occur.
CThe instance list would show existing instances.
DThe script would skip instance creation.
💡 Hint
Consider the importance of setting ACTIVE_PROJECT in variable_tracker and step 1 in execution_table.
Concept Snapshot
Scripting with gcloud:
- Write gcloud commands in a script file.
- Run script in terminal to execute commands sequentially.
- Set project first to avoid errors.
- Commands interact with GCP services and show output.
- Script ends after all commands run.
Full Transcript
This visual execution trace shows how scripting with gcloud works. First, you write commands like setting the project, listing instances, and creating a VM. When you run the script, each command executes in order. The project must be set first to ensure commands run in the right context. The output after each command shows success or results, such as no instances found or instance creation confirmation. Variables like ACTIVE_PROJECT and INSTANCE_CREATED track the script's state. This helps beginners see how gcloud commands run step-by-step in a script.

Practice

(1/5)
1. What does the gcloud compute instances list command do?
easy
A. It deletes all virtual machine instances.
B. It creates a new virtual machine instance.
C. It shows all virtual machine instances in your project.
D. It updates the configuration of an instance.

Solution

  1. Step 1: Understand the command structure

    The command uses gcloud compute instances list, which is designed to list resources.
  2. Step 2: Identify the resource targeted

    The resource targeted is virtual machine instances under compute service.
  3. Final Answer:

    It shows all virtual machine instances in your project. -> Option C
  4. Quick Check:

    List command = show resources [OK]
Hint: List commands show resources, create commands add resources [OK]
Common Mistakes:
  • Confusing list with create or delete commands
  • Assuming it modifies instances instead of listing
  • Ignoring the service and resource part of the command
2. Which of the following is the correct syntax to create a new Compute Engine instance named my-vm in zone us-central1-a using gcloud?
easy
A. gcloud create instances my-vm --zone us-central1-a
B. gcloud instances create my-vm --zone us-central1-a
C. gcloud compute create instance my-vm zone=us-central1-a
D. gcloud compute instances create my-vm --zone=us-central1-a

Solution

  1. Step 1: Check the correct command order

    The correct order is gcloud compute instances create followed by the instance name.
  2. Step 2: Verify flag syntax

    The zone flag must be --zone=us-central1-a with an equals sign.
  3. Final Answer:

    gcloud compute instances create my-vm --zone=us-central1-a -> Option D
  4. Quick Check:

    Correct syntax uses 'compute instances create' and '--zone=' [OK]
Hint: Use full service and resource names with flags using '=' [OK]
Common Mistakes:
  • Omitting 'compute' or 'instances' keywords
  • Using space instead of '=' in flags
  • Wrong command order or missing flags
3. What will be the output of this command if there are two instances named vm1 and vm2 in zone us-east1-b?

gcloud compute instances list --filter="zone:(us-east1-b)" --format="value(name)"
medium
A. vm1 vm2
B. vm1\nvm2
C. ["vm1", "vm2"]
D. Error: Invalid filter syntax

Solution

  1. Step 1: Understand the filter flag

    The filter limits results to instances in zone us-east1-b, so both vm1 and vm2 match.
  2. Step 2: Understand the format flag

    The format value(name) outputs only the names, each on a new line.
  3. Final Answer:

    vm1\nvm2 -> Option B
  4. Quick Check:

    Filter + value format = names on separate lines [OK]
Hint: value(name) outputs names line by line, not space separated [OK]
Common Mistakes:
  • Expecting space-separated names instead of new lines
  • Thinking output is JSON array
  • Misreading filter syntax as invalid
4. You wrote this script line to delete a Compute Engine instance:

gcloud compute instances delete my-instance --zone us-west1-b

But it fails with an error. What is the most likely cause?
medium
A. Missing '=' sign in the --zone flag
B. Instance name is incorrect
C. Delete command requires --force flag
D. gcloud command does not support delete

Solution

  1. Step 1: Check flag syntax

    The zone flag must be written as --zone=us-west1-b with an equals sign.
  2. Step 2: Confirm command support

    The delete command is valid and does not require --force unless confirmation is skipped.
  3. Final Answer:

    Missing '=' sign in the --zone flag -> Option A
  4. Quick Check:

    Flags need '=' between flag and value [OK]
Hint: Flags require '=' between name and value [OK]
Common Mistakes:
  • Using space instead of '=' in flags
  • Assuming delete needs --force always
  • Thinking delete command is unsupported
5. You want to write a script that creates three Compute Engine instances named app-1, app-2, and app-3 in zone europe-west1-c. Which script snippet correctly uses a loop with gcloud commands?
hard
A. for i in 1 2 3; do gcloud compute instances create app-$i --zone=europe-west1-c; done
B. gcloud compute instances create app-1 app-2 app-3 --zone europe-west1-c
C. for i in 1..3; gcloud compute instances create app-i --zone=europe-west1-c; done
D. gcloud create instances app-{1..3} --zone=europe-west1-c

Solution

  1. Step 1: Understand shell loop syntax

    The correct bash loop syntax is for i in 1 2 3; do ... done.
  2. Step 2: Check command inside loop

    The command uses variable substitution app-$i and correct flag --zone=europe-west1-c.
  3. Step 3: Verify other options

    gcloud compute instances create app-1 app-2 app-3 --zone europe-west1-c creates all instances in one command but does not use a loop. Options A and D have syntax errors or invalid commands.
  4. Final Answer:

    for i in 1 2 3; do gcloud compute instances create app-$i --zone=europe-west1-c; done -> Option A
  5. Quick Check:

    Bash loop with correct flags and variable substitution [OK]
Hint: Use 'for i in 1 2 3; do ... done' for loops in bash [OK]
Common Mistakes:
  • Using invalid loop syntax like '1..3'
  • Missing 'do' and 'done' in loop
  • Incorrect flag syntax without '='
  • Trying to create multiple instances without loop or correct command