Docs/Tools/Cost Optimize
Cost

cost_optimize.py

Applies the car-painter KEDA scale-to-zero pattern to your Kubernetes service. Generates keda.tf, http-scaler.yaml, and patches the deployment to set minReplicas: 0. Typical saving: 60–90% compute cost for bursty or low-traffic services.

What it does

Generates three artefacts that wire KEDA's HTTP add-on into your service. Pods scale to zero on 5 minutes of idle traffic and spin back up within 60 seconds on the first incoming request.

CLI Usage
bash
python3 tools/cost_optimize.py \
  --terraform-dir ./my-app/terraform \
  --platform eks \
  --service payment-api
Platform Options
--platformMechanismNotes
eksKEDA + HTTP Add-onInstall KEDA via Helm chart
aksBuilt-in KEDA add-onEnable in AKS portal / Bicep
gkeKEDA or Cloud RunCloud Run is natively serverless — prefer it
Key KEDA Settings
minReplicaCount0Scale to zero when idle
scaledownPeriod3005 minutes idle before scaling to zero
targetPendingRequests100Scale up trigger threshold
maxReplicaCount10Override for prod (default: 10)
When NOT to Use
  • Stateful services (databases, message stores)
  • Message-queue consumers — use KEDA queue scalers instead
  • Services with less than 60-second cold-start tolerance
  • Prod services where latency SLOs require pre-warmed replicas
Node-Level Savings — Karpenter

KEDA removes idle pods. Karpenter removes idle nodes. Use both together for maximum savings: KEDA scales your pods to zero, then Karpenter consolidates and terminates the now-empty nodes automatically.

What it doesRight-sizes and consolidates EC2 nodes in real time. Replaces Cluster Autoscaler on EKS.
Spot supportAutomatically provisions Spot instances and handles interruptions — 60–90% node cost reduction.
ConsolidationNodePool consolidateAfter: 30s — evicts pods and terminates underutilised nodes within 30 seconds.
Works with KEDAWhen KEDA scales pods to 0, Karpenter sees empty nodes and terminates them. Full cost elimination when idle.

Minimal NodePool that enables Spot + consolidation:

yaml — karpenter NodePool
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
  name: default
spec:
  template:
    spec:
      requirements:
        - key: karpenter.sh/capacity-type
          operator: In
          values: ["spot", "on-demand"]
        - key: kubernetes.io/arch
          operator: In
          values: ["amd64"]
      nodeClassRef:
        group: karpenter.k8s.aws
        kind: EC2NodeClass
        name: default
  disruption:
    consolidationPolicy: WhenEmptyOrUnderutilized
    consolidateAfter: 30s
  limits:
    cpu: "100"
    memory: 400Gi

Install: helm install karpenter oci://public.ecr.aws/karpenter/karpenter --version 1.0.0 -n karpenter

Native Serverless Alternatives

Always prefer managed serverless over K8s + KEDA when the workload is stateless HTTP — lower operational overhead and lower cost.

Cloud Run (GCP)Native scale-to-zero, no KEDA needed
Azure Container AppsNative scale-to-zero, no KEDA needed
AWS Fargate (ECS)Scale to 0 via ECS Service with min=0
Next step: Test Runner →