0
0
GCPcloud~5 mins

Cost optimization strategies in GCP - Commands & Configuration

Choose your learning style9 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.