interview-prep

Crisp answer: Auto Scaling automatically adjusts the number of EC2 instances, ECS tasks, or other resources in response to demand. It uses scaling policies to define when and how to scale based on CloudWatch metrics or schedules.

Components:

Auto Scaling Group (ASG):
  - The group of EC2 instances managed together
  - Defined: min, max, desired capacity
  - Launch template: what instances to launch (AMI, instance type, SG, IAM role)
  - Health checks: EC2 (instance state) or ELB (load balancer health)
  - AZ rebalancing: distributes instances evenly across configured AZs

Scaling Policies:
  - Target Tracking:   maintain a metric at a target value (simplest, recommended)
  - Step Scaling:      add/remove specific counts at threshold steps
  - Simple Scaling:    single threshold, cooldown period (legacy)
  - Scheduled:         scale to specific size at a time (known peak/off-peak)
  - Predictive:        ML-based prediction of future load (EC2 only)

Target Tracking (recommended):

aws autoscaling put-scaling-policy \
  --auto-scaling-group-name my-asg \
  --policy-name cpu-target-tracking \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{
    "TargetValue": 50.0,
    "PredefinedMetricSpecification": {
      "PredefinedMetricType": "ASGAverageCPUUtilization"
    }
  }'
# Maintains average CPU at 50% across all instances in the group

Kubernetes equivalent — HPA and Cluster Autoscaler:

# HPA: scale pods when CPU > 50%
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api
  minReplicas: 2
  maxReplicas: 20
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
# Cluster Autoscaler: adds nodes when pods are Pending due to insufficient resources
# Installed as a Deployment in kube-system
# Reads ASG tags to know which ASGs to scale:
aws autoscaling create-or-update-tags \
  --tags ResourceId=my-asg,ResourceType=auto-scaling-group,Key=k8s.io/cluster-autoscaler/enabled,Value=true,PropagateAtLaunch=false

KEDA (Kubernetes Event-Driven Autoscaling):

Scale pods based on external event sources: SQS queue depth, Kafka lag, HTTP request rate, custom metrics.

What to say in the interview:

"For EC2 workloads I use Auto Scaling Groups with target tracking policies — they're self-tuning and handle scale-in and scale-out automatically. For EKS I use two layers: HPA scales pods based on CPU and memory metrics, and Cluster Autoscaler scales nodes based on pending pods. The two work together: HPA tries to scale pods, if there are no nodes with capacity the pods go Pending, and Cluster Autoscaler sees the pending pods and adds nodes. KEDA extends HPA with event-driven sources like SQS queue depth which is useful for batch processing workloads."


My notes