aws-cost-optimizer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

AWS Cost Optimizer

AWS成本优化器

Analyze and optimize AWS costs with actionable recommendations.
通过可执行的建议来分析并优化AWS成本。

Quick Start

快速入门

Review Cost Explorer, identify top costs, right-size instances, use Reserved Instances, delete unused resources.
查看Cost Explorer,识别主要成本项,调整实例规格,使用预留实例,删除未使用的资源。

Instructions

操作指南

Cost Analysis Process

成本分析流程

  1. Identify cost drivers
  2. Analyze usage patterns
  3. Recommend optimizations
  4. Estimate savings
  5. Implement changes
  1. 识别成本动因
  2. 分析使用模式
  3. 提出优化建议
  4. 估算节省金额
  5. 实施变更

Using AWS Cost Explorer

使用AWS Cost Explorer

Access Cost Explorer:
  • AWS Console → Billing → Cost Explorer
  • View costs by service, region, tag
Key metrics to check:
  • Monthly costs by service
  • Cost trends over time
  • Unused or underutilized resources
  • Data transfer costs
访问Cost Explorer:
  • AWS控制台 → 账单 → Cost Explorer
  • 按服务、区域、标签查看成本
需检查的关键指标:
  • 各服务月度成本
  • 成本随时间变化的趋势
  • 未使用或利用率不足的资源
  • 数据传输成本

EC2 Cost Optimization

EC2成本优化

Right-sizing instances:
Check utilization:
bash
undefined
调整实例规格:
查看利用率:
bash
undefined

Get CloudWatch metrics

Get CloudWatch metrics

aws cloudwatch get-metric-statistics
--namespace AWS/EC2
--metric-name CPUUtilization
--dimensions Name=InstanceId,Value=i-1234567890abcdef0
--start-time 2024-01-01T00:00:00Z
--end-time 2024-01-31T23:59:59Z
--period 3600
--statistics Average

**Recommendations:**
- CPU < 20%: Downsize instance type
- CPU > 80%: Upsize or add instances
- Memory < 50%: Consider smaller instance

**Reserved Instances:**
- 1-year: ~30-40% savings
- 3-year: ~50-70% savings
- Best for predictable workloads

**Savings Plans:**
- More flexible than Reserved Instances
- Commit to $/hour usage
- Apply across instance families

**Spot Instances:**
- Up to 90% savings
- For fault-tolerant workloads
- Batch processing, CI/CD, testing

**Stop unused instances:**
```bash
aws cloudwatch get-metric-statistics
--namespace AWS/EC2
--metric-name CPUUtilization
--dimensions Name=InstanceId,Value=i-1234567890abcdef0
--start-time 2024-01-01T00:00:00Z
--end-time 2024-01-31T23:59:59Z
--period 3600
--statistics Average

**建议:**
- CPU利用率<20%:降低实例规格
- CPU利用率>80%:升级实例规格或增加实例数量
- 内存利用率<50%:考虑使用更小规格的实例

**预留实例:**
- 1年期:约节省30-40%
- 3年期:约节省50-70%
- 最适合可预测的工作负载

**Savings Plans:**
- 比预留实例更灵活
- 按每小时使用量承诺消费
- 适用于跨实例系列的场景

**竞价实例:**
- 最高可节省90%
- 适用于容错型工作负载
- 如批处理、CI/CD、测试场景

**停止未使用的实例:**
```bash

Find stopped instances

Find stopped instances

aws ec2 describe-instances
--filters "Name=instance-state-name,Values=stopped"
--query 'Reservations[].Instances[].[InstanceId,Tags[?Key==
Name
].Value|[0]]'
aws ec2 describe-instances
--filters "Name=instance-state-name,Values=stopped"
--query 'Reservations[].Instances[].[InstanceId,Tags[?Key==
Name
].Value|[0]]'

Terminate if not needed

Terminate if not needed

aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
undefined
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
undefined

S3 Cost Optimization

S3成本优化

Lifecycle policies:
json
{
  "Rules": [{
    "Id": "Archive old data",
    "Status": "Enabled",
    "Transitions": [
      {
        "Days": 30,
        "StorageClass": "STANDARD_IA"
      },
      {
        "Days": 90,
        "StorageClass": "GLACIER"
      }
    ],
    "Expiration": {
      "Days": 365
    }
  }]
}
Storage classes:
  • Standard: Frequent access
  • Standard-IA: Infrequent access (30+ days)
  • Glacier: Archive (90+ days)
  • Glacier Deep Archive: Long-term archive
Delete incomplete multipart uploads:
bash
aws s3api list-multipart-uploads --bucket my-bucket
生命周期策略:
json
{
  "Rules": [{
    "Id": "Archive old data",
    "Status": "Enabled",
    "Transitions": [
      {
        "Days": 30,
        "StorageClass": "STANDARD_IA"
      },
      {
        "Days": 90,
        "StorageClass": "GLACIER"
      }
    ],
    "Expiration": {
      "Days": 365
    }
  }]
}
存储类别:
  • Standard:频繁访问
  • Standard-IA:不频繁访问(30天以上)
  • Glacier:归档(90天以上)
  • Glacier Deep Archive:长期归档
删除未完成的分段上传:
bash
aws s3api list-multipart-uploads --bucket my-bucket

Set lifecycle rule to abort after 7 days

Set lifecycle rule to abort after 7 days


**Analyze storage:**
```bash

**分析存储情况:**
```bash

Get bucket size

Get bucket size

aws s3 ls s3://my-bucket --recursive --summarize
undefined
aws s3 ls s3://my-bucket --recursive --summarize
undefined

RDS Cost Optimization

RDS成本优化

Right-size databases:
  • Check CPU, memory, IOPS utilization
  • Downsize if consistently low
  • Use Aurora Serverless for variable workloads
Reserved Instances:
  • 1-year: ~30-40% savings
  • 3-year: ~50-60% savings
Stop dev/test databases:
bash
undefined
调整数据库规格:
  • 检查CPU、内存、IOPS利用率
  • 如果持续处于低负载则降低规格
  • 对于可变工作负载使用Aurora Serverless
预留实例:
  • 1年期:约节省30-40%
  • 3年期:约节省50-60%
停止开发/测试数据库:
bash
undefined

Stop RDS instance

Stop RDS instance

aws rds stop-db-instance --db-instance-identifier mydb
aws rds stop-db-instance --db-instance-identifier mydb

Start when needed

Start when needed

aws rds start-db-instance --db-instance-identifier mydb

**Delete old snapshots:**
```bash
aws rds start-db-instance --db-instance-identifier mydb

**删除旧快照:**
```bash

List snapshots

List snapshots

aws rds describe-db-snapshots --query 'DBSnapshots[?SnapshotCreateTime<
2023-01-01
]'
aws rds describe-db-snapshots --query 'DBSnapshots[?SnapshotCreateTime<
2023-01-01
]'

Delete old snapshots

Delete old snapshots

aws rds delete-db-snapshot --db-snapshot-identifier snapshot-id
undefined
aws rds delete-db-snapshot --db-snapshot-identifier snapshot-id
undefined

Data Transfer Costs

数据传输成本

Reduce data transfer:
  • Use CloudFront for content delivery
  • Keep data in same region
  • Use VPC endpoints for AWS services
  • Compress data before transfer
VPC endpoints:
bash
undefined
减少数据传输:
  • 使用CloudFront进行内容分发
  • 将数据保留在同一区域
  • 为AWS服务使用VPC终端节点
  • 传输前压缩数据
VPC终端节点:
bash
undefined

Create S3 VPC endpoint (no data transfer charges)

Create S3 VPC endpoint (no data transfer charges)

aws ec2 create-vpc-endpoint
--vpc-id vpc-12345678
--service-name com.amazonaws.us-east-1.s3
--route-table-ids rtb-12345678
undefined
aws ec2 create-vpc-endpoint
--vpc-id vpc-12345678
--service-name com.amazonaws.us-east-1.s3
--route-table-ids rtb-12345678
undefined

EBS Cost Optimization

EBS成本优化

Delete unattached volumes:
bash
undefined
删除未挂载的卷:
bash
undefined

Find unattached volumes

Find unattached volumes

aws ec2 describe-volumes
--filters "Name=status,Values=available"
--query 'Volumes[].[VolumeId,Size,VolumeType]'
aws ec2 describe-volumes
--filters "Name=status,Values=available"
--query 'Volumes[].[VolumeId,Size,VolumeType]'

Delete if not needed

Delete if not needed

aws ec2 delete-volume --volume-id vol-1234567890abcdef0

**Delete old snapshots:**
```bash
aws ec2 delete-volume --volume-id vol-1234567890abcdef0

**删除旧快照:**
```bash

List old snapshots

List old snapshots

aws ec2 describe-snapshots --owner-ids self
--query 'Snapshots[?StartTime<
2023-01-01
]'
aws ec2 describe-snapshots --owner-ids self
--query 'Snapshots[?StartTime<
2023-01-01
]'

Delete

Delete

aws ec2 delete-snapshot --snapshot-id snap-1234567890abcdef0

**Use gp3 instead of gp2:**
- gp3 is 20% cheaper
- Better performance
- Migrate existing volumes
aws ec2 delete-snapshot --snapshot-id snap-1234567890abcdef0

**使用gp3替代gp2:**
- gp3便宜20%
- 性能更优
- 迁移现有卷

Lambda Cost Optimization

Lambda成本优化

Optimize memory allocation:
  • More memory = faster execution = lower cost
  • Test different memory settings
  • Use AWS Lambda Power Tuning tool
Reduce cold starts:
  • Use provisioned concurrency (if needed)
  • Keep functions warm with scheduled events
  • Minimize dependencies
Monitor invocations:
bash
undefined
优化内存分配:
  • 内存越多=执行速度越快=成本越低
  • 测试不同的内存设置
  • 使用AWS Lambda Power Tuning工具
减少冷启动:
  • (如有需要)使用预置并发
  • 通过定时事件保持函数温暖
  • 最小化依赖项
监控调用次数:
bash
undefined

Get Lambda metrics

Get Lambda metrics

aws cloudwatch get-metric-statistics
--namespace AWS/Lambda
--metric-name Invocations
--dimensions Name=FunctionName,Value=my-function
--start-time 2024-01-01T00:00:00Z
--end-time 2024-01-31T23:59:59Z
--period 86400
--statistics Sum
undefined
aws cloudwatch get-metric-statistics
--namespace AWS/Lambda
--metric-name Invocations
--dimensions Name=FunctionName,Value=my-function
--start-time 2024-01-01T00:00:00Z
--end-time 2024-01-31T23:59:59Z
--period 86400
--statistics Sum
undefined

CloudWatch Costs

CloudWatch成本

Reduce log retention:
bash
undefined
缩短日志保留时间:
bash
undefined

Set log retention to 7 days

Set log retention to 7 days

aws logs put-retention-policy
--log-group-name /aws/lambda/my-function
--retention-in-days 7

**Delete unused log groups:**
```bash
aws logs put-retention-policy
--log-group-name /aws/lambda/my-function
--retention-in-days 7

**删除未使用的日志组:**
```bash

List log groups

List log groups

aws logs describe-log-groups
aws logs describe-log-groups

Delete

Delete

aws logs delete-log-group --log-group-name /aws/lambda/old-function
undefined
aws logs delete-log-group --log-group-name /aws/lambda/old-function
undefined

Unused Resources

未使用的资源

Find unused resources:
Elastic IPs not attached:
bash
aws ec2 describe-addresses \
  --query 'Addresses[?AssociationId==null]'
Load balancers with no targets:
bash
aws elbv2 describe-load-balancers
aws elbv2 describe-target-health --target-group-arn arn
NAT Gateways with low traffic:
bash
undefined
查找未使用的资源:
未挂载的弹性IP:
bash
aws ec2 describe-addresses \
  --query 'Addresses[?AssociationId==null]'
无目标的负载均衡器:
bash
aws elbv2 describe-load-balancers
aws elbv2 describe-target-health --target-group-arn arn
低流量的NAT网关:
bash
undefined

Check CloudWatch metrics for BytesOutToDestination

Check CloudWatch metrics for BytesOutToDestination

undefined
undefined

Cost Optimization Checklist

成本优化检查清单

Compute:
  • Right-size EC2 instances
  • Use Reserved Instances for predictable workloads
  • Use Spot Instances for fault-tolerant workloads
  • Stop/terminate unused instances
  • Implement auto-scaling
Storage:
  • Implement S3 lifecycle policies
  • Delete old EBS snapshots
  • Delete unattached EBS volumes
  • Use appropriate S3 storage classes
  • Delete incomplete multipart uploads
Database:
  • Right-size RDS instances
  • Use Reserved Instances
  • Stop dev/test databases when not in use
  • Delete old RDS snapshots
  • Consider Aurora Serverless
Networking:
  • Use CloudFront to reduce data transfer
  • Delete unused Elastic IPs
  • Use VPC endpoints
  • Review NAT Gateway usage
Monitoring:
  • Reduce CloudWatch log retention
  • Delete unused log groups
  • Review custom metrics
General:
  • Enable AWS Cost Anomaly Detection
  • Set up billing alerts
  • Use AWS Budgets
  • Tag resources for cost allocation
  • Review Cost Explorer regularly
计算资源:
  • 调整EC2实例规格
  • 为可预测工作负载使用预留实例
  • 为容错型工作负载使用竞价实例
  • 停止/终止未使用的实例
  • 实现自动扩缩容
存储资源:
  • 配置S3生命周期策略
  • 删除旧的EBS快照
  • 删除未挂载的EBS卷
  • 使用合适的S3存储类别
  • 删除未完成的分段上传
数据库:
  • 调整RDS实例规格
  • 使用预留实例
  • 非使用时段停止开发/测试数据库
  • 删除旧的RDS快照
  • 考虑使用Aurora Serverless
网络:
  • 使用CloudFront减少数据传输
  • 删除未使用的弹性IP
  • 使用VPC终端节点
  • 审核NAT网关使用情况
监控:
  • 缩短CloudWatch日志保留时间
  • 删除未使用的日志组
  • 审核自定义指标
通用项:
  • 启用AWS成本异常检测
  • 设置账单告警
  • 使用AWS Budgets
  • 为资源添加标签以进行成本分配
  • 定期查看Cost Explorer

Savings Estimation

节省金额估算

Reserved Instances:
  • 1-year, no upfront: ~30% savings
  • 1-year, all upfront: ~40% savings
  • 3-year, all upfront: ~60% savings
Spot Instances:
  • 70-90% savings vs On-Demand
S3 Lifecycle:
  • Standard-IA: ~50% cheaper than Standard
  • Glacier: ~80% cheaper than Standard
Right-sizing:
  • Typical savings: 20-40% on oversized instances
预留实例:
  • 1年期,无预付:约节省30%
  • 1年期,全预付:约节省40%
  • 3年期,全预付:约节省60%
竞价实例:
  • 比按需实例节省70-90%
S3生命周期策略:
  • Standard-IA:比Standard便宜约50%
  • Glacier:比Standard便宜约80%
实例规格调整:
  • 典型节省:对过大的实例可节省20-40%

Tools and Commands

工具与命令

AWS Cost Explorer:
  • View costs by service, region, tag
  • Forecast future costs
  • Identify cost anomalies
AWS Budgets:
bash
undefined
AWS Cost Explorer:
  • 按服务、区域、标签查看成本
  • 预测未来成本
  • 识别成本异常
AWS Budgets:
bash
undefined

Create budget

Create budget

aws budgets create-budget
--account-id 123456789012
--budget file://budget.json

**AWS Trusted Advisor:**
- Cost optimization recommendations
- Underutilized resources
- Idle resources

**Third-party tools:**
- CloudHealth
- CloudCheckr
- Spot.io
aws budgets create-budget
--account-id 123456789012
--budget file://budget.json

**AWS Trusted Advisor:**
- 成本优化建议
- 利用率不足的资源
- 闲置资源

**第三方工具:**
- CloudHealth
- CloudCheckr
- Spot.io

Best Practices

最佳实践

Tagging strategy:
  • Tag all resources
  • Use tags for cost allocation
  • Common tags: Environment, Project, Owner, CostCenter
Regular reviews:
  • Weekly: Check for anomalies
  • Monthly: Review Cost Explorer
  • Quarterly: Optimize Reserved Instances
Automation:
  • Auto-stop dev instances at night
  • Auto-delete old snapshots
  • Auto-scale based on demand
Monitoring:
  • Set up billing alerts
  • Use AWS Budgets
  • Enable Cost Anomaly Detection
标签策略:
  • 为所有资源添加标签
  • 使用标签进行成本分配
  • 常见标签:Environment、Project、Owner、CostCenter
定期审核:
  • 每周:检查异常情况
  • 每月:查看Cost Explorer
  • 每季度:优化预留实例
自动化:
  • 夜间自动停止开发实例
  • 自动删除旧快照
  • 根据需求自动扩缩容
监控:
  • 设置账单告警
  • 使用AWS Budgets
  • 启用成本异常检测