DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Design Cloud Cost Optimization with Claude Code: Spot Instances, S3 Lifecycle, Dev Auto-Stop

Introduction

AWS bills hitting $5,000/month? Spot instances + S3 lifecycle + dev environment auto-stop can cut costs 50%. Let Claude Code design the optimization.

CLAUDE.md Rules

## Cost Optimization Rules
- Production: On-Demand 30% + Spot 70% mix
- Dev environments: auto-stop nights/weekends
- S3: 30d→IA, 90d→Glacier, 365d→delete
- Incomplete multipart: delete after 7 days
Enter fullscreen mode Exit fullscreen mode

Generated Terraform

# ECS Spot mix (70% cost reduction)
resource "aws_ecs_service" "app" {
  capacity_provider_strategy {
    capacity_provider = "FARGATE_SPOT"
    weight            = 70
    base              = 0
  }
  capacity_provider_strategy {
    capacity_provider = "FARGATE"
    weight            = 30
    base              = 1  # Always 1 on-demand task
  }
}

# S3 Lifecycle
resource "aws_s3_bucket_lifecycle_configuration" "uploads" {
  rule {
    id = "uploads-lifecycle"
    transition { days = 30; storage_class = "STANDARD_IA" }
    transition { days = 90; storage_class = "GLACIER_IR" }
    expiration { days = 365 }
  }
  rule {
    id = "abort-multipart"
    abort_incomplete_multipart_upload { days_after_initiation = 7 }
  }
}
Enter fullscreen mode Exit fullscreen mode
// Auto-stop dev environment (save ~$50/day)
export async function stopDevEnvironment(): Promise<void> {
  await Promise.all(services.map(s =>
    ecs.send(new UpdateServiceCommand({ cluster, service: s, desiredCount: 0 }))
  ));
  await rds.send(new StopDBInstanceCommand({ DBInstanceIdentifier: DEV_RDS }));
  logger.info('Dev environment stopped. Saving ~$50/day.');
}

export async function checkDailyCostAnomaly(): Promise<void> {
  const changePercent = ((todayCost - yesterdayCost) / yesterdayCost) * 100;
  if (changePercent > 50) {
    await sendSlackAlert({ text: `⚠️ Cost anomaly: +${changePercent.toFixed(0)}%` });
  }
}
Enter fullscreen mode Exit fullscreen mode

Summary

  1. FARGATE_SPOT weight 70: ~70% compute cost reduction
  2. S3 lifecycle: auto-tier 30d→IA→Glacier→expire
  3. Dev auto-stop: saves ~$1,500/month
  4. Cost anomaly detection: alert on >50% day-over-day increase

Review with **Code Review Pack (¥980)* at prompt-works.jp*

myouga (@myougatheaxo) — Axolotl VTuber.

Top comments (0)