Skip to content

fix: switch ECS services to Fargate Spot to reduce cost - #153

Open
Akash29g wants to merge 2 commits into
mainfrom
cost/fargate-spot
Open

fix: switch ECS services to Fargate Spot to reduce cost#153
Akash29g wants to merge 2 commits into
mainfrom
cost/fargate-spot

Conversation

@Akash29g

Copy link
Copy Markdown
Owner

What

Switch both ECS services (docanalytics-api-svc, docanalytics-web-svc) from on-demand Fargate to Fargate Spot capacity.

Why

Cost Explorer (Charge type = Usage, gross pre-credit) shows Elastic Container Service as the single largest daily line item on the account, running ~$2/day combined with ALB and RDS. With AWS credits nearly exhausted, Fargate Spot gives up to ~70% off the same vCPU/memory pricing for both services, with no code or architecture change required.

Change

  • infra/ecs.tfaws_ecs_service.api and aws_ecs_service.web: replaced launch_type = "FARGATE" with a capacity_provider_strategy block (FARGATE_SPOT, weight 1, base 1).
  • Added aws_ecs_cluster_capacity_providers.main so the cluster allows Spot capacity, with a matching default strategy.

Scope / Safety

  • Infra-only change — no application code, Dockerfiles, or task definitions affected.
  • desired_count = 1 and the lifecycle { ignore_changes = [task_definition, desired_count] } blocks are unchanged, so the existing CI/CD deploy flow (deploy.yml) is unaffected.
  • Fargate Spot tasks can be reclaimed with a 2-minute warning; ECS reschedules automatically. At desired_count = 1 this may cause a brief (seconds-level) blip, not sustained downtime.
  • deploy.yml does not run Terraform, so this change requires a manual terraform apply from infra/ after merge — it will not take effect on push alone.

How to verify

  1. terraform plan locally shows changes limited to the 2 services + 1 new capacity-provider resource — no destroys.
  2. After terraform apply: ECS console → Services → both services show CAPACITY PROVIDER: FARGATE_SPOT and RUNNING count stays at 1 throughout the rollout.
  3. docanalytics.dev stays reachable during and after the rollout.
  4. Next day: Cost Explorer (Charge type = Usage, grouped by Service, Daily) shows a drop in the Elastic Container Service line vs. the prior day's baseline.

@Akash29g
Akash29g requested a review from g9shubh as a code owner August 23, 2026 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant