Opscraft
AWS core services · Lesson 2 of 8 · 5 min read

Compute: EC2, Auto Scaling and load balancing

EC2 essentials

  • Instance families: general (m, t burstable), compute (c), memory (r, x), storage (i, d), accelerated (g, p, inf). Graviton (*g, ARM) gives better price/performance if your images support arm64.
  • Purchasing: On-Demand; Savings Plans (Compute SP flexible across EC2/Fargate/Lambda) and Reserved Instances for steady load; Spot (up to ~90% off, 2-minute interruption notice) for stateless/batch/CI and Karpenter node pools with diversification.
  • AMIs — bake golden images (EC2 Image Builder/Packer); user data for bootstrap; IMDSv2 (session tokens) required to mitigate SSRF credential theft; hop limit 1 blocks containers from the node's credentials unless needed.
  • Storage: EBS (network block, AZ-scoped) vs instance store (ephemeral, lost on stop).
  • Placement groups: cluster (low latency), spread (separate hardware), partition (large distributed systems).
  • Access: SSM Session Manager instead of SSH/bastions — no open port 22, IAM-controlled, logged.
Enlarge diagramSavings Plans / RIs · steady baseline · up to ~70% offOn-Demand · spikesSpot · CI, batcha typical fleetSpot can be reclaimed with a 2-minute warning: use many instance types and handle interruption.
Figure 1. Ways to pay for EC2. Cover the steady baseline with Savings Plans, run interruptible work on Spot, and use On-Demand for the rest.
Swipe sideways to see all of it, or tap to open it full screen.
Enlarge diagramApp on EC2Metadata service169.254.169.254PUT /latest/api/token (TTL header)session tokenGET credentials + token headertemporary role credentialsSet hop limit 1 (or 2 for containers) so the token cannot be fetched from further away.
Figure 2. IMDSv2 requires a session token from a PUT request, which blocks the classic SSRF trick of tricking an app into fetching instance credentials.
Swipe sideways to see all of it, or tap to open it full screen.

Auto Scaling groups

  • Launch template (AMI, type, SG, user data, IMDSv2) + ASG (min/desired/max, subnets across AZs).
  • Scaling policies: target tracking (e.g. CPU 50%, ALB requests per target), step scaling, scheduled, predictive.
  • Health checks: EC2 status or ELB health checks (replace instances the LB considers unhealthy); grace period for boot time.
  • Instance refresh for rolling AMI updates with min healthy percentage; lifecycle hooks to drain or run scripts on launch/terminate; warm pools for slow-booting apps.
  • Mixed instances policy — On-Demand base + Spot with multiple instance types and capacity-optimised allocation.
Enlarge diagramASG · MIN 2 · DESIRED 4 · MAX 10Launch templateAMI, type, SG, userdata, IMDSv2instance 1AZ ainstance 2AZ binstance 3AZ ainstance 4AZ bscalingpolicytarget CPU50%healthchecksELB: replaceunhealthy
Figure 3. An Auto Scaling group keeps the right number of healthy instances across AZs and adds or removes them by policy.
Swipe sideways to see all of it, or tap to open it full screen.

Elastic Load Balancing

ALB NLB GWLB
Layer 7 (HTTP/HTTPS/gRPC, WebSockets) 4 (TCP/UDP/TLS) 3 (appliances)
Routing Host/path/header/query rules, weighted target groups Port-based To firewall appliances via GENEVE
IPs Dynamic (use DNS name/alias) Static IP per AZ, Elastic IPs possible —
Client IP X-Forwarded-For Preserved (or proxy protocol) —
Extras WAF, Cognito/OIDC auth, redirects, fixed responses Ultra-low latency, millions of rps, PrivateLink service Inspection
  • Target types: instance, IP (pods with the AWS Load Balancer Controller — target-type: ip), Lambda.
  • Health checks per target group; deregistration delay (connection draining, default 300 s — tune down for fast deploys, align with pod preStop sleeps).
  • Cross-zone load balancing: always on for ALB; off by default on NLB (charges apply when on).
  • TLS termination with ACM certificates; SNI for multiple certs; security policies for TLS versions.
  • 502 = target returned an invalid response/closed connection (app crash, keep-alive timeout shorter than ALB idle timeout); 503 = no healthy targets; 504 = target timed out.
Enlarge diagramTarget markeddrainingdeploy or scale-inNo new requestsexisting ones continueDelay endsdefault 300 s; tune forfast deploysTarget removedsafe to stop1234
Figure 4. Deregistration delay lets in-flight requests finish before a target is removed. Align it with the app's shutdown (and a preStop sleep on Kubernetes).
Swipe sideways to see all of it, or tap to open it full screen.