Hotstar needed to handle 25 million concurrent viewers during the IPL final. Their infrastructure could not be sized for peak load year-round - it would cost crores during off-season for capacity that sits idle. Instead, they use EC2 to add capacity before the match and release it after. You pay for compute only when you use it. **EC2 (Elastic Compute Cloud)** lets you rent virtual machines on demand. Each EC2 instance is a virtual machine running on AWS's physical hardware in your chosen region and Availability Zone. You choose the OS, the instance size, the storage, and the network configuration. AWS manages the physical layer - you manage everything above it.
An engineer once ran a memory-intensive Java application on a `c5.xlarge` (compute-optimized) instance. It kept getting killed by the OOM killer. The fix was switching to `r5.xlarge` (memory-optimized) - same vCPU count, 4x the RAM, similar cost. Understanding instance families saves real money and prevents performance failures. EC2 instance types follow the naming convention: `family.size`. Example: `m6i.2xlarge`. * `m` - family (general purpose) * `6` - generation (6th generation, newer = better price/performance) * `i` - processor (Intel, `a` = AMD, `g` = Graviton ARM) * `2xlarge` - size (2x the vCPU and RAM of xlarge)
An EC2 instance is not just a virtual machine - it boots from an image that defines the OS, pre-installed software, and initial disk state. **AMIs (Amazon Machine Images)** are those images. **Launch Templates** define all the configuration for launching an instance - AMI, instance type, key pair, security groups, IAM role, user data, EBS volumes. They are the reusable, version-controlled definition of your server configuration.
During Diwali sales, Meesho's traffic spikes 15x. If they sized their fleet for peak, they would run 15x too many instances for the other 364 days. Auto Scaling Groups solve this - the fleet grows to handle the spike and shrinks when it passes. **Auto Scaling Groups (ASGs)** manage a fleet of EC2 instances. You define the minimum, maximum, and desired instance count. ASGs automatically add or remove instances based on demand, health check results, or scheduled events. ASG: min=2, desired=4, max=20 Normal load: [instance1] [instance2] [instance3] [instance4] Traffic spike: [1] [2] [3] [4] [5] [6] [7] [8] <- scale out Load drops: [1] [2] [3] [4] <- scale in Instance fails: [1] [2] [4] <- ASG replaces [3]
An ALB sits in front of your ASG and distributes incoming traffic across all healthy instances. When ASG launches a new instance, it registers it with the load balancer's target group. When an instance is unhealthy, the ALB stops sending traffic to it before ASG terminates it.
1. Create a security group for the ALB: ```bash ALB_SG=$(aws ec2 create-security-group \ --group-name "lab-alb-sg" \ --description "ALB security group" \ --vpc-id <your-vpc-id> \ --query 'GroupId' --output text) aws ec2 authorize-security-group-ingress --group-id ${ALB_SG} --protocol tcp --port 80 --cidr 0.0.0.0/0 ``` 2. Create a security group for EC2 instances (only allow traffic from ALB): ```bash APP_SG=$(aws ec2 create-security-group \ --group-name "lab-app-sg" \ --description "App server security group" \ --vpc-id <your-vpc-id> \ --query 'GroupId' --output text) aws ec2 authorize-security-group-ingress --group-id ${APP_SG} \ --protocol tcp --port 3000 --source-group ${ALB_SG} ``` 3. Create a launch template with user data that starts a Node.js app: ```bash USER_DATA=$(base64 << 'EOF' #!/bin/bash apt update -y apt install -y nodejs npm mkdir -p /opt/webserver cat > /opt/webserver/app.js << 'APPEOF' const http = require('http'); const os = require('os'); http.createServer((req, res) => { if (req.url === '/health') { res.writeHead(200); res.end('OK'); } else { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(`<h1>Hello from ${os.hostname()}</h1><p>Instance: ${process.env.INSTANCE_ID || 'unknown'}</p>`); } }).listen(3000); APPEOF node /opt/webserver/app.js & EOF ) aws ec2 create-launch-template \ --launch-template-name "lab-template" \ --launch-template-data "{ \"ImageId\": \"<your-ubuntu-ami-id>\", \"InstanceType\": \"t3.micro\", \"SecurityGroupIds\": [\"${APP_SG}\"], \"UserData\": \"${USER_DATA}\" }" ``` 4. Create the ALB, target group, and ASG (using values from above steps), then verify scaling works by checking instance count. 5. Test load balancing by requesting the ALB DNS multiple times: ```bash ALB_DNS="<your-alb-dns>.ap-south-1.elb.amazonaws.com" for i in {1..10}; do curl -s http://${ALB_DNS}/ | grep "Hello from" done ## Each request may return a different hostname - confirming load balancing works ``` **Expected result:** Different `hostname` values appear across requests, proving traffic is distributed across multiple EC2 instances behind the ALB. ---
Hotstar needed to handle 25 million concurrent viewers during the IPL final. Their infrastructure could not be sized for...
An engineer once ran a memory-intensive Java application on a c5.xlarge (compute-optimized) instance. It kept getting ki...
An EC2 instance is not just a virtual machine - it boots from an image that defines the OS, pre-installed software, and ...
During Diwali sales, Meesho's traffic spikes 15x. If they sized their fleet for peak, they would run 15x too many instan...
An ALB sits in front of your ASG and distributes incoming traffic across all healthy instances. When ASG launches a new ...
Create a security group for the ALB: Create a security group for EC2 instances (only allow traffic from ALB): Create a l...
Command Purpose aws ec2 describe-instances List all EC2 instances aws autoscaling describe-auto-scaling-groups List all ...
Aligns directly with DevOps, Site Reliability (SRE), and Platform Engineering job descriptions.