cost-optimization

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cloud Cost Optimization

云成本优化

Strategies and patterns for optimizing cloud costs across AWS, Azure, and GCP.
适用于AWS、Azure和GCP的云成本优化策略与模式。

Purpose

目标

Implement systematic cost optimization strategies to reduce cloud spending while maintaining performance and reliability.
实施系统化的成本优化策略,在保持性能和可靠性的同时降低云支出。

When to Use

适用场景

  • Reduce cloud spending
  • Right-size resources
  • Implement cost governance
  • Optimize multi-cloud costs
  • Meet budget constraints
  • 降低云支出
  • 合理调整资源规模
  • 实施成本治理
  • 优化多云成本
  • 满足预算限制

Cost Optimization Framework

成本优化框架

1. Visibility

1. 可见性

  • Implement cost allocation tags
  • Use cloud cost management tools
  • Set up budget alerts
  • Create cost dashboards
  • 实施成本分配标签
  • 使用云成本管理工具
  • 设置预算警报
  • 构建成本仪表板

2. Right-Sizing

2. 资源合理调整

  • Analyze resource utilization
  • Downsize over-provisioned resources
  • Use auto-scaling
  • Remove idle resources
  • 分析资源利用率
  • 缩减过度配置的资源
  • 使用自动扩缩容
  • 移除闲置资源

3. Pricing Models

3. 定价模型

  • Use reserved capacity
  • Leverage spot/preemptible instances
  • Implement savings plans
  • Use committed use discounts
  • 使用预留容量
  • 利用竞价/可抢占实例
  • 实施节省计划
  • 使用承诺使用折扣

4. Architecture Optimization

4. 架构优化

  • Use managed services
  • Implement caching
  • Optimize data transfer
  • Use lifecycle policies
  • 使用托管服务
  • 实施缓存机制
  • 优化数据传输
  • 使用生命周期策略

AWS Cost Optimization

AWS成本优化

Reserved Instances

预留实例

Savings: 30-72% vs On-Demand
Term: 1 or 3 years
Payment: All/Partial/No upfront
Flexibility: Standard or Convertible
Savings: 30-72% vs On-Demand
Term: 1 or 3 years
Payment: All/Partial/No upfront
Flexibility: Standard or Convertible

Savings Plans

节省计划

Compute Savings Plans: 66% savings
EC2 Instance Savings Plans: 72% savings
Applies to: EC2, Fargate, Lambda
Flexible across: Instance families, regions, OS
Compute Savings Plans: 66% savings
EC2 Instance Savings Plans: 72% savings
Applies to: EC2, Fargate, Lambda
Flexible across: Instance families, regions, OS

Spot Instances

竞价实例

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
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

S3 Cost Optimization

S3成本优化

hcl
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"
    }

    expiration {
      days = 365
    }
  }
}
hcl
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"
    }

    expiration {
      days = 365
    }
  }
}

Azure Cost Optimization

Azure成本优化

Reserved VM Instances

预留VM实例

  • 1 or 3 year terms
  • Up to 72% savings
  • Flexible sizing
  • Exchangeable
  • 1年或3年期限
  • 最高节省72%
  • 灵活的规格调整
  • 可兑换

Azure Hybrid Benefit

Azure混合权益

  • Use existing Windows Server licenses
  • Up to 80% savings with RI
  • Available for Windows and SQL Server
  • 使用现有Windows Server许可证
  • 结合预留实例最高节省80%
  • 适用于Windows和SQL Server

Azure Advisor Recommendations

Azure Advisor建议

  • Right-size VMs
  • Delete unused resources
  • Use reserved capacity
  • Optimize storage
  • 合理调整VM规模
  • 删除未使用资源
  • 使用预留容量
  • 优化存储

GCP Cost Optimization

GCP成本优化

Committed Use Discounts

承诺使用折扣

  • 1 or 3 year commitment
  • Up to 57% savings
  • Applies to vCPUs and memory
  • Resource-based or spend-based
  • 1年或3年承诺
  • 最高节省57%
  • 适用于vCPU和内存
  • 基于资源或基于支出

Sustained Use Discounts

持续使用折扣

  • Automatic discounts
  • Up to 30% for running instances
  • No commitment required
  • Applies to Compute Engine, GKE
  • 自动折扣
  • 运行实例最高节省30%
  • 无需承诺
  • 适用于Compute Engine、GKE

Preemptible VMs

可抢占VM

  • Up to 80% savings
  • 24-hour maximum runtime
  • Best for batch workloads
  • 最高节省80%
  • 最长运行24小时
  • 最适合批处理工作负载

Tagging Strategy

标签策略

AWS Tagging

AWS标签设置

hcl
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
hcl
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"
    }
  )
}
参考: 参见
references/tagging-standards.md

Cost Monitoring

成本监控

Budget Alerts

预算警报

hcl
undefined
hcl
undefined

AWS Budget

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"] } }
undefined
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"] } }
undefined

Cost Anomaly Detection

成本异常检测

  • AWS Cost Anomaly Detection
  • Azure Cost Management alerts
  • GCP Budget alerts
  • AWS Cost Anomaly Detection
  • Azure Cost Management警报
  • GCP Budget警报

Architecture Patterns

架构模式

Pattern 1: Serverless First

模式1:优先使用无服务器

  • Use Lambda/Functions for event-driven
  • Pay only for execution time
  • Auto-scaling included
  • No idle costs
  • 使用Lambda/Functions处理事件驱动型工作负载
  • 仅按执行时间付费
  • 包含自动扩缩容
  • 无闲置成本

Pattern 2: Right-Sized Databases

模式2:合理调整数据库规模

Development: t3.small RDS
Staging: t3.large RDS
Production: r6g.2xlarge RDS with read replicas
Development: t3.small RDS
Staging: t3.large RDS
Production: r6g.2xlarge RDS with read replicas

Pattern 3: Multi-Tier Storage

模式3:多层存储

Hot data: S3 Standard
Warm data: S3 Standard-IA (30 days)
Cold data: S3 Glacier (90 days)
Archive: S3 Deep Archive (365 days)
热数据: S3 Standard
温数据: S3 Standard-IA (30天)
冷数据: S3 Glacier (90天)
归档: S3 Deep Archive (365天)

Pattern 4: Auto-Scaling

模式4:自动扩缩容

hcl
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]
}
hcl
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]
}

Cost Optimization Checklist

成本优化检查清单

  • Implement cost allocation tags
  • Delete unused resources (EBS, EIPs, snapshots)
  • Right-size instances based on utilization
  • Use reserved capacity for steady workloads
  • Implement auto-scaling
  • Optimize storage classes
  • Use lifecycle policies
  • Enable cost anomaly detection
  • Set budget alerts
  • Review costs weekly
  • Use spot/preemptible instances
  • Optimize data transfer costs
  • Implement caching layers
  • Use managed services
  • Monitor and optimize continuously
  • 实施成本分配标签
  • 删除未使用的资源(EBS、EIP、快照)
  • 根据利用率合理调整实例规模
  • 为稳定工作负载使用预留容量
  • 实施自动扩缩容
  • 优化存储类别
  • 使用生命周期策略
  • 启用成本异常检测
  • 设置预算警报
  • 每周审查成本
  • 使用竞价/可抢占实例
  • 优化数据传输成本
  • 实施缓存层
  • 使用托管服务
  • 持续监控和优化

Tools

工具

  • AWS: Cost Explorer, Cost Anomaly Detection, Compute Optimizer
  • Azure: Cost Management, Advisor
  • GCP: Cost Management, Recommender
  • Multi-cloud: CloudHealth, Cloudability, Kubecost
  • AWS: Cost Explorer、Cost Anomaly Detection、Compute Optimizer
  • Azure: Cost Management、Advisor
  • GCP: Cost Management、Recommender
  • 多云: CloudHealth、Cloudability、Kubecost

Reference Files

参考文件

  • references/tagging-standards.md
    - Tagging conventions
  • assets/cost-analysis-template.xlsx
    - Cost analysis spreadsheet
  • references/tagging-standards.md
    - 标签约定
  • assets/cost-analysis-template.xlsx
    - 成本分析电子表格

Related Skills

相关技能

  • terraform-module-library
    - For resource provisioning
  • multi-cloud-architecture
    - For cloud selection
  • terraform-module-library
    - 用于资源部署
  • multi-cloud-architecture
    - 用于云服务商选择