LESSON 30分

「過剰なスペックはコストの無駄、不足はパフォーマンスの問題。Right-sizingは両方のバランスを取る技術だ」と佐藤CTOは言った。

1. Right-sizing の基本アプローチ

// メトリクスベースの分析
interface ResourceUtilization {
  instanceId: string;
  instanceType: string;
  avgCpuPercent: number;
  maxCpuPercent: number;
  avgMemoryPercent: number;
  maxMemoryPercent: number;
  networkInGbps: number;
  period: string;  // 分析期間
}

function recommendRightSizing(util: ResourceUtilization): {
  recommendation: 'downsize' | 'upsize' | 'optimal' | 'terminate';
  suggestedType?: string;
  estimatedSavings?: number;
} {
  // 常時低使用率 → ダウンサイズまたは停止
  if (util.maxCpuPercent < 10 && util.maxMemoryPercent < 10) {
    return { recommendation: 'terminate', estimatedSavings: 100 };
  }
  if (util.avgCpuPercent < 20 && util.avgMemoryPercent < 30) {
    return { recommendation: 'downsize', estimatedSavings: 40 };
  }
  // ピーク時に高使用率 → アップサイズ
  if (util.maxCpuPercent > 90 || util.maxMemoryPercent > 90) {
    return { recommendation: 'upsize' };
  }
  return { recommendation: 'optimal' };
}

2. AWS Compute Optimizer

推奨事項条件期待削減
Over-provisionedCPU/Memory平均 < 40%20-50%
Under-provisionedCPU/Memoryピーク > 90%- (リスク回避)
Graviton移行x86 → ARM互換ワークロード20%
世代更新旧世代 → 最新世代10-30%

3. コンテナリソース最適化

# Kubernetes: VPA (Vertical Pod Autoscaler) で自動最適化
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: api-server-vpa
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  updatePolicy:
    updateMode: "Auto"  # 自動でリソースを調整
  resourcePolicy:
    containerPolicies:
      - containerName: api
        minAllowed:
          cpu: 100m
          memory: 128Mi
        maxAllowed:
          cpu: 2000m
          memory: 2Gi
// ECS Fargate のタスクサイズ最適化
const fargateOptions = [
  { cpu: 256,  memory: 512,  costPerHour: 0.01234 },
  { cpu: 512,  memory: 1024, costPerHour: 0.02469 },
  { cpu: 1024, memory: 2048, costPerHour: 0.04938 },
  { cpu: 2048, memory: 4096, costPerHour: 0.09875 },
  { cpu: 4096, memory: 8192, costPerHour: 0.19750 },
];
// 実測のCPU/メモリ使用率から最小限のサイズを選択

4. Graviton/ARM 移行

サービスGraviton対応期待効果
EC2c7g, m7g, r7g同等性能で20%安価
RDSdb.r7g同等性能で20%安価
ElastiCachecache.r7g同等性能で20%安価
Lambdaarm64同等性能で20%安価
ECS/FargateARM64対応同等性能で20%安価

まとめ

トピック要点
Right-sizingメトリクスベースで過剰/不足を判定
Compute OptimizerAWSの推奨を活用し自動分析
コンテナ最適化VPA/適切なFargateサイズ選択
GravitonARM移行で20%コスト削減

チェックリスト

  • Right-sizingの判定基準を説明できる
  • Compute Optimizerの活用方法を理解した
  • コンテナリソースの最適化手法を知っている
  • Graviton移行のメリットと対象サービスを把握した

次のステップへ

リソース最適化を学んだ。次は 予約・Spot戦略 で、料金プランを活用したコスト削減を学ぼう。

推定読了時間: 30分