TL;DR
Master Google Cloud cost management and optimization. Learn about billing accounts, budgets, cost controls, resource optimization, and best practices for managing cloud costs effectively.
GCP Cost Management and Optimization Guide
Google Cloud Platform offers various tools and best practices for managing and optimizing cloud costs. This guide covers key strategies and implementation details for effective cost management.
$1
`` graph TB
subgraph CostManagement["Cost Management"]
direction TB
subgraph Billing["Billing Tools"]
direction LR
BA["Billing Account"]
BUD["Budgets"]
EXP["Exports"]
end
subgraph Controls["Cost Controls"]
direction LR
QUOT["Quotas"]
CAP["Caps"]
ALERT["Alerts"]
end
subgraph Optimization["Optimization"]
direction LR
REC["Recommendations"]
DISC["Discounts"]
RIGHT["Right-sizing"]
end
end
subgraph Monitoring["Cost Monitoring"]
direction TB
DASH["Dashboards"]
REPORT["Reports"]
ANALY["Analytics"]
end
CostManagement --> Monitoring
classDef primary fill:#4285f4,stroke:#666,stroke-width:2px,color:#fff
classDef secondary fill:#34a853,stroke:#666,stroke-width:2px,color:#fff
classDef tertiary fill:#fbbc05,stroke:#666,stroke-width:2px,color:#fff
class CostManagement,Billing primary
class Controls,Optimization secondary
class Monitoring tertiary
mermaid
`
$1
$1
` gcloud beta billing accounts create \
--display-name="My Billing Account" gcloud beta billing projects link my-project \
--billing-account=0X0X0X-0X0X0X-0X0X0X
bash
`Create billing account
Link project to billing account
$1
` budgets:
- name: monthly-budget
amount:
specifiedAmount:
currencyCode: USD
units: 1000
thresholdRules:
- spendBasis: CURRENT_SPEND
thresholdPercent: 0.5
- spendBasis: CURRENT_SPEND
thresholdPercent: 0.9
- spendBasis: FORECASTED_SPEND
thresholdPercent: 1.0
notificationsChannelIds:
- "projects/my-project/notificationChannels/12345"
yaml
`budget.yaml
$1
$1
` gcloud compute project-info describe \
--project=my-project gcloud compute quotas update \
--project=my-project \
--region=us-central1 \
--quota=CPUS \
--limit=100
bash
`View quotas
Request quota increase
$1
` costCaps:
- name: compute-cap
amount:
specifiedAmount:
currencyCode: USD
units: 500
basis: CURRENT_SPEND
enforceSpendingLimits: true
restrictions:
- service: compute.googleapis.com
yaml
`cost-cap.yaml
$1
$1
` gcloud compute instances list \
--filter="status=RUNNING" \
--format="table(name,zone,machineType,cpuPlatform)" gcloud compute instances set-machine-type my-instance \
--machine-type=e2-standard-2 gcloud compute resource-policies create instance-schedule my-schedule \
--description="Business hours schedule" \
--vm-start-schedule="0 8 1-5" \
--vm-stop-schedule="0 18 1-5" \
--timezone="America/New_York"
bash
`List idle instances
Resize instance
Create instance schedule
$1
` gsutil du -sh gs://my-bucket/* cat > lifecycle.json << EOF
{
"rule": [
{
"action": {"type": "SetStorageClass", "storageClass": "NEARLINE"},
"condition": {"age": 30, "matchesStorageClass": ["STANDARD"]}
},
{
"action": {"type": "SetStorageClass", "storageClass": "COLDLINE"},
"condition": {"age": 90, "matchesStorageClass": ["NEARLINE"]}
},
{
"action": {"type": "Delete"},
"condition": {"age": 365}
}
]
}
EOF
gsutil lifecycle set lifecycle.json gs://my-bucket
bash
`List bucket sizes
Configure lifecycle policy
$1
$1
` gcloud beta billing accounts export billing-data \
--billing-account=0X0X0X-0X0X0X-0X0X0X \
--dataset=billing_data bq mk \
--time_partitioning_type=DAY \
--schema=schema.json \
billing_dataset.billing_export
bash
`Set up BigQuery export
Create export table
$1
` -- Cost by service
SELECT
service.description,
SUM(cost) as total_cost,
SUM(usage.amount) as usage_amount,
usage.unit as usage_unit
FROM WHERE DATE(_PARTITIONTIME) >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
GROUP BY service.description, usage.unit
ORDER BY total_cost DESC
LIMIT 10; -- Cost trends
SELECT
DATE(usage_start_time) as usage_date,
service.description,
SUM(cost) as daily_cost
FROM WHERE DATE(usage_start_time) >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
GROUP BY usage_date, service.description
ORDER BY usage_date DESC, daily_cost DESC;
sql
billing_dataset.billing_export
billing_dataset.billing_export
`
$1
$1
` gcloud compute commitments create my-commitment \
--plan=12-month \
--resources=memory=32GB,vcpu=8 \
--region=us-central1 gcloud compute commitments list \
--format="table(name,plan,endTimestamp,status)"
bash
`Create commitment
List commitments
$1
` def calculate_sustained_use_discount(usage_hours, total_hours):
"""Calculate sustained use discount."""
usage_percentage = usage_hours / total_hours
if usage_percentage >= 0.25:
tier1 = min(usage_percentage - 0.25, 0.25) * 0.2
tier2 = min(max(usage_percentage - 0.50, 0), 0.25) * 0.4
tier3 = min(max(usage_percentage - 0.75, 0), 0.25) * 0.6
return tier1 + tier2 + tier3
return 0
python
`sustained_use.py
$1
$1
` dashboards:
- name: cost-dashboard
displayName: "Cost Overview"
gridLayout:
columns: 2
widgets:
- title: "Monthly Cost Trend"
xyChart:
dataSets:
- timeSeriesQuery:
timeSeriesFilter:
filter: "metric.type=\"billing/monthly_cost\""
- title: "Cost by Service"
pieChart:
chartData:
timeSeriesQuery:
timeSeriesFilter:
filter: "metric.type=\"billing/service_cost\""
yaml
`dashboard.yaml
$1
` alertPolicies:
- displayName: "Budget Alert"
combiner: OR
conditions:
- displayName: "Monthly Budget Exceeded"
conditionThreshold:
filter: "metric.type=\"billing/monthly_cost\""
comparison: COMPARISON_GT
thresholdValue: 1000
duration: 0s
notificationChannels:
- "projects/my-project/notificationChannels/12345"
yaml
``alert-policy.yaml
$1
1. Resource Management
- Right-size resources
- Use preemptible VMs
- Implement auto-scaling
- Clean up unused resources
2. Storage Optimization
- Use appropriate storage classes
- Implement lifecycle policies
- Clean up unused storage
- Optimize object sizes
3. Network Optimization
- Use appropriate network tiers
- Optimize egress traffic
- Implement caching
- Use Cloud CDN
4. Instance Optimization
- Use committed use discounts
- Leverage sustained use discounts
- Schedule instance operations
- Use appropriate machine types
$1
1. Billing Management
- Set up billing alerts
- Configure budgets
- Regular cost reviews
- Export billing data
2. Resource Organization
- Use labels effectively
- Implement quotas
- Monitor usage patterns
- Regular cleanup
3. Cost Monitoring
- Create dashboards
- Set up alerts
- Regular reporting
- Track trends
$1
Effective cost management is crucial for cloud success. Key takeaways:
For more information, refer to the official documentation:
Why This Matters
Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.
Trade-offs to Consider
Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.
When NOT to Use This
Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.
Decision Framework
Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.