GCloud Compute
This skill provides a consolidated guide for managing Google Cloud Compute Engine instances directly using the gcloud CLI.
1. Setup Guide
If gcloud is not yet installed or configured, follow these steps:
Installation
- Debian/Ubuntu:
# Add Package Source sudo apt-get install -y apt-transport-https ca-certificates gnupg curl curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee /etc/apt/sources.list.d/google-cloud-sdk.list # Install sudo apt-get update && sudo apt-get install -y google-cloud-cli - Other Linux Distributions:
curl https://sdk.cloud.google.com | bash exec -l $SHELL - macOS:
brew install --cask google-cloud-sdk
Initialization & Authentication
- Initialize gcloud:
gcloud init - Web-free Login: If you are in a remote environment or prefer no browser, run:
gcloud auth login --no-launch-browser- Copy the provided URL into your local browser.
- Authorize access and copy the verification code.
- Paste the code back into your terminal.
2. Project Management
After logging in, you must select or create a Google Cloud Project.
A. List Existing Projects
Check if you already have a project to use:
gcloud projects list
B. Create a New Project
If you don't have a project, or want a new one for this VM, follow these steps:
- Create the project:
Note: [PROJECT_ID] must be unique across all of Google Cloud.gcloud projects create [PROJECT_ID] --name="[PROJECT_NAME]" - Set as active:
gcloud config set project [PROJECT_ID] - Enable Compute Engine API:
Note: This requires billing to be enabled for the project.gcloud services enable compute.googleapis.com
3. VM Management Workflow
Follow this workflow to create and connect to a VM.
Step A: Evaluate Machine Type and Cost
Before creating an instance, check the available machine types and their specifications to estimate costs.
List Machine Types: Display available machine types and their core/memory specs in your zone.
gcloud compute machine-types list --zones=[ZONE]Note: Costs vary by machine type. For example,
e2-microis generally the cheapest, whilee2-standard-4provides more resources at a higher price.Estimate Cost: Use the Google Cloud Pricing Calculator for precise estimates based on your selected machine type and region.
Step B: Create an Instance
Mandatory: You must provide a unique name for your instance.
gcloud compute instances create [MANDATORY_INSTANCE_NAME] \
--zone=[ZONE] \
--machine-type=[MACHINE_TYPE] \
--image-family=debian-12 \
--image-project=debian-cloud
Tip: Common zones include us-central1-a, asia-northeast3-a (Seoul). Recommended machine types: e2-micro (development), e2-small (standard).
Step C: Connect via SSH
Once the instance is running, connect using:
gcloud compute ssh [INSTANCE_NAME] --zone=[ZONE]
4. Quick Reference
| Action | Command |
|---|---|
| List Instances | gcloud compute instances list |
| Stop Instance | gcloud compute instances stop [NAME] |
| Start Instance | gcloud compute instances start [NAME] |
| Delete Instance | gcloud compute instances delete [NAME] |
| Check Config | gcloud config list |
Tips for Efficiency
- Preemptible VMs: Add
--preemptibleto the create command to save up to 80% on costs for short-lived tasks. - Static External IP: If you need a constant IP, use
--address=[IP_NAME].