Strategies and patterns for optimizing cloud costs across AWS, Azure, GCP, and OCI.
Implement systematic cost optimization strategies to reduce cloud spending while maintaining performance and reliability.
- Reduce cloud spending
- Right-size resources
- Implement cost governance
- Optimize multi-cloud costs
- Meet budget constraints
- Implement cost allocation tags
- Use cloud cost management tools
- Set up budget alerts
- Create cost dashboards
- Analyze resource utilization
- Downsize over-provisioned resources
- Use auto-scaling
- Remove idle resources
- Use reserved capacity
- Leverage spot/preemptible instances
- Implement savings plans
- Use committed use discounts
- Use managed services
- Implement caching
- Optimize data transfer
- Use lifecycle policies
Savings: 30-72% vs On-Demand
Term: 1 or 3 years
Payment: All/Partial/No upfront
Flexibility: Standard or Convertible
Compute Savings Plans: 66% savings
EC2 Instance Savings Plans: 72% savings
Compute Savings Plans apply to: EC2, Fargate, Lambda
Compute Savings Plans are flexible across: Instance families, regions, OS
EC2 Instance Savings Plans: locked to one instance family in one region
Note: these savings percentages are approximate maximums that may change. Before recommending any reserved capacity, savings plan, or committed use purchase, check current provider pricing and usage history, for example Cost Explorer recommendations, and get explicit human approval. The same applies to the Azure and GCP commitment options below.
Savings: Up to 90% vs On-Demand
Best for: Batch jobs, CI/CD, stateless workloads
Risk: 2-minute interruption notice
Strategy: Mix with On-Demand for resilience
resource "aws_s3_bucket_lifecycle_configuration" "example" {
bucket = aws_s3_bucket.example.id
rule {
id = "transition-to-ia"
status = "Enabled"
transition {
days = 30
storage_class = "STANDARD_IA"
}
transition {
days = 90
storage_class = "GLACIER"
}
}
}
Note: an expiration rule permanently deletes objects, and it applies to the whole bucket unless scoped. Before adding one, confirm retention and compliance requirements, versioning, and Object Lock status with the bucket owner.
- 1 or 3 year terms
- Up to 72% savings
- Flexible sizing
- Exchangeable
- Use existing Windows Server licenses
- Up to 80% savings with RI
- Available for Windows and SQL Server
- Right-size VMs
- Delete unused resources only after owner verification, a backup, and explicit human approval
- Use reserved capacity
- Optimize storage
- 1 or 3 year commitment
- Up to 57% savings
- Applies to vCPUs and memory
- Resource-based or spend-based
- Automatic discounts
- Up to 30% for running instances
- No commitment required
- Applies to Compute Engine, GKE
- Up to 80% savings
- 24-hour maximum runtime
- Best for batch workloads
- Spot VMs are the recommended successor and have no 24-hour maximum runtime
- Scale OCPUs and memory independently
- Match instance sizing to workload demand
- Reduce wasted capacity from fixed VM shapes
- Use annual commitments for predictable spend
- Set compartment-level budgets with alerts
- Track monthly forecasts with OCI Cost Analysis
- Use preemptible instances for batch and ephemeral workloads
- Keep interruption-tolerant autoscaling groups
- Mix with standard capacity for critical services
locals {
common_tags = {
Environment = "production"
Project = "my-project"
CostCenter = "engineering"
Owner = "team@example.com"
ManagedBy = "terraform"
}
}
resource "aws_instance" "example" {
ami = "ami-12345678"
instance_type = "t3.medium"
tags = merge(
local.common_tags,
{
Name = "web-server"
}
)
}
Reference: See references/tagging-standards.md
# AWS Budget
resource "aws_budgets_budget" "monthly" {
name = "monthly-budget"
budget_type = "COST"
limit_amount = "1000"
limit_unit = "USD"
time_period_start = "2024-01-01_00:00"
time_unit = "MONTHLY"
notification {
comparison_operator = "GREATER_THAN"
threshold = 80
threshold_type = "PERCENTAGE"
notification_type = "ACTUAL"
subscriber_email_addresses = ["team@example.com"]
}
}
- AWS Cost Anomaly Detection
- Azure Cost Management alerts
- GCP Budget alerts
- OCI Budgets and Cost Analysis
- Use Lambda/Functions for event-driven
- Pay only for execution time
- Auto-scaling included
- No idle costs
Development: t3.small RDS
Staging: t3.large RDS
Production: r6g.2xlarge RDS with read replicas
Hot data: S3 Standard
Warm data: S3 Standard-IA (30 days)
Cold data: S3 Glacier (90 days)
Archive: S3 Deep Archive (365 days)
resource "aws_autoscaling_policy" "scale_up" {
name = "scale-up"
scaling_adjustment = 2
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.main.name
}
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
alarm_name = "cpu-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "60"
statistic = "Average"
threshold = "80"
alarm_actions = [aws_autoscaling_policy.scale_up.arn]
}
Note: this example only scales up. Pair it with a matching scale down policy and a low CPU alarm, or use a target tracking policy instead. Scope the alarm to the instances of the target group, and set explicit minimum and maximum sizes on the parent autoscaling group, because the maximum size caps cost exposure.
- AWS: Cost Explorer, Cost Anomaly Detection, Compute Optimizer
- Azure: Cost Management, Advisor
- GCP: Cost Management, Recommender
- OCI: Cost Analysis, Budgets, Cloud Advisor
- Multi-cloud: CloudHealth, Cloudability, Kubecost
terraform-module-library - For resource provisioning
multi-cloud-architecture - For cloud selection