0
0
GCPcloud~5 mins

Scripting with gcloud in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to automate tasks in Google Cloud instead of doing them by hand. Scripting with gcloud lets you write commands that run one after another to manage your cloud resources quickly and reliably.
When you want to create multiple virtual machines without clicking in the console each time
When you need to update firewall rules regularly based on changing needs
When you want to back up data from a storage bucket every day automatically
When you want to deploy an app and set up its environment in one go
When you want to check the status of your cloud resources and save the results
Commands
This command sets your active Google Cloud project so that all following commands apply to it.
Terminal
gcloud config set project example-project-123
Expected OutputExpected
Updated property [core/project].
This command creates a new virtual machine named example-vm in the specified zone with a medium machine type.
Terminal
gcloud compute instances create example-vm --zone=us-central1-a --machine-type=e2-medium
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/example-project-123/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 zone where the VM will be created
--machine-type - Defines the size and power of the VM
This command lists all virtual machines in your current project, so you can verify your VM was created.
Terminal
gcloud compute instances list
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
This command deletes the example-vm virtual machine without asking for confirmation because of the --quiet flag.
Terminal
gcloud compute instances delete example-vm --zone=us-central1-a --quiet
Expected OutputExpected
Deleted [https://www.googleapis.com/compute/v1/projects/example-project-123/zones/us-central1-a/instances/example-vm].
--quiet - Skips confirmation prompts for automation
Key Concept

If you remember nothing else from this pattern, remember: scripting with gcloud lets you automate cloud tasks by running commands in order without manual clicks.

Common Mistakes
Not setting the active project before running commands
Commands may run in the wrong project or fail because the project is not specified
Always run 'gcloud config set project your-project-id' before other commands
Forgetting to specify the zone when creating or deleting VMs
The command will fail or act on the wrong zone because zone is required for these operations
Always include the --zone flag with the correct zone value
Running delete commands without the --quiet flag in scripts
The script will pause waiting for confirmation, breaking automation
Use --quiet to skip confirmation prompts in scripts
Summary
Set the active project with 'gcloud config set project' to target your cloud resources.
Create and manage virtual machines using 'gcloud compute instances' commands with proper flags.
Use 'gcloud compute instances list' to check the status of your VMs.
Add the --quiet flag in scripts to avoid manual confirmation prompts during deletions.