Bird
Raised Fist0
GCPcloud~5 mins

Cost optimization strategies in GCP - Commands & Configuration

Choose your learning style10 modes available
Introduction
Cloud costs can grow quickly if resources are not managed well. Cost optimization strategies help you use cloud services efficiently to save money without losing performance.
When you want to reduce your monthly cloud bill by avoiding unused resources.
When you need to choose the right machine types for your workloads to avoid overpaying.
When you want to schedule resources to run only when needed, like shutting down test servers at night.
When you want to use discounts like committed use contracts or sustained use discounts.
When you want to monitor and get alerts on unexpected cost spikes.
Commands
This command lists all your virtual machines so you can see which ones are running and might be costing you money.
Terminal
gcloud compute instances list
Expected OutputExpected
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-vm us-central1-a e2-medium False 10.128.0.2 34.68.123.45 RUNNING
Stops the example-vm instance to save costs when it is not needed, like during off-hours.
Terminal
gcloud compute instances stop example-vm --zone=us-central1-a
Expected OutputExpected
Stopped [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-vm].
→
--zone - Specifies the zone where the VM is located.
Lists available machine types so you can choose a smaller or cheaper machine if your workload allows.
Terminal
gcloud compute machine-types list --zones=us-central1-a
Expected OutputExpected
NAME ZONE CPUS MEMORY_GB DEPRECATED e2-micro us-central1-a 2 1 e2-small us-central1-a 2 2 e2-medium us-central1-a 2 4
→
--zones - Filters machine types by zone.
Changes the machine type of example-vm to a smaller one to reduce costs.
Terminal
gcloud compute instances set-machine-type example-vm --machine-type=e2-small --zone=us-central1-a
Expected OutputExpected
Updated [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-vm].
→
--machine-type - Specifies the new machine type.
→
--zone - Specifies the zone of the instance.
Creates a budget alert to notify you when your spending reaches 80% of $100, helping you avoid surprises.
Terminal
gcloud beta billing budgets create --display-name="Monthly Budget" --budget-amount=100 --threshold-rules=threshold-percent=0.8
Expected OutputExpected
Created budget: projects/1234567890/budgets/9876543210
→
--budget-amount - Sets the budget amount in USD.
→
--threshold-rules - Sets the alert threshold percentage.
Key Concept

If you remember nothing else from cost optimization, remember: regularly check your running resources and adjust or stop what you don't need to save money.

Common Mistakes
Stopping instances without checking if they have persistent disks attached.
You might lose data if disks are not persistent or snapshots are not taken.
Always verify disk types and back up important data before stopping or deleting instances.
Choosing machine types that are too large for the workload.
You pay more for unused CPU and memory resources.
Use monitoring tools to understand resource usage and select the smallest machine type that meets your needs.
Not setting budget alerts.
You may not notice when costs spike unexpectedly.
Create budget alerts to get notified early and take action.
Summary
List running virtual machines to identify resources that can be stopped or resized.
Stop or resize instances to reduce costs when full capacity is not needed.
Set budget alerts to monitor spending and avoid unexpected charges.

Practice

(1/5)
1. Which of the following is a basic way to reduce costs in Google Cloud Platform (GCP)?
easy
A. Use the most powerful machines regardless of need
B. Increase the number of virtual machines
C. Use smaller or cheaper machine types
D. Keep all resources running 24/7

Solution

  1. Step 1: Understand cost drivers in GCP

    Machine types with more CPU and memory cost more money.
  2. Step 2: Choose cost-saving options

    Using smaller or cheaper machine types reduces the hourly cost.
  3. Final Answer:

    Use smaller or cheaper machine types -> Option C
  4. Quick Check:

    Smaller machines cost less [OK]
Hint: Pick smaller machines to save money quickly [OK]
Common Mistakes:
  • Thinking more machines always save money
  • Believing running all resources nonstop is cheaper
  • Choosing the most powerful machines by default
2. Which of the following is the correct way to specify a preemptible VM in a GCP instance creation command?
easy
A. gcloud compute instances create my-vm --preemptible
B. gcloud compute instances create my-vm --no-preemptible
C. gcloud compute instances create my-vm --machine-type=preemptible
D. gcloud compute instances create my-vm --enable-preemptible=false

Solution

  1. Step 1: Review gcloud syntax for preemptible VMs

    The flag to create a preemptible VM is --preemptible.
  2. Step 2: Identify the correct command

    gcloud compute instances create my-vm --preemptible uses --preemptible correctly to enable preemptible VM.
  3. Final Answer:

    gcloud compute instances create my-vm --preemptible -> Option A
  4. Quick Check:

    Use --preemptible flag to create preemptible VM [OK]
Hint: Look for --preemptible flag to enable preemptible VMs [OK]
Common Mistakes:
  • Using --no-preemptible which disables preemptible
  • Confusing machine-type with preemptible flag
  • Using incorrect flag names
3. Consider this Python snippet using GCP SDK to list VM instances and filter by machine type for cost optimization:
instances = [
  {"name": "vm1", "machine_type": "n1-standard-1"},
  {"name": "vm2", "machine_type": "n1-highmem-4"},
  {"name": "vm3", "machine_type": "n1-standard-1"}
]
cheap_vms = [vm["name"] for vm in instances if vm["machine_type"] == "n1-standard-1"]
print(cheap_vms)
What is the output?
medium
A. ['vm2']
B. ['vm1', 'vm3']
C. ['vm1', 'vm2', 'vm3']
D. []

Solution

  1. Step 1: Understand the list comprehension filter

    The code selects VMs where machine_type equals 'n1-standard-1'.
  2. Step 2: Identify matching VMs

    vm1 and vm3 have 'n1-standard-1', vm2 does not.
  3. Final Answer:

    ['vm1', 'vm3'] -> Option B
  4. Quick Check:

    Filter by machine_type 'n1-standard-1' gives vm1 and vm3 [OK]
Hint: Filter list by machine_type to find cheaper VMs [OK]
Common Mistakes:
  • Including vm2 which has a different machine type
  • Returning empty list by mistake
  • Confusing keys in dictionary
4. You wrote this command to create a preemptible VM but it keeps creating normal VMs:
gcloud compute instances create cheap-vm --machine-type=n1-standard-1 --enable-preemptible=false
What is the error and how to fix it?
medium
A. The VM name is invalid; rename it
B. The machine type is wrong; use a cheaper type
C. The command is missing zone specification
D. The flag disables preemptible; use --preemptible instead

Solution

  1. Step 1: Analyze the preemptible flag usage

    The flag --enable-preemptible=false disables preemptible VMs.
  2. Step 2: Correct the flag to enable preemptible

    Use --preemptible without false to create preemptible VM.
  3. Final Answer:

    The flag disables preemptible; use --preemptible instead -> Option D
  4. Quick Check:

    Use --preemptible to enable preemptible VMs [OK]
Hint: Use --preemptible flag alone to enable preemptible VMs [OK]
Common Mistakes:
  • Using --enable-preemptible=false disables preemptible
  • Changing machine type instead of flag
  • Ignoring flag syntax errors
5. A company wants to optimize costs by automatically stopping VMs when not in use during nights and weekends. Which GCP service or strategy best supports this goal?
hard
A. Use Cloud Scheduler to trigger Cloud Functions that stop/start VMs on schedule
B. Manually stop VMs every night using the console
C. Use preemptible VMs only without scheduling
D. Increase VM size to reduce runtime

Solution

  1. Step 1: Identify automation options for VM management

    Cloud Scheduler can run tasks on a schedule, and Cloud Functions can execute commands like stopping VMs.
  2. Step 2: Match automation to cost optimization goal

    Automating stop/start reduces costs by not running VMs when unused.
  3. Final Answer:

    Use Cloud Scheduler to trigger Cloud Functions that stop/start VMs on schedule -> Option A
  4. Quick Check:

    Automate VM stop/start with Cloud Scheduler and Functions [OK]
Hint: Automate VM stop/start with Cloud Scheduler and Functions [OK]
Common Mistakes:
  • Relying on manual stopping which is error-prone
  • Using preemptible VMs without scheduling
  • Increasing VM size which raises costs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard