π
π§ COMPLETE AWS EC2 HANDS-ON ROADMAP
(Beginner β Intermediate β Advanced β Real-World)
π’ PHASE 1: EC2 FOUNDATIONS (YOU DID MOST OF THESE)
- β LAB 1 β Launch EC2 Instance
- β LAB 2 β Connect to EC2 using SSH
- β LAB 3 β Install Apache & Host Website
- β LAB 4 β Elastic IP (Static IP)
- β LAB 5 β Domain mapping using Amazon Route 53
π Extra Topics Covered
π‘ PHASE 2: EC2 CORE CONCEPTS (VERY IMPORTANT)
- π² LAB 6 β Security Groups (Inbound / Outbound rules)
- π² LAB 7 β Network ACLs (NACL) & difference vs Security Group
- π² LAB 8 β Key Pairs (create, delete, recover access)
- π² LAB 9 β EC2 User Data (boot-time automation)
- π² LAB 10 β EC2 Instance Lifecycle (start, stop, reboot, terminate)
π Extra Topics Covered
π PHASE 3: STORAGE & DATA (REAL PROJECT SKILLS)
- π² LAB 11 β EBS Volume (attach, detach, resize)
- π² LAB 12 β Take Snapshot & Restore EC2
- π² LAB 13 β AMI Creation (custom image)
- π² LAB 14 β Root vs Additional Volumes
- π² LAB 15 β EBS vs Instance Store
π΅ PHASE 4: SCALING & HIGH AVAILABILITY (INTERVIEW FAVORITE)
- π² LAB 16 β Launch EC2 in Multiple AZs
- π² LAB 17 β Application Load Balancer (ALB)
- π² LAB 18 β Target Groups & Health Checks
- π² LAB 19 β Auto Scaling Group (ASG)
- π² LAB 20 β High Availability Architecture
π΄ PHASE 5: MONITORING, LOGGING & ACCESS
- π² LAB 21 β CloudWatch Metrics
- π² LAB 22 β CloudWatch Alarms
- π² LAB 23 β EC2 Logs & Troubleshooting
- π² LAB 24 β IAM Role for EC2 (NO access keys)
- π² LAB 25 β Session Manager (SSH without key)
π£ PHASE 6: SECURITY & BEST PRACTICES (REAL WORLD)
- π² LAB 26 β Private EC2 + Bastion Host
- π² LAB 27 β EC2 in Public vs Private Subnet
- π² LAB 28 β Patch Management for EC2
- π² LAB 29 β EC2 Backup Strategy
- π² LAB 30 β EC2 Security Best Practices
β« PHASE 7: COST, PERFORMANCE & REAL PROJECTS
- π² LAB 31 β EC2 Pricing (On-Demand / Reserved / Spot)
- π² LAB 32 β Spot Instance Hands-on
- π² LAB 33 β Instance Rightsizing
- π² LAB 34 β Free Tier Safety Practices
- π² LAB 35 β Production EC2 Architecture (End-to-End)
π FINAL REAL-WORLD PROJECTS
- π² LAB 36 β Host Full Web App (Frontend + Backend)
- π² LAB 37 β EC2 + RDS Architecture
- π² LAB 38 β EC2 Zero-Downtime Deployment
- π² LAB 39 β EC2 Failure Recovery Simulation
- π² LAB 40 β EC2 Interview Scenario Questions
π
π§ COMPLETE AWS EC2 HANDS-ON ROADMAP
π§ͺ EC2 HANDS-ON β LAB 1
π Launch Your First EC2 Instance (Free Tier β SAFE)
π― Objective
You will:
- Launch a Free Tier EC2 instance
- Understand AMI, instance type, key pair, security group
- Get a public IP
π§ Before You Start (Very Important)
- Login to Amazon Web Services Management Console
- Make sure you are in ONE region (example:
ap-south-1 (Mumbai))
π Region matters because:
- EC2 runs inside a region
- Free tier limits are per region
πΉ STEP 1: Go to EC2 Dashboard
- Login to AWS Console
- In the search bar, type:
EC2 - Click EC2
π You are now inside the EC2 service
πΉ STEP 2: Click βLaunch Instanceβ
- Click Launch Instance
- You will see a Launch Instance page
πΉ STEP 3: Name Your Instance
-
Instance name:
ec2-hands-on-1
π This is just a label to identify your server
πΉ STEP 4: Choose AMI (Operating System)
Select:
- Amazon Linux 2023 AMI
π Why?
- Free tier eligible
- Optimized for AWS
- Secure and lightweight
β Do NOT choose Ubuntu for now
πΉ STEP 5: Choose Instance Type
Select:
- t2.micro
π Why?
- 1 vCPU
- 1 GB RAM
- FREE TIER ELIGIBLE
β οΈ Anything else = billing risk
πΉ STEP 6: Create Key Pair (VERY IMPORTANT)
Key pair = login password for your server
-
Click Create new key pair
-
Key pair name:
ec2-key-hands-on -
Key pair type: RSA
-
Private key format: .pem
-
Click Create key pair
π A .pem file will download
β οΈ DO NOT DELETE IT
Without this, you cannot login
πΉ STEP 7: Configure Network Settings (Security Group)
Click Edit under Network settings
Inbound rules (ALLOW ACCESS):
| Type | Port | Source |
|---|---|---|
| SSH | 22 | My IP |
| HTTP | 80 | Anywhere |
π Explanation:
- SSH (22) β Login to EC2
- HTTP (80) β Web server access
- My IP β Secure (only your system)
- Anywhere β Public website access
πΉ STEP 8: Storage (Leave Default)
- 8 GiB (gp3) β Leave as it is
π Free tier allows up to 30 GB
πΉ STEP 9: Launch Instance
- Review everything
- Click Launch Instance
- Click View all instances
πΉ STEP 10: Verify Instance Status
Your instance state should be:
- π‘ Initializing
- π’ Running
Click your instance and check:
- Instance State β Running
- Public IPv4 address β COPY IT
π This IP is your server on the internet
β TASK FOR YOU (DO THIS NOW)
β Launch EC2 instance β Instance state = Running β You can see Public IPv4 address
Excellent π LAB 1 completed successfully β Now we move to the next hands-on, exactly as promised.
π§ͺ EC2 HANDS-ON β LAB 2
π Connect to EC2 using SSH


π― Objective of This Lab
You will:
- Connect to your EC2 instance from your Windows laptop
- Understand SSH, key pair, user, IP
- Login successfully without fear or confusion
π§ Important Concepts
Before doing steps, understand this:
πΉ What is SSH?
SSH (Secure Shell) is a secure way to:
- Connect your laptop β EC2 server
- Run commands on the server
Think like this:
SSH = Remote control of your EC2 using text commands
πΉ What do we need for SSH?
We need 4 things:
- Public IP β Address of your EC2
- Key pair (.pem file) β Password (but more secure)
- Username β Depends on OS
- SSH command
πΉ STEP 1: Open PowerShell (Windows)
- Press Windows key
- Type:
PowerShell - Open Windows PowerShell
π Do NOT open CMD PowerShell supports SSH by default
πΉ STEP 2: Go to Folder Where .pem File Exists
Your key file name:
ec2-key-hands-on.pem
Usually it is in Downloads folder.
Run this command:
cd Downloads
π Explanation:
cd= change directoryDownloads= folder name
To confirm file exists, run:
ls
π ls shows list of files
You should see:
ec2-key-hands-on.pem
πΉ STEP 3: Copy Your EC2 Public IP
- Go to EC2 Console
- Click your instance
- Copy Public IPv4 address
Example:
13.235.xxx.xxx
πΉ STEP 4: Understand the SSH Command (IMPORTANT)
SSH command format:
ssh -i keyfile.pem username@public-ip
Now replace values π
| Part | Value |
|---|---|
| keyfile | ec2-key-hands-on.pem |
| username | ec2-user |
| public-ip | your EC2 IP |
π Why ec2-user?
Because you selected Amazon Linux AMI
πΉ STEP 5: Connect to EC2 (RUN THIS)
ssh -i ec2-key-hands-on.pem ec2-user@YOUR_PUBLIC_IP
Example:
ssh -i ec2-key-hands-on.pem ec2-user@13.235.xxx.xxx
πΉ STEP 6: Type βyesβ (First Time Only)
You will see:
Are you sure you want to continue connecting (yes/no)?
Type:
yes
Press Enter
π This happens only once It means: βI trust this serverβ
π SUCCESS CHECK (VERY IMPORTANT)
If you see something like:
[ec2-user@ip-172-31-xx-xx ~]$
β YOU ARE INSIDE EC2 SERVER
This means:
- Your laptop β connected to AWS server
- SSH is working
- Key pair is correct
π Verify You Are Really Inside EC2
Run:
whoami
Output should be:
ec2-user
Run:
pwd
Output:
/home/ec2-user
β TASK FOR YOU (LAB 2)
β Open PowerShell
β SSH into EC2
β See [ec2-user@...]$ prompt
β Run whoami
π What is a Key Pair in EC2?
A Key Pair is like a lock and key π
- Public key β Stored inside the EC2 instance (AWS keeps it)
- Private key β Downloaded by you (file like
.pemor.ppk)
This key pair is used to log in securely to your EC2 instance.
π Without a key pair, you CANNOT login to EC2
π§ When You Create a Key Pair β What Options You See
When you click Create new key pair, AWS shows 2 main options:

1οΈβ£ Key Pair Type
You will see two options:
πΉ RSA (Most common β )
- Old but very stable
- Supported everywhere
- Default choice
π Use this if you are a beginner
πΉ ED25519
- Newer & more secure
- Faster
- Not supported by some old systems
π Use this only if you clearly know you need it
β
Recommended for you: RSA
2οΈβ£ Private Key File Format (MOST IMPORTANT)
This is where people get confused π
You will see two options:
πΉ .pem file (Linux / Mac / Git Bash)
Use this when:
-
EC2 OS is Amazon Linux / Ubuntu
-
You connect using:
- Git Bash
- MobaXterm
- Mac Terminal
- Linux Terminal
πΉ .ppk file (Windows β PuTTY)
Use this when:
- You use PuTTY on Windows
- You donβt want to convert
.pemto.ppk
Used directly in PuTTY β Auth β Private key file
π§ Very Important Rule (Remember This)
| Your Tool | Choose This |
|---|---|
| Git Bash / MobaXterm | .pem |
| Mac / Linux Terminal | .pem |
| PuTTY (Windows) | .ppk |
π If confused β Always choose .pem
You can later convert .pem β .ppk, but not easily the other way.
β IMPORTANT WARNINGS (Real-life mistakes)
β οΈ Download key only ONCE
- AWS will never show it again
- If you lose it β You lose access
β οΈ Do NOT share your private key
- Anyone with this file can access your server
β οΈ Do NOT upload key to GitHub
- This is a serious security risk
π§ Short Memory Trick
Linux server =
.pemPuTTY user =.ppk
π What βKey Pairβ Means Internally (Big Picture)
An EC2 key pair uses asymmetric encryption.
That means:
- π Public Key β stored on the EC2 server
- ποΈ Private Key β stored ONLY with you
- They work together, not separately
Think of it like:
Public key = Lock Private key = Only key that can open that lock
π§ Step-by-Step: What Happens Internally

When EC2 instance is launched:
- The public key is copied into the server file:
~/.ssh/authorized_keys
This file lives inside the EC2 instance.
π This file decides who is allowed to log in.
π’ You try to connect (SSH)
You run:
ssh -i mykey.pem ec2-user@<public-ip>
What happens internally:
- You say: βHey server, I want to loginβ
- Server says: βProve you are authorizedβ
π’ Server sends a challenge π
The EC2 server:
- Creates a random encrypted message
- Encrypts it using the public key
- Sends it to your system
β οΈ Important:
- Only the matching private key can decrypt it
π’ Step 5: Your private key responds π
Your computer:
- Uses your private key
- Decrypts the message
- Sends the correct response back
π’ Step 6: Server verifies and allows login β
Server checks:
- βDoes this response match what I expected?β
If YES:
- Login allowed π
If NO:
- β Permission denied
π Internal Flow (Very Simple)
You (Private Key)
β
Decrypt challenge
β
Send proof
β
EC2 checks using Public Key
β
Login allowed
π Why Password Is NOT Used
EC2 does NOT use passwords by default because:
β Passwords can be:
- Brute-forced
- Stolen
- Guessed
β Keys are:
- Very long
- Mathematically linked
- Impossible to guess
π₯ What If Someone Gets Public Key?
Nothing happens.
βοΈ Public key:
- Can be shared
- Is useless without private key
β Private key:
- MUST be protected
- Gives full access
𧨠What If You Lose the Private Key?
Internally:
- Server has the public key
- You donβt have the private key
- Authentication fails forever β
Result:
-
You cannot login
-
You must:
- Detach root volume
- Attach to another EC2
- Add a new public key manually
π§ One-Line Summary (Interview Ready)
EC2 uses SSH key-based authentication where the public key is stored on the server and the private key proves the clientβs identity without ever being sent over the network.
π§ͺ Real-World Analogy
- Public key β Lock on your house
- Private key β Actual key in your pocket
- SSH login β Trying to open the door
π§ͺ EC2 HANDS-ON β LAB 3
π Install Apache Web Server & Host Your First Website

π― Objective of This Lab
You will:
- Install Apache (httpd) on EC2
- Start the web server
- Host a simple web page
- Access it using browser + public IP
π§ Important Concept (Simple Words)
πΉ What is Apache?
Apache (httpd) is a web server.
Meaning:
- It listens on port 80
- When someone opens your IP in browser
- Apache sends a web page
πΉ STEP 1: Update Your EC2 Server
Run this command:
sudo dnf update -y
π Explanation:
sudoβ run command as admin (root)dnfβ package manager (like Play Store)updateβ update system packages-yβ auto-approve (no questions)
πΉ STEP 2: Install Apache (httpd)
Run:
sudo dnf install httpd -y
π Explanation:
installβ install softwarehttpdβ Apache web server package
πΉ STEP 3: Start Apache Server
sudo systemctl start httpd
π Explanation:
systemctlβ service managerstartβ start servicehttpdβ Apache service
πΉ STEP 4: Enable Apache on Boot (VERY IMPORTANT)
sudo systemctl enable httpd
π Why?
- If EC2 restarts
- Apache starts automatically
πΉ STEP 5: Check Apache Status
sudo systemctl status httpd
You should see:
Active: active (running)
Press:
q
to exit status screen
πΉ STEP 6: Test in Browser (BIG MOMENT π)
- Copy your EC2 Public IP
- Open browser
- Paste:
http://YOUR_PUBLIC_IP
Expected Result:
π’ Apache Test Page
β This means:
- EC2 is running
- Apache is working
- Security group allows HTTP (port 80)
πΉ STEP 7: Create Your Own Web Page
Apache default folder:
/var/www/html
Go to folder:
cd /var/www/html
πΉ STEP 8: Create HTML File
sudo nano index.html
π nano = simple text editor
Paste this:
<!DOCTYPE html>
<html>
<head>
<title>My First EC2 Website</title>
</head>
<body>
<h1>Hello from EC2 π</h1>
<p>Apache Web Server is working!</p>
</body>
</html>
Save & Exit:
- Press CTRL + X
- Press Y
- Press Enter
πΉ STEP 9: Refresh Browser
Open again:
http://YOUR_PUBLIC_IP
π You should see your own website
π COMMON ISSUE CHECK
If page does not open:
- Check Security Group β HTTP (80) allowed
- Instance state = Running
- Apache status = active
β TASK FOR YOU (LAB 3)
β Apache installed β Apache running β Custom HTML page created β Website opens using public IP
π§ͺ EC2 HANDS-ON β LAB 4
π Elastic IP (Static Public IP) β No More IP Changes

π― Objective of This Lab
You will:
- Understand why public IP changes
- Create an Elastic IP (EIP)
- Attach it to your EC2 instance
- Access your website using a fixed IP
π§ Why Public IP Changes? (Simple Explanation)
Because the public IP is temporary unless you reserve it.
-
AWS gives you a temporary public IP by default.
-
When the instance stops and restarts, that IP is taken back by AWS.
β Bad for:
- Websites
- APIs
- Domain mapping
π¨ IMPORTANT RULE (Remember This)
|Action | Public IP| |--- |---| |Reboot EC2 | β Does NOT change| |Stop EC2 | β Changes| |Terminate EC2 | β Instance gone| |Start EC2 | β New IP|
π° Cost: π ~$0.005 per hour per Elastic IP
π§ What is Elastic IP?
Elastic IP (EIP) is:
- A static public IPv4 address
- Belongs to your AWS account
- You can attach/detach to EC2
Think like:
Elastic IP = Permanent phone number π± Public IP = Temporary number
β οΈ Cost Warning (IMPORTANT)
-
Free when attached to running EC2
-
Charged if:
- Not attached
- Instance stopped
π We will keep it attached
πΉ STEP 1: Go to Elastic IPs
- Open EC2 Console
- Left menu β Elastic IPs
- Click Allocate Elastic IP address
πΉ STEP 2: Allocate Elastic IP
- Network border group β Leave default
- Click Allocate
You will now see a new Elastic IP
πΉ STEP 3: Associate Elastic IP to EC2
- Select Elastic IP
- Click Actions β Associate Elastic IP
- Resource type β Instance
- Instance β select
ec2-hands-on-1 - Click Associate
πΉ STEP 4: Verify Association
-
Elastic IP shows:
- Instance ID
- Private IP
Your EC2 now has:
- Static public IP
πΉ STEP 5: Test Website Using Elastic IP
Open browser:
http://ELASTIC_IP
π’ Your website should load
πΉ STEP 6: Confirm IP Stability (Understanding)
If you:
- Restart EC2 β (DONβT DO NOW)
- Elastic IP will NOT change
π IMPORTANT RULE
If you ever:
- Terminate EC2 π First release Elastic IP Otherwise AWS will charge
β TASK FOR YOU (LAB 4)
β Elastic IP allocated β Elastic IP associated to EC2 β Website opens using Elastic IP
π§ͺ EC2 HANDS-ON β LAB 5
π Attach a Domain to EC2 using Route 53 (Real-World Hosting)

π― Objective of This Lab
You will:
- Understand DNS & Domain flow
- Create a Hosted Zone in Route 53
- Map your Elastic IP β Domain
- Open your website using a domain name π
π§ First: Understand the Flow (VERY IMPORTANT)
When someone types:
www.example.com
What happens?
- Browser asks DNS
- DNS replies β Elastic IP
- Elastic IP points β EC2
- Apache sends the website
π Route 53 = DNS service
π§ What is Route 53?
Amazon Route 53 is:
- AWS DNS service
- Converts domain β IP address
- Highly available & fast
β οΈ IMPORTANT NOTE (READ CAREFULLY)
There are TWO CASES:
β Case 1: You already bought a domain
(from GoDaddy / Namecheap / Route 53)
β Continue with this lab
β Case 2: You donβt have a domain yet
β You can still practice using a fake domain name (no website will open publicly, but concept is same)
Iβll explain both safely.
πΉ STEP 1: Open Route 53
- AWS Console β search Route 53
- Open Route 53
- Click Hosted zones
- Click Create hosted zone
πΉ STEP 2: Create Hosted Zone
Fill details:
-
Domain name Example:
devcloudpractice.com -
Type β Public Hosted Zone
Click Create hosted zone
π Hosted Zone = DNS control panel for your domain
πΉ STEP 3: Understand Name Servers (CRITICAL)
After creation, youβll see 4 NS records, like:
ns-123.awsdns-45.com
ns-678.awsdns-90.net
...
π These are AWS DNS servers
πΉ STEP 4: Update Name Servers (Only if Domain Bought Outside AWS)
If your domain is from:
- GoDaddy
- Namecheap
- Hostinger
Go to domain provider:
- Open DNS / Nameserver settings
- Replace existing name servers
- Paste Route 53 name servers
- Save
β³ DNS propagation: 5β30 minutes (sometimes 24 hrs)
β οΈ If domain bought from Route 53 β skip this step
πΉ STEP 5: Create A Record (Domain β EC2)
Inside Hosted Zone:
-
Click Create record
-
Record type β A
-
Record name:
- Leave empty β root domain
(
example.com)
- Leave empty β root domain
(
-
Value:
- Paste Elastic IP
-
TTL β default
-
Click Create records
π A record = maps domain β IP
πΉ STEP 6: (Optional) Create www Record
Create another record:
-
Record name:
www -
Type β A
-
Value β Elastic IP
This enables:
www.example.com
πΉ STEP 7: Test Your Domain π
Open browser:
http://yourdomain.com
or
http://www.yourdomain.com
π’ Your EC2 website should load
π§ Real-World Knowledge (Interview Ready)
β Route 53 does not host websites β It only does DNS resolution β EC2 + Apache hosts the website β Elastic IP ensures static mapping
π Common Issues Checklist
| Issue | Fix |
|---|---|
| Site not opening | Wait DNS propagation |
| IP wrong | Check Elastic IP |
| Apache down | systemctl status httpd |
| HTTP blocked | Security Group port 80 |
β TASK FOR YOU (LAB 5)
β Hosted Zone created β A record added β Domain mapped to Elastic IP
π EC2 Security Group β SSH, Ports, and IP Access
1οΈβ£ What is a Security Group in EC2?
-
A Security Group is a virtual firewall
-
It controls:
- Who can reach your EC2 instance
- On which port
-
It works at network level (before login)
π If traffic is not allowed by Security Group β EC2 never receives it.
2οΈβ£ What does 0.0.0.0/0 mean?
0.0.0.0/0 = ANY IP address on the internet
β οΈ This means publicly open to the world.
3οΈβ£ What does βMy IPβ mean?
When you select My IP, AWS:
- Detects your current public internet IP
- Sets the rule as:
x.x.x.x/32
/32 means:
- Only ONE exact IP
- Only your current network
π This is secure and recommended.
4οΈβ£ Understanding the 3 Important Ports
π Port 22 β SSH
Used for:
- Remote login to EC2
- Full server control (admin access)
What SSH gives:
- File access
- Command execution
- Full OS control
π This is the most sensitive port
π Port 80 β HTTP
Used for:
- Website access via browser
- Public web traffic
What HTTP gives:
- Only what your application exposes
- No OS access
- No command execution
π Designed to be public
βοΈ Port 8080 β Application Port
Used for:
- Spring Boot
- Node.js
- Test applications
Access level:
- Application only
- Not OS-level
π Safe for testing, risky if admin APIs are exposed
5οΈβ£ Why ONLY SSH is dangerous when open to 0.0.0.0/0
Key reason:
SSH provides full administrative access to the server
Comparison:
| Port | Access Type | Risk |
|---|---|---|
| 22 (SSH) | Full OS control | π₯ Very High |
| 80 (HTTP) | Web content only | π’ Low |
| 8080 | App-level access | π‘ Medium |
π Admin access should never be public
6οΈβ£ What happens if SSH is set to 0.0.0.0/0
Important clarification:
β Anyone CANNOT login without .pem key
β Anyone CAN try to connect
What attackers can do:
-
Scan your public IP
-
Detect open port 22
-
Attempt:
- Usernames
- Stolen keys
- Brute-force attacks
Even if they fail:
- Continuous attack attempts
- Log flooding
- Resource usage
- High security risk
7οΈβ£ Two Levels of EC2 Security (Very Important)
Level 1οΈβ£: Security Group (Network Gate)
- Controls who can reach the port
Level 2οΈβ£: SSH Authentication (Login Lock)
-
Requires:
- Correct username
- Correct private key (
.pem)
π 0.0.0.0/0 opens the gate, not the lock
8οΈβ£ Why .pem key is still required
- EC2 uses key-based authentication
- Password login is disabled by default
- Private key is never sent over the network
So:
- β No
.pemβ No login - β Even AWS canβt login without it
9οΈβ£ Recommended Safe Configuration
| Port | Source | Status |
|---|---|---|
| 22 (SSH) | My IP | β Secure |
| 80 (HTTP) | 0.0.0.0/0 | β Required |
| 8080 | 0.0.0.0/0 (temporary) | β οΈ OK for testing |
π Golden Security Rule (Must Remember)
Never expose administrative access (SSH) to the public internet.
- SSH β restrict to your IP
- HTTP/HTTPS β public
- App ports β open only if needed
π― Interview-Ready One-Line Answer
Opening SSH to 0.0.0.0/0 allows anyone on the internet to attempt connections, increasing attack risk. SSH must be restricted to trusted IPs, while HTTP is designed for public access.
π§ͺ EC2 HANDS-ON β LAB 6
π Security Groups (Inbound & Outbound Rules β Deep Hands-On)

π― Objective of LAB 6
By the end of this lab, you will:
- Create & modify Security Group rules
- Understand Inbound vs Outbound
- Prove that Security Groups are STATEFUL
- Be able to explain this confidently in interviews
π§ FIRST: What is a Security Group? (Very Simple)
A Security Group (SG) is:
- A virtual firewall
- Controls who can access your EC2
- Works at instance level
Think like this:
Security Group = Security guard at the door πͺ Only allowed people can enter
π§ IMPORTANT RULE (MEMORIZE)
β Security Groups:
- β Allow rules only
- β No deny rules
- β Are STATEFUL
We will prove this practically.
πΉ STEP 1: Open Your EC2 Security Group
- AWS Console β EC2
- Click Instances
- Select your instance
ec2-hands-on-1 - Go to Security tab
- Click the Security group name
You are now inside Security Group settings
πΉ STEP 2: Understand Existing Inbound Rules
You should already see something like:
| Type | Port | Source |
|---|---|---|
| SSH | 22 | My IP |
| HTTP | 80 | Anywhere |
What this means:
- SSH β You can connect from your laptop
- HTTP β Anyone can open your website
πΉ STEP 3: TEST 1 β Remove HTTP Access (Hands-On Proof)
β Remove HTTP Rule
- Click Edit inbound rules
- β Delete HTTP (port 80)
- Click Save rules
π TEST IN BROWSER
Open:
http://ELASTIC_IP
Expected Result:
β Website WILL NOT OPEN
β This proves:
- Security Group controls traffic
- Port 80 is required for web access
πΉ STEP 4: Add HTTP Rule Back
- Edit inbound rules
- Add rule:
| Setting | Value |
|---|---|
| Type | HTTP |
| Port | 80 |
| Source | Anywhere (0.0.0.0/0) |
- Save rules
π Refresh browser
Result:
π’ Website opens again
πΉ STEP 5: TEST 2 β Change SSH Source (IMPORTANT)
β Break SSH Access (on purpose)
- Edit inbound rules
- Change SSH source from:
My IP
to:
0.0.0.0/0
- Save
π Meaning:
- Anyone on internet can try SSH (β unsafe)
β οΈ We will fix it later
πΉ STEP 6: Understand INBOUND vs OUTBOUND
Inbound Rules
π Who can come IN to EC2 Examples:
- SSH (22)
- HTTP (80)
Outbound Rules
π Where EC2 can go OUT
Default outbound rule:
All traffic β 0.0.0.0/0
π Means:
- EC2 can access internet
- Install updates
- Download packages
πΉ STEP 7: STATEFUL PROOF (VERY IMPORTANT)
Security Groups are STATEFUL.
What does that mean?
If:
- Inbound request is allowed Then:
- Response is automatically allowed (No outbound rule needed)
π Example:
- Browser β EC2 (HTTP allowed)
- EC2 β Browser (response allowed automatically)
π‘ This is interview GOLD
πΉ STEP 8: FIX SECURITY (BEST PRACTICE)
Now make SSH secure again π
- Edit inbound rules
- Change SSH source back to:
My IP
- Save
β Your EC2 is secure again
π§ INTERVIEW-READY ANSWER (MEMORIZE)
Security Group is a stateful virtual firewall that controls inbound and outbound traffic at the EC2 instance level. It supports only allow rules, and responses to allowed inbound traffic are automatically permitted.
β LAB 6 TASK CHECKLIST
β Removed HTTP and tested β Added HTTP and tested β Understood inbound vs outbound β Proved STATEFUL behavior β Secured SSH again
π§ͺ EC2 HANDS-ON β LAB 7
π Network ACL (NACL) vs Security Group (Hands-On + Deep Clarity)

π― Objective of LAB 7
By the end of this lab, you will:
- Create & modify a Network ACL
- See how it works at subnet level
- PROVE it is STATELESS
- Understand NACL vs Security Group (interview-ready)
π§ FIRST: What is a Network ACL? (Simple Words)
A Network ACL (NACL) is:
- A firewall for a subnet
- Works before traffic reaches EC2
- Applies to ALL instances in that subnet
Think like:
NACL = Security gate at society entrance π’ Security Group = Guard at each flat πͺ
π§ VERY IMPORTANT RULES (MEMORIZE)
| Feature | NACL |
|---|---|
| Level | Subnet |
| Rules | Allow + Deny |
| Nature | STATELESS |
| Order | Rule number (100, 110β¦) |
| Default | Allow all |
πΉ STEP 1: Open Network ACLs
- AWS Console β VPC
- Left menu β Network ACLs
- You will see a default NACL
πΉ STEP 2: Identify Your Subnet
- Go to EC2 β Instances
- Click your instance
- Note Subnet ID
π We will apply NACL to this subnet
πΉ STEP 3: Create Custom NACL
-
VPC β Network ACLs
-
Click Create network ACL
-
Name:
ec2-hands-on-nacl -
Select your VPC
-
Click Create
πΉ STEP 4: Associate NACL with Subnet
- Select your new NACL
- Go to Subnet associations
- Click Edit subnet associations
- Select your EC2 subnet
- Save
β οΈ This NACL now controls traffic for your EC2
πΉ STEP 5: BLOCK HTTP Using NACL (Hands-On Proof)
Inbound Rule β DENY HTTP
- Select Inbound rules
- Click Edit inbound rules
- Add rule:
| Rule # | Type | Port | Source | Action |
|---|---|---|---|---|
| 100 | HTTP | 80 | 0.0.0.0/0 | DENY |
- Save
π TEST IN BROWSER
Open:
http://ELASTIC_IP
β Website WILL NOT OPEN
π Even though:
- Security Group allows HTTP
- NACL denies it
π NACL blocks first
πΉ STEP 6: ALLOW HTTP Again
Add ALLOW rule with higher priority:
| Rule # | Type | Port | Source | Action |
|---|---|---|---|---|
| 90 | HTTP | 80 | 0.0.0.0/0 | ALLOW |
π Rule 90 runs before 100
π Refresh browser β π’ Works
πΉ STEP 7: PROVE STATELESS BEHAVIOR (IMPORTANT)
β Remove Outbound Rule
- Go to Outbound rules
- Delete rule:
ALLOW ALL (0.0.0.0/0)
- Save
π Test Website Again
β Website WILL NOT LOAD
π Why?
- Inbound allowed
- Outbound response blocked
π This PROVES NACL is STATELESS
πΉ STEP 8: FIX OUTBOUND RULE (IMPORTANT)
Add outbound rule:
| Rule # | Type | Port | Destination | Action |
|---|---|---|---|---|
| 100 | HTTP | 80 | 0.0.0.0/0 | ALLOW |
Also add:
| Rule # | Type | Port | Destination | Action |
|---|---|---|---|---|
| 110 | HTTPS | 443 | 0.0.0.0/0 | ALLOW |
Save
πΉ STEP 9: FINAL COMPARISON (INTERVIEW TABLE)
| Feature | Security Group | NACL |
|---|---|---|
| Level | Instance | Subnet |
| Rules | Allow only | Allow + Deny |
| Nature | Stateful | Stateless |
| Rule Order | No order | Number based |
| Scope | Specific EC2 | All in subnet |
π§ INTERVIEW-READY ANSWER
Security Groups act as a stateful firewall at the instance level allowing only permitted traffic, whereas Network ACLs operate at the subnet level, are stateless, and support both allow and deny rules evaluated in order.
β οΈ CLEANUP (VERY IMPORTANT)
To avoid confusion later:
- Either restore default NACL
- Or keep both inbound & outbound properly allowed
β LAB 7 TASK CHECKLIST
β Created custom NACL β Associated subnet β Denied & allowed HTTP β Proved stateless behavior β Understood SG vs NACL clearly
π§ͺ EC2 HANDS-ON β LAB 8
π EC2 Key Pairs (Create, Use, Delete & Recover Access)

π― Objective of LAB 8
You will:
- Understand what a key pair really is
- Create & attach new key pairs
- Know what happens if key is deleted
- Learn key recovery concept (very important)
π§ FIRST: What is an EC2 Key Pair?
A Key Pair has:
- Public key β Stored in EC2
- Private key (.pem) β Stored on your laptop
Think like:
Lock (public key) is on server π Key (private key) is with you π
AWS never stores private key.
πΉ STEP 1: View Existing Key Pair
- EC2 Console β Key Pairs
- You will see:
ec2-key-hands-on
π AWS only shows key name, not the file
πΉ STEP 2: Create a SECOND Key Pair
- Click Create key pair
- Name:
ec2-key-backup
- Type: RSA
- Format: .pem
- Create & download
π This is for learning purpose
πΉ STEP 3: Understand a CRITICAL RULE
β You cannot directly change key pair of:
- Running instance
- Stopped instance
Key pair is:
- Injected at launch time
πΉ STEP 4: What Happens If Key is Deleted? (Concept)
Scenario:
.pemfile deleted from laptop β- EC2 still running
Result:
β You are LOCKED OUT
AWS cannot recover it
πΉ STEP 5: HOW TO RECOVER ACCESS (IMPORTANT CONCEPT)
There are 3 real-world recovery methods:
π’ Method 1: EC2 Instance Connect (AWS Linux only)
-
Works only if:
- Instance supports it
- Port 22 allowed
-
Temporary access
π’ Method 2: Detach Root Volume (MOST COMMON)
Steps (conceptual):
- Stop instance
- Detach root EBS
- Attach it to another EC2
- Add new public key to:
~/.ssh/authorized_keys
- Reattach volume
- Start instance
π Very important interview topic
π’ Method 3: Session Manager (BEST PRACTICE)
- Uses IAM Role
- No SSH, no key pair
- Secure & auditable
Weβll do this hands-on later
πΉ STEP 6: BEST PRACTICES (MEMORIZE)
β Always keep backup key
β Use IAM Roles + Session Manager
β Never share .pem
β Restrict SSH source
β Rotate keys in production
π§ INTERVIEW-READY ANSWER
An EC2 key pair consists of a public key stored on the instance and a private key held by the user. AWS does not store the private key, and if it is lost, access must be recovered through volume attachment or Session Manager.
π DO NOT TRY KEY RECOVERY NOW
We will do it safely in advanced labs.
β LAB 8 TASK CHECKLIST
β Understood key pair concept β Created backup key β Learned recovery methods β Understood best practices
π§ͺ EC2 HANDS-ON β LAB 9
π EC2 User Data (Automatic Setup at Launch)

π― Objective of LAB 9
By the end of this lab, you will:
- Understand what User Data is
- Use User Data to auto-install Apache
- Deploy a website WITHOUT SSH
- Learn cloud-init concept (interview topic)
π§ FIRST: What is EC2 User Data? (Very Simple)
User Data is:
- A script that runs only once
- Runs when EC2 starts for the first time
- Used for automation
Think like:
User Data = Auto-setup instructions for EC2 π§
Example:
- Install Apache
- Start service
- Create website
- Configure app
π§ VERY IMPORTANT RULES (MEMORIZE)
β User Data:
- Runs only at first launch
- Runs as root user
- Uses cloud-init
- Best for bootstrapping
πΉ WHAT WE WILL DO (PLAN)
We will:
- Launch NEW EC2 instance
- Add User Data script
- Apache installs automatically
- Website works without SSH
πΉ STEP 1: Launch a NEW EC2 Instance
Go to: EC2 β Instances β Launch instance
Instance name:
ec2-userdata-demo
πΉ STEP 2: Choose AMI & Instance Type
- AMI β Amazon Linux 2023
- Instance type β t2.micro (Free tier)
πΉ STEP 3: Select Key Pair
Choose:
ec2-key-hands-on
(We wonβt SSH, but AWS requires it)
πΉ STEP 4: Network & Security Group
Inbound rules:
| Type | Port | Source |
|---|---|---|
| HTTP | 80 | Anywhere |
| SSH | 22 | My IP |
πΉ STEP 5: ADD USER DATA (MOST IMPORTANT)
Scroll down β Advanced details
Find:
π User data
Paste EXACTLY this:
#!/bin/bash
dnf update -y
dnf install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello from EC2 User Data π</h1>" > /var/www/html/index.html
π§ EXPLAIN SCRIPT LINE BY LINE (IMPORTANT)
#!/bin/bash
β‘ Tells EC2: βThis is a Linux shell scriptβ
dnf update -y
β‘ Updates system packages
dnf install httpd -y
β‘ Installs Apache web server
systemctl start httpd
β‘ Starts Apache
systemctl enable httpd
β‘ Starts Apache on reboot
echo "<h1>Hello from EC2 User Data π</h1>" > /var/www/html/index.html
β‘ Creates website homepage automatically
πΉ STEP 6: Launch Instance
Click Launch instance
Wait until: π’ Instance state β Running
πΉ STEP 7: Test WITHOUT SSH (BIG MOMENT π)
- Copy Public IP
- Open browser:
http://PUBLIC_IP
Expected Output:
Hello from EC2 User Data π
β Apache installed β Website deployed β No manual work
πΉ STEP 8: Verify User Data Ran (Optional SSH)
If you want to check logs:
sudo cat /var/log/cloud-init-output.log
π Shows user data execution logs
π§ INTERVIEW-READY ANSWER
EC2 User Data is a cloud-init based mechanism that allows running scripts at instance launch to automate configuration such as installing software and starting services. It runs only once at first boot.
β οΈ COMMON MISTAKES (VERY IMPORTANT)
β Forget #!/bin/bash
β Wrong indentation
β Expecting script to run again after reboot
β Missing HTTP rule in SG
β LAB 9 TASK CHECKLIST
- β New EC2 launched
- β User Data added
- β Apache auto-installed
- β Website works without SSH
- β Understood automation concept
π§ͺ EC2 HANDS-ON β LAB 10
π EC2 Instance Lifecycle (Start, Stop, Reboot, Terminate)

π― Objective of LAB 10
You will:
- Perform Start / Stop / Reboot / Terminate
- Understand what happens to IP, storage, billing
- Learn when to use each action
- Be interview-ready
π§ FIRST: EC2 Lifecycle States (Simple)
An EC2 instance can be in:
| State | Meaning |
|---|---|
| Pending | Starting |
| Running | Active |
| Stopped | Powered off |
| Terminated | Deleted forever |
πΉ STEP 1: Identify Two Instances
You should have:
ec2-hands-on-1(Elastic IP attached)ec2-userdata-demo
We will safely test lifecycle on ec2-userdata-demo
πΉ STEP 2: STOP the Instance
- EC2 β Instances
- Select
ec2-userdata-demo - Instance state β Stop
- Confirm
π What Happens When STOP?
| Item | Result |
|---|---|
| Instance | OFF |
| Billing | β No compute cost |
| EBS root | β Preserved |
| Public IP | β Released |
| Elastic IP | β Not attached |
πΉ STEP 3: START the Instance Again
- Select instance
- Instance state β Start
Wait β Running
π Check Public IP
- Public IP will be NEW
- Website still works (Apache already installed)
π This proves:
- Data remains
- Public IP changes
πΉ STEP 4: REBOOT the Instance
- Select instance
- Instance state β Reboot
π What Happens When REBOOT?
| Item | Result |
|---|---|
| OS | Restarts |
| Public IP | β Same |
| Data | β Safe |
| Billing | β Continues |
π Reboot = Restart laptop
πΉ STEP 5: TERMINATE the Instance (IMPORTANT)
β οΈ THIS IS DESTRUCTIVE
- Select
ec2-userdata-demo - Instance state β Terminate
- Confirm
π What Happens When TERMINATE?
| Item | Result |
|---|---|
| Instance | β Deleted |
| EBS root | β Deleted |
| Public IP | β Gone |
| Recovery | β Impossible |
π Terminate = Delete forever
π§ VERY IMPORTANT INTERVIEW POINTS
- Stop β Save money
- Start β New public IP
- Reboot β Same IP
- Terminate β Data lost
π§ INTERVIEW-READY ANSWER
Stopping an EC2 instance halts compute billing while preserving EBS volumes, whereas terminating an instance permanently deletes the instance and associated root volume, making recovery impossible.
β οΈ COST BEST PRACTICES
- Stop unused instances
- Use Elastic IP for static access
- Terminate unused test EC2s
- Monitor free tier usage
β LAB 10 TASK CHECKLIST
- Stopped instance
- Started instance
- Rebooted instance
- Terminated test instance
- Understood lifecycle fully
π§ͺ EC2 HANDS-ON β LAB 11
π EBS Volumes (Attach, Mount, Detach & Resize)

π― Objective of LAB 11
By the end of this lab, you will:
- Understand what EBS is
- Create an extra EBS volume
- Attach it to EC2
- Mount it inside Linux
- Resize it safely
- Be interview-ready
π§ FIRST: What is EBS? (Simple Words)
EBS (Elastic Block Store) is:
- A virtual hard disk
- Used by EC2
- Persistent storage
Think like:
EC2 = Laptop π» EBS = Hard disk πΎ
Even if EC2 stops β data stays.
π§ IMPORTANT TYPES (FOR NOW)
Weβll use:
- gp3 (General Purpose SSD)
π Free tier friendly π Balanced performance
πΉ STEP 1: Identify Your EC2 Instance
Use:
ec2-hands-on-1
β οΈ Do NOT use terminated instance
πΉ STEP 2: Create a NEW EBS Volume
- AWS Console β EC2
- Left menu β Volumes
- Click Create volume
Fill details:
- Volume type β gp3
- Size β 5 GiB
- Availability Zone β SAME as EC2
- Name:
ebs-hands-on-1
Click Create volume
π AZ must match EC2 AZ
πΉ STEP 3: Attach EBS Volume to EC2
- Select volume
ebs-hands-on-1 - Actions β Attach volume
- Instance β
ec2-hands-on-1 - Device β
/dev/xvdf - Attach
πΉ STEP 4: Connect to EC2 (SSH)
ssh -i ec2-key-hands-on.pem ec2-user@ELASTIC_IP
πΉ STEP 5: Check Disk is Attached
Run:
lsblk
π Explanation:
lsblkβ list block devices (disks)
You will see:
xvdaβ root diskxvdfβ new EBS (no mount yet)
πΉ STEP 6: Format the New Volume
sudo mkfs -t xfs /dev/xvdf
π Explanation:
mkfsβ make filesystemxfsβ Linux filesystem/dev/xvdfβ new disk
β οΈ Formatting erases data (safe now)
πΉ STEP 7: Create Mount Directory
Mounting an EBS volume is the process of making an attached storage volume accessible to the operating system at a specific directory path.
sudo mkdir /data
π /data = folder to access EBS
β Why mounting is required?
Because:
- EC2 OS doesnβt know where to store data
- EBS is just a block device
- OS needs a mount point (folder)
π What happens if you DONβT mount?
- Volume is attached β
- OS cannot read/write β
- Storage is wasted
πΉ STEP 8: Mount the Volume
sudo mount /dev/xvdf /data
πΉ STEP 9: Verify Mount
df -h
π You should see /data mounted
πΉ STEP 10: Make Mount Persistent (IMPORTANT)
Open fstab:
sudo nano /etc/fstab
Add line at bottom:
/dev/xvdf /data xfs defaults,nofail 0 2
Save & exit:
- CTRL + X β Y β Enter
π This ensures mount survives reboot
πΉ STEP 11: Resize EBS Volume (Hands-On)
Increase volume size
- EC2 β Volumes
- Select
ebs-hands-on-1 - Actions β Modify volume
- Change size:
5 β 10 GiB
- Save
Extend filesystem (inside EC2)
sudo xfs_growfs /data
π Expands filesystem to new size
Verify:
df -h
π§ VERY IMPORTANT INTERVIEW POINTS
β EBS is AZ-specific β EBS is persistent β Can attach/detach β Can resize without downtime β Root volume is also EBS
π§ INTERVIEW-READY ANSWER
Amazon EBS is a persistent block storage service designed for EC2 instances. It provides durable storage that can be attached, detached, resized, and snapshotted independently of the instance lifecycle.
β οΈ COMMON MISTAKES
β Different AZ for volume
β Forgetting filesystem resize
β Not updating /etc/fstab
β Formatting wrong disk
β LAB 11 TASK CHECKLIST
β Created EBS volume β Attached to EC2 β Formatted & mounted β Made persistent β Resized volume
π§ͺ EC2 HANDS-ON β LAB 12
π EBS Snapshots & Restore (Backup & Disaster Recovery)

π― Objective of LAB 12
By the end of this lab, you will:
- Create an EBS snapshot (backup)
- Restore a new volume from snapshot
- Attach it to EC2
- Prove data recovery
- Understand real-world backup strategy
π§ FIRST: What is an EBS Snapshot? (Simple)
An EBS Snapshot is:
- A backup of an EBS volume
- Stored in Amazon S3 (managed by AWS)
- Incremental (only changes are saved)
Think like:
Snapshot = Photo of your disk πΈ Volume = Live hard disk πΎ
π§ VERY IMPORTANT RULES (MEMORIZE)
β Snapshots are AZ-independent β Volumes are AZ-dependent β Snapshots are incremental β Used for backup & restore
πΉ STEP 1: Put Test Data on EBS Volume
SSH into your EC2:
ssh -i ec2-key-hands-on.pem ec2-user@ELASTIC_IP
Create a test file:
sudo echo "EBS Snapshot Test File" > /data/test.txt
Verify:
cat /data/test.txt
πΉ STEP 2: Create Snapshot
- AWS Console β EC2
- Left menu β Volumes
- Select volume
ebs-hands-on-1 - Actions β Create snapshot
Details:
- Name:
snapshot-ebs-hands-on-1
- Description:
Backup before changes
Click Create snapshot
πΉ STEP 3: Verify Snapshot
- Go to Snapshots
- Status β Completed
π Snapshot is safely stored
πΉ STEP 4: Create NEW Volume from Snapshot
- Select snapshot
- Actions β Create volume
Settings:
- Volume type β gp3
- Availability Zone β Same as EC2
- Size β Leave default
- Name:
ebs-restored-from-snapshot
Click Create volume
πΉ STEP 5: Attach Restored Volume to EC2
- Select new volume
- Actions β Attach volume
- Instance β
ec2-hands-on-1 - Device β
/dev/xvdg - Attach
πΉ STEP 6: Verify New Disk Inside EC2
lsblk
You should see:
xvdgβ restored volume
πΉ STEP 7: Mount Restored Volume
Create mount folder:
sudo mkdir /restore
Mount:
sudo mount /dev/xvdg /restore
πΉ STEP 8: VERIFY DATA RECOVERY (MOST IMPORTANT)
ls /restore
cat /restore/test.txt
π Output:
EBS Snapshot Test File
β Backup & restore successful
π§ REAL-WORLD USE CASES
- Backup before OS upgrade
- Disaster recovery
- Clone environments
- Create AMIs
- Cross-region backup (copy snapshot)
π§ INTERVIEW-READY ANSWER
An EBS snapshot is an incremental backup of an EBS volume stored in Amazon S3. Snapshots are AZ-independent and can be used to restore volumes or create new volumes across Availability Zones.
β οΈ COMMON MISTAKES
- β Creating volume in wrong AZ
- β Forgetting to mount restored volume
- β Assuming snapshot is full copy (itβs incremental)
- β Deleting snapshot before restore
β LAB 12 TASK CHECKLIST
- β Created snapshot
- β Restored volume
- β Attached to EC2
- β Mounted volume
- β Verified recovered data
π§ͺ EC2 HANDS-ON β LAB 13
π Create Custom AMI (Golden Image for EC2)

π― Objective of LAB 13
By the end of this lab, you will:
- Create a custom AMI
- Launch new EC2 from that AMI
- Prove pre-installed software exists
- Understand Golden AMI concept
- Be interview-ready
π§ FIRST: What is an AMI? (Simple Words)
An AMI (Amazon Machine Image) is:
-
A template for EC2
-
Contains:
- OS
- Installed software
- Configurations
-
Used to launch multiple identical EC2s
Think like:
AMI = Clone of your configured server π§¬
π§ GOLDEN AMI (VERY IMPORTANT)
A Golden AMI:
- Has OS patches
- Has required software
- Has security hardening
- Used across environments
π Used by:
- Auto Scaling
- Dev / QA / Prod
πΉ STEP 1: Prepare Your EC2 for AMI
Weβll use:
ec2-hands-on-1
Confirm:
- Apache installed
- Website working
- EBS attached
(Optional check):
systemctl status httpd
πΉ STEP 2: Create AMI from EC2
- EC2 Console β Instances
- Select
ec2-hands-on-1 - Actions β Image and templates
- Click Create image
Fill Details:
- Image name:
ec2-golden-ami-v1
- Description:
Apache + custom configuration
- Leave defaults (Reboot = YES)
Click Create image
π AWS creates:
- AMI
- Snapshots of attached EBS volumes
πΉ STEP 3: Track AMI Creation
- Go to AMIs
- Status β Pending β Available
π Wait until Available
πΉ STEP 4: Launch New EC2 from Custom AMI
- Select AMI
ec2-golden-ami-v1 - Click Launch instance from AMI
Configure:
- Instance name:
ec2-from-golden-ami
- Instance type β t2.micro
- Key pair β existing
- Security group β allow HTTP + SSH
- Launch
πΉ STEP 5: Test NEW EC2
- Copy Public IP
- Open browser:
http://PUBLIC_IP
π Website should load immediately β No installation needed
πΉ STEP 6: PROOF (Optional SSH)
SSH into new EC2:
ssh -i ec2-key-hands-on.pem ec2-user@NEW_PUBLIC_IP
Check:
systemctl status httpd
Result:
active (running)
π§ REAL-WORLD USE CASES
- β Auto Scaling Groups
- β Faster deployments
- β Consistent environments
- β Rollback strategy
π§ INTERVIEW-READY ANSWER
An AMI is a pre-configured template containing the operating system, applications, and settings required to launch EC2 instances. A Golden AMI ensures consistency and faster provisioning across environments.
β οΈ IMPORTANT BEST PRACTICES
- β Version your AMIs
- β Patch before AMI creation
- β Delete unused AMIs
- β Tag AMIs properly
β LAB 13 TASK CHECKLIST
- β Created custom AMI
- β Launched EC2 from AMI
- β Website worked instantly
- β Understood Golden AMI concept
π§ͺ EC2 HANDS-ON β LAB 14
π Root Volume vs Additional EBS Volumes (Deep Practical Understanding)

π― Objective of LAB 14
By the end of this lab, you will:
- Clearly understand root volume
- Understand additional EBS volumes
- See Delete on Termination in action
- Know real-world best practices
- Be interview-ready
π§ FIRST: What is Root Volume? (Simple Words)
The root volume:
- Contains the OS (Amazon Linux)
- Is attached at launch
- Usually named:
/dev/xvda
Think like:
Root volume = Laptop C: drive π»
Without root volume β EC2 cannot boot
π§ What is Additional EBS Volume?
Additional EBS:
-
Extra disks you attach later
-
Used for:
- App data
- Logs
- Databases
-
Examples:
/dev/xvdf
/dev/xvdg
Think like:
Additional EBS = External hard disk πΎ
πΉ STEP 1: Identify Volumes on Your EC2
SSH into EC2:
ssh -i ec2-key-hands-on.pem ec2-user@ELASTIC_IP
Run:
lsblk
You will see:
xvdaβ Root volumexvdfβ Additional EBSxvdgβ Snapshot-restored EBS
πΉ STEP 2: Check Mount Points
df -h
You will see:
/β Root volume/dataβ Additional EBS/restoreβ Snapshot EBS
π Root volume mounts at /
πΉ STEP 3: Check βDelete on Terminationβ (IMPORTANT)
- EC2 Console β Instances
- Select
ec2-hands-on-1 - Go to Storage tab
- Click Volume ID of root volume
Look for:
Delete on termination: Yes
π Meaning:
- If EC2 terminates
- Root volume is deleted
πΉ STEP 4: Check Additional EBS Delete Behavior
Click additional EBS volume:
Youβll see:
Delete on termination: No
π Meaning:
- Data survives EC2 termination
- Used for backups & safety
πΉ STEP 5: PRACTICAL SCENARIO (CONCEPT)
If EC2 is TERMINATED:
| Volume Type | Result |
|---|---|
| Root volume | β Deleted |
| Additional EBS | β Preserved |
π Thatβs why:
- App data should be on separate EBS
- OS only on root
πΉ STEP 6: BEST PRACTICE (REAL WORLD)
- β Keep OS on root volume
- β Keep data/logs on separate EBS
- β Disable delete-on-termination for critical volumes
- β Take snapshots regularly
π§ INTERVIEW-READY ANSWER
The root volume contains the operating system and is required for booting an EC2 instance, while additional EBS volumes are used for application data. By default, the root volume is deleted on termination, whereas additional volumes persist unless explicitly configured otherwise.
β οΈ COMMON MISTAKES
- β Storing critical data on root volume
- β Not checking delete-on-termination
- β Forgetting to back up additional volumes
β LAB 14 TASK CHECKLIST
- β Identified root vs additional volumes
- β Checked mount points
- β Understood delete-on-termination
- β Learned real-world design practice
π§ͺ EC2 HANDS-ON β LAB 15
π EBS vs Instance Store (Performance vs Persistence)
π― Objective of LAB 15
By the end of this lab, you will:
- Understand Instance Store
- Compare EBS vs Instance Store
- Know when to use each
- Be interview-ready
π§ FIRST: What is Instance Store?
Instance Store is:
-
Temporary (ephemeral) storage
-
Physically attached to EC2 host
-
Data is lost if:
- Instance stops
- Instance terminates
- Host fails
Think like:
Instance Store = RAM disk / temporary scratch space β‘
π§ What is EBS? (Reminder)
EBS:
- Persistent block storage
- Independent of EC2 lifecycle
- Backed by AWS-managed infrastructure
Think like:
EBS = External hard disk πΎ
πΉ HANDS-ON NOTE (IMPORTANT)
β οΈ Free Tier usually does NOT support Instance Store
So weβll do:
- Conceptual + console visibility
- Real-world explanation (interview focus)
πΉ STEP 1: Identify Instance Store Support
-
EC2 β Launch instance
-
Select an instance type like:
i3,i4,d2
Youβll see:
Instance store volumes available
π These instance types support instance store
πΉ STEP 2: Understand Storage at Launch
When instance store is used:
- Disk appears as:
/dev/nvme1n1
- Must be formatted manually
- Not persistent
πΉ STEP 3: EBS vs Instance Store (INTERVIEW TABLE)
| Feature | EBS | Instance Store |
|---|---|---|
| Persistence | β Yes | β No |
| Data survives stop | β Yes | β No |
| Snapshot | β Yes | β No |
| Performance | Good | Very High |
| Cost | Paid | Included |
| Use case | Databases | Cache / temp |
πΉ STEP 4: REAL-WORLD USE CASES
Use EBS when:
- β Databases
- β App data
- β Logs
- β Backups
Use Instance Store when:
- β Cache
- β Buffer
- β Temporary data
- β High-speed scratch space
π§ INTERVIEW-READY ANSWER
Amazon EBS provides persistent block storage independent of the EC2 lifecycle, whereas Instance Store offers high-performance, ephemeral storage that is lost when the instance stops or terminates.
β οΈ COMMON MISTAKES
- β Using instance store for databases
- β Expecting data persistence
- β No backups
β LAB 15 TASK CHECKLIST
- β Understood instance store
- β Compared with EBS
- β Learned performance trade-offs
- β Interview clarity achieved
π§ͺ EC2 HANDS-ON β LAB 16
π Multi-AZ EC2 Deployment (High Availability Basics)

π― Objective of LAB 16
By the end of this lab, you will:
- Understand Availability Zones (AZs) deeply
- Launch EC2 instances in multiple AZs
- Prove why Multi-AZ is needed
- Be able to explain HA architecture in interviews
π§ FIRST: What is an Availability Zone? (Very Simple)
An Availability Zone (AZ) is:
- One or more physically separate data centers
- Inside a single AWS region
- Connected with high-speed private network
Example (Mumbai region):
ap-south-1a
ap-south-1b
ap-south-1c
Think like:
Region = City ποΈ AZ = Different buildings in the city π’π’π’
π§ Why Multi-AZ is IMPORTANT?
If you use only ONE AZ:
- Power failure β
- Network issue β
- Fire / flood β β‘οΈ Your application goes DOWN
If you use MULTI-AZ:
- One AZ fails β
- Other AZ still works β β‘οΈ Application stays UP
π This is called High Availability
πΉ STEP 1: Check AZ of Your Existing EC2
- EC2 β Instances
- Select
ec2-hands-on-1 - Check:
Availability Zone
Example:
ap-south-1a
π Right now β Single-AZ architecture
πΉ STEP 2: Launch SECOND EC2 in DIFFERENT AZ
We will launch another EC2, but in a different AZ.
Launch Instance:
- Name:
ec2-hands-on-az2
-
AMI: Amazon Linux 2023
-
Instance type: t2.micro
-
Key pair: existing
-
Security group:
- SSH (22) β My IP
- HTTP (80) β Anywhere
π₯ IMPORTANT (AZ SELECTION)
In Network settings β Subnet:
- Select a subnet from a different AZ
Example:
- First EC2 β
ap-south-1a - Second EC2 β
ap-south-1b
π This step is CRITICAL
πΉ STEP 3: Launch & Verify
Wait until: π’ Instance state β Running
Now you have:
- EC2 in AZ-A
- EC2 in AZ-B
β Multi-AZ setup achieved
πΉ STEP 4: Install Apache on SECOND EC2
SSH into second EC2:
ssh -i ec2-key-hands-on.pem ec2-user@SECOND_PUBLIC_IP
Install Apache:
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
Create page:
echo "<h1>Hello from AZ-2 EC2</h1>" | sudo tee /var/www/html/index.html
πΉ STEP 5: Test Both EC2s
Browser tests:
EC2-1 (AZ-A):
http://IP-OF-EC2-1
EC2-2 (AZ-B):
http://IP-OF-EC2-2
β Both websites work independently
πΉ STEP 6: FAILURE SCENARIO (CONCEPTUAL)
Imagine:
- AZ-A goes DOWN β
What happens?
| Setup | Result |
|---|---|
| Single-AZ | App DOWN β |
| Multi-AZ | App STILL UP β |
π This is why AWS recommends Multi-AZ
π§ VERY IMPORTANT INTERVIEW POINTS
β Multi-AZ β Auto Scaling β Multi-AZ β Load Balancer β Multi-AZ = Design choice β HA requires more than one AZ
π§ INTERVIEW-READY ANSWER
A Multi-AZ architecture deploys EC2 instances across multiple Availability Zones within a region to ensure high availability and fault tolerance. If one AZ fails, traffic can be served from another AZ.
β οΈ COMMON MISTAKES
β Launching both EC2s in same AZ β Assuming region = AZ β Thinking Multi-AZ happens automatically
β LAB 16 TASK CHECKLIST
β Identified AZ of EC2 β Launched second EC2 in different AZ β Installed Apache on second EC2 β Tested both independently β Understood HA concept
π§ͺ EC2 HANDS-ON β LAB 17
π Application Load Balancer (ALB) β Distribute Traffic Across AZs

π― Objective of LAB 17
By the end of this lab, you will:
- Create an Application Load Balancer
- Route traffic to multiple EC2s
- Understand Target Groups & Health Checks
- Achieve true High Availability
- Be 100% interview-ready
π§ FIRST: What is a Load Balancer? (Simple Words)
A Load Balancer:
- Receives traffic from users
- Distributes it across multiple EC2 instances
- Prevents overload & downtime
Think like:
Load Balancer = Traffic police π¦
π§ What is ALB?
Application Load Balancer (ALB):
-
Works at Layer 7 (HTTP/HTTPS)
-
Routes based on:
- URL path
- Host name
-
Supports Auto Scaling
ALB is part of Amazon Web Services β Elastic Load Balancing
π§ ARCHITECTURE YOU ARE BUILDING
User
β
Application Load Balancer
β β
EC2 (AZ-A) EC2 (AZ-B)
πΉ STEP 1: Create Target Group
Target Group = Group of EC2s ALB sends traffic to
- AWS Console β EC2
- Left menu β Target Groups
- Click Create target group
Configure:
- Target type β Instances
- Name:
tg-ec2-hands-on
- Protocol β HTTP
- Port β 80
- VPC β your VPC
- Health check path:
/
Click Next
πΉ STEP 2: Register EC2 Instances
-
Select BOTH EC2 instances:
ec2-hands-on-1ec2-hands-on-az2
-
Click Include as pending
-
Click Create target group
πΉ STEP 3: Create Application Load Balancer
- EC2 β Load Balancers
- Click Create Load Balancer
- Select Application Load Balancer
Basic Configuration
- Name:
alb-ec2-hands-on
- Scheme β Internet-facing
- IP address type β IPv4
| Feature | Internet-facing | Internal |
|---|---|---|
| Accessible from internet | β Yes | β No |
| IP type | Public IPs | Private IPs |
| Subnet type | Public subnet | Private subnet |
| Common use | Websites, APIs | Backend services |
| DNS resolves to | Public IPs | Private IPs |
Network Mapping (IMPORTANT)
-
VPC β your VPC
-
Select at least 2 AZs
- ap-south-1a
- ap-south-1b
π ALB must be Multi-AZ
Security Group
-
Create new SG:
- HTTP (80) β Anywhere
-
Name:
alb-sg
Listener & Routing
- Listener β HTTP : 80
- Default action β Forward to:
tg-ec2-hands-on
Click Create load balancer
πΉ STEP 4: Wait for ALB to Become Active
Status:
Provisioning β Active
Copy:
DNS name
Example:
alb-ec2-hands-on-123.ap-south-1.elb.amazonaws.com
πΉ STEP 5: Test Load Balancer (FUN PART π)
Open browser:
http://ALB_DNS_NAME
Refresh multiple times π
You should see:
- Sometimes: Hello from AZ-1
- Sometimes: Hello from AZ-2
β Traffic is distributed
πΉ STEP 6: Health Check Verification
- EC2 β Target Groups
- Select
tg-ec2-hands-on - Check Targets
Both instances should be:
Healthy
πΉ STEP 7: FAILURE TEST (VERY IMPORTANT)
Stop ONE EC2 instance:
- Stop
ec2-hands-on-az2
Now refresh ALB URL again π
π’ Website STILL works π Traffic goes only to healthy EC2
π THIS IS HIGH AVAILABILITY
π§ INTERVIEW-READY ANSWER
An Application Load Balancer operates at Layer 7 and distributes HTTP/HTTPS traffic across multiple targets in different Availability Zones. It uses health checks to route traffic only to healthy instances, ensuring high availability.
π§ VERY IMPORTANT INTERVIEW POINTS
β ALB is regional β ALB is Multi-AZ by default β ALB uses Target Groups β ALB performs health checks β ALB does NOT host applications
β οΈ COMMON MISTAKES
β Not opening HTTP in EC2 SG β Not opening HTTP in ALB SG β Registering wrong instances β Using only one AZ
β LAB 17 TASK CHECKLIST
β Created Target Group β Registered EC2s β Created ALB β Verified traffic distribution β Tested failure scenario
π§ͺ EC2 HANDS-ON β LAB 18
π Target Groups & Health Checks (Deep Dive + Failure Control)

π― Objective of LAB 18
By the end of this lab, you will:
- Fully understand Target Groups
- Customize Health Checks
- See how instances become Healthy / Unhealthy
- Control traffic without stopping EC2
- Be interview + production ready
π§ FIRST: What is a Target Group? (Simple Words)
A Target Group:
-
Is a logical group of backend resources
-
ALB sends traffic only to healthy targets
-
Can contain:
- EC2 instances
- IP addresses
- Lambda (not now)
Think like:
Target Group = Team of workers π· ALB = Manager who sends work only to active workers
π§ VERY IMPORTANT CONCEPT
ALB never sends traffic directly to EC2 It sends traffic via Target Groups
π This gives:
- Flexibility
- Health control
- Scaling support
πΉ STEP 1: Open Target Group Settings
- EC2 Console β Target Groups
- Select:
tg-ec2-hands-on
- Go to Health checks tab
- Click Edit
πΉ STEP 2: Understand Health Check Settings
Default values (important):
| Setting | Meaning |
|---|---|
| Protocol | HTTP |
| Path | / |
| Port | traffic port (80) |
| Healthy threshold | 5 |
| Unhealthy threshold | 2 |
| Timeout | 5 sec |
| Interval | 30 sec |
π ALB checks:
http://EC2-IP:80/
If it fails β instance marked Unhealthy
πΉ STEP 3: CREATE A CUSTOM HEALTH CHECK PAGE (Hands-On)
SSH into EC2 in AZ-A:
ssh -i ec2-key-hands-on.pem ec2-user@EC2_A_IP
Create health page:
echo "OK" | sudo tee /var/www/html/health
Repeat same on EC2 in AZ-B.
πΉ STEP 4: Update Target Group Health Check Path
Back to Target Group β Health checks β Edit
Change:
Path: /health
Save changes
β³ Wait ~1 minute
πΉ STEP 5: Verify Health Status
Go to: Target Groups β Targets
You should see:
Healthy
π Health is now checked via /health
πΉ STEP 6: BREAK ONE INSTANCE (NO STOP)
SSH into EC2-A:
sudo rm /var/www/html/health
πΉ STEP 7: Observe ALB Behavior (CRITICAL)
Wait ~1 minute
Now check: Target Groups β Targets
EC2-A status:
Unhealthy
EC2-B:
Healthy
πΉ STEP 8: Test in Browser
Open:
http://ALB_DNS_NAME
π’ Website STILL works π Traffic goes only to healthy EC2
π This is zero-downtime protection
πΉ STEP 9: FIX THE INSTANCE
On EC2-A:
echo "OK" | sudo tee /var/www/html/health
Wait ~1 minute
Status becomes:
Healthy
Traffic resumes automatically
π§ VERY IMPORTANT INTERVIEW POINTS
β Health checks prevent bad traffic β EC2 can be running but unhealthy β ALB removes unhealthy targets automatically β No manual intervention needed
π§ INTERVIEW-READY ANSWER
Target Groups define the backend resources for a load balancer, and health checks continuously monitor the availability of each target. The load balancer routes traffic only to healthy targets, ensuring fault tolerance and zero downtime.
β οΈ COMMON MISTAKES
β Wrong health check path β App returns 404 / 500 β Health check port blocked in SG β Assuming βrunningβ means βhealthyβ
β LAB 18 TASK CHECKLIST
β Understood Target Groups β Created custom health endpoint β Modified health check path β Forced unhealthy state β Observed ALB traffic control
π§ͺ EC2 HANDS-ON β LAB 19
π Auto Scaling Group (ASG) β Automatically Scale EC2

π― Objective of LAB 19
By the end of this lab, you will:
- Understand what Auto Scaling is
- Create an Auto Scaling Group
- Automatically add/remove EC2 instances
- Attach ASG to Application Load Balancer
- Be 100% interview + real-world ready
π§ FIRST: What is Auto Scaling? (Very Simple)
Auto Scaling:
-
Automatically launches EC2 instances
-
Automatically terminates EC2 instances
-
Based on:
- Load (CPU)
- Health
- Capacity rules
Think like:
Auto Scaling = Smart system that hires & fires workers automatically π€
π§ WHY Auto Scaling is IMPORTANT?
Without Auto Scaling:
- High traffic β app crashes β
- Low traffic β money wasted β
With Auto Scaling:
- High traffic β more EC2s β
- Low traffic β fewer EC2s β
π Performance + Cost optimization
π§ ARCHITECTURE WE ARE BUILDING
Users
β
Application Load Balancer
β
Auto Scaling Group
β β β
EC2 (AZ-A) EC2 (AZ-B) EC2 (AZ-C)
πΉ STEP 1: Create Launch Template (MOST IMPORTANT)
Auto Scaling uses a Launch Template to create EC2s.
Go to:
EC2 β Launch Templates β Create launch template
Configure Launch Template
Launch template name
lt-ec2-hands-on
AMI
- Select your custom AMI:
ec2-golden-ami-v1
π This ensures:
- Apache already installed
- Website ready instantly
Instance type
t2.micro
Key pair
ec2-key-hands-on
Network settings
-
Do NOT select subnet here
-
Security group:
- SSH (22) β My IP
- HTTP (80) β ALB SG (recommended)
π EC2 should accept traffic only from ALB
Advanced details
- Leave empty (no user data now)
Click Create launch template
πΉ STEP 2: Create Auto Scaling Group
- Go to Auto Scaling Groups
- Click Create Auto Scaling group
Basic Settings
- Name:
asg-ec2-hands-on
- Launch template:
lt-ec2-hands-on
Click Next
Network Settings (VERY IMPORTANT)
-
VPC β your VPC
-
Subnets β select at least 2 AZs
- ap-south-1a
- ap-south-1b
π This makes ASG Multi-AZ
Attach Load Balancer
- Select:
Attach to an existing load balancer
- Choose:
Application Load Balancer
- Select target group:
tg-ec2-hands-on
Click Next
πΉ STEP 3: Configure Group Size
Set:
- Desired capacity β 2
- Minimum capacity β 1
- Maximum capacity β 3
π Meaning:
- At least 1 EC2 always running
- Normally 2 EC2s
- Max 3 during traffic spike
πΉ STEP 4: Configure Scaling Policy
Choose:
Target tracking scaling policy
Metric:
Average CPU utilization
Target value:
50%
π If CPU > 50% β scale OUT π If CPU < 50% β scale IN
Click Next β Create Auto Scaling group
πΉ STEP 5: VERIFY ASG CREATION
Go to:
Auto Scaling Groups β asg-ec2-hands-on
You should see:
- 2 EC2 instances launching automatically
π You did NOT manually create them!
πΉ STEP 6: VERIFY ALB INTEGRATION
-
Go to Target Groups
-
Open
tg-ec2-hands-on -
Targets β Youβll see:
- ASG-created EC2s
- Status = Healthy
πΉ STEP 7: TEST AUTO SCALING (CONCEPTUAL + OPTIONAL)
To simulate load:
sudo yum install stress -y
stress --cpu 2
π CPU goes high β ASG launches new EC2
(We can skip actual stress to save free tier)
you can also stop one EC2 to see ASG replace it. for that just terminate one of the ASG EC2s from console.
π§ VERY IMPORTANT INTERVIEW POINTS
β ASG uses Launch Template β ASG is Multi-AZ β ASG replaces unhealthy EC2s β ASG works with ALB β ASG = High Availability + Scalability
π§ INTERVIEW-READY ANSWER
An Auto Scaling Group automatically adjusts the number of EC2 instances based on demand and health checks. It ensures high availability, fault tolerance, and cost efficiency by scaling out during high load and scaling in during low usage.
β οΈ COMMON MISTAKES
β Using default AMI instead of Golden AMI β Single AZ ASG β Wrong security group (ALB traffic blocked) β No scaling policy
β LAB 19 TASK CHECKLIST
β Created Launch Template β Created Auto Scaling Group β Multi-AZ configuration β Integrated with ALB β Understood scaling logic
π§ͺ EC2 HANDS-ON β LAB 20
π High Availability EC2 Architecture (End-to-End Design)

π― Objective of LAB 20
By the end of this lab, you will:
- Understand complete HA EC2 architecture
- Know how each component works together
- Be able to design & explain production systems
- Be interview + real-world ready
π§ COMPLETE ARCHITECTURE (WHAT YOU BUILT)
Users
β
Route 53 (DNS)
β
Application Load Balancer (Multi-AZ)
β
Auto Scaling Group
β β β
EC2 (AZ-A) EC2 (AZ-B) EC2 (AZ-C)
β
EBS (Persistent Storage)
π§ COMPONENT-BY-COMPONENT BREAKDOWN
πΉ DNS β Amazon Route 53
- Maps domain β ALB
- Health checks (optional)
- Highly available globally
πΉ Application Load Balancer
- Internet-facing
- Distributes traffic
- Health checks targets
- Multi-AZ by default
πΉ Auto Scaling Group
- Maintains desired EC2 count
- Scales in/out automatically
- Replaces unhealthy EC2s
- Uses Launch Template + Golden AMI
πΉ EC2 Instances
- Stateless compute layer
- No data stored locally
- Created automatically
πΉ EBS
- Persistent storage
- Independent of EC2 lifecycle
- Used for logs / app data
πΉ FAILURE SCENARIOS (CRITICAL THINKING)
Scenario 1: EC2 Failure β
- ASG launches new EC2 automatically
- ALB routes traffic to healthy targets
Scenario 2: AZ Failure β
- ALB routes traffic to other AZs
- ASG launches instances in healthy AZs
Scenario 3: Traffic Spike π
- CPU increases
- ASG scales out automatically
- Performance maintained
Scenario 4: Traffic Drop π
- ASG scales in
- Cost optimized
π§ WHY THIS ARCHITECTURE IS PRODUCTION-READY
β No single point of failure β Horizontally scalable β Cost efficient β Secure & resilient β Fully automated
π§ INTERVIEW DESIGN ANSWER (GOLD)
I would design a highly available EC2 architecture using Route 53 for DNS, an internet-facing Application Load Balancer, Auto Scaling Groups across multiple Availability Zones, and EC2 instances launched from a Golden AMI. This ensures fault tolerance, scalability, and zero downtime.
π§ COMMON INTERVIEW FOLLOW-UP QUESTIONS
| Question | Answer |
|---|---|
| Single EC2 down? | ASG replaces |
| AZ down? | ALB routes elsewhere |
| Scale during traffic? | ASG |
| Static IP needed? | ALB DNS / Route 53 |
| Data persistence? | EBS / RDS |
π PHASE COMPLETED π
You have completed: β Core EC2 β Storage β Networking β Load Balancing β Auto Scaling β High Availability
π§ͺ EC2 HANDS-ON β LAB 21
π Amazon CloudWatch (Monitoring, Metrics & Alarms)


π― Objective of LAB 21
By the end of this lab, you will:
- Understand what CloudWatch is
- Monitor EC2 metrics (CPU, Network, Disk)
- Create a CloudWatch Alarm
- Trigger an alarm practically
- Be interview + production ready
π§ FIRST: What is CloudWatch? (Very Simple)
CloudWatch is:
- AWS monitoring service
- Collects metrics, logs, events
- Helps you see & react to problems
Think like:
CloudWatch = CCTV + Health monitor for AWS resources π
π§ What CloudWatch Does for EC2
CloudWatch can: β Monitor CPU usage β Monitor network traffic β Trigger alarms β Help Auto Scaling β Reduce downtime
πΉ STEP 1: Open CloudWatch
- AWS Console
- Search CloudWatch
- Open CloudWatch Dashboard
πΉ STEP 2: View EC2 Metrics
- Left menu β Metrics
- Click EC2
- Click Per-Instance Metrics
- Select one EC2 from ASG or
ec2-hands-on-1
Youβll see metrics like:
- CPUUtilization
- NetworkIn
- NetworkOut
- DiskReadOps
π These are real-time performance data
πΉ STEP 3: View CPU Utilization Graph
- Select CPUUtilization
- Click Graphed metrics
Youβll see:
- Time on X-axis
- CPU % on Y-axis
π This is what ASG uses for scaling
πΉ STEP 4: Create a CloudWatch Alarm (IMPORTANT)
Now we create an alarm on CPU usage.
- Select CPUUtilization
- Click Create alarm
Alarm Configuration
Metric
- CPUUtilization
Condition
- Threshold type β Static
- Whenever CPUUtilization Greater than
70%
Notification
- Alarm state trigger β In alarm
π For now:
- Choose Create new SNS topic
- Email β your email
- Confirm subscription from email (IMPORTANT)
Alarm Name
ec2-high-cpu-alarm
Click Create alarm
πΉ STEP 5: Verify Alarm Status
Go to: CloudWatch β Alarms
Status initially:
OK
π Alarm watches EC2 continuously
πΉ STEP 6: Trigger Alarm (Hands-On)
SSH into EC2:
ssh -i ec2-key-hands-on.pem ec2-user@EC2_PUBLIC_IP
Install stress tool:
sudo yum install stress -y
Run stress:
stress --cpu 2
π This increases CPU usage
πΉ STEP 7: Observe Alarm State Change
Wait ~1β2 minutes
Alarm status changes:
OK β ALARM
π§ You receive email alert
π Monitoring + alerting works
πΉ STEP 8: Stop Stress Test
Press:
CTRL + C
After few minutes:
ALARM β OK
π§ VERY IMPORTANT INTERVIEW POINTS
β CloudWatch metrics are automatic β Alarms trigger actions or notifications β ASG uses CloudWatch metrics β Default EC2 metrics are every 5 minutes β Detailed monitoring = 1 minute
π§ INTERVIEW-READY ANSWER
Amazon CloudWatch is a monitoring service that collects metrics, logs, and events from AWS resources. It enables real-time visibility, alarm-based notifications, and automated actions such as Auto Scaling.
β οΈ COMMON MISTAKES
β Forgetting to confirm SNS email β Expecting disk usage without agent β Setting wrong threshold β Monitoring wrong instance
β LAB 21 TASK CHECKLIST
β Viewed EC2 metrics β Understood CPU graph β Created CloudWatch alarm β Triggered alarm practically β Received notification
π§ͺ EC2 HANDS-ON β LAB 22
π CloudWatch Alarms + Auto Scaling (Automatic Scale Out & In)

π― Objective of LAB 22
By the end of this lab, you will:
- Connect CloudWatch β Auto Scaling
- Create scale-out & scale-in alarms
- See EC2 instances launch automatically
- Understand how AWS self-heals
- Be interview + real-world ready
π§ FIRST: How This Works (Simple Flow)
High CPU
β
CloudWatch Alarm
β
Auto Scaling Policy
β
New EC2 launched
And reverse for scale-in.
Think like:
CloudWatch = Sensor π Auto Scaling = Action π€
π§ IMPORTANT CONCEPT
- CloudWatch detects
- Auto Scaling reacts
- You do nothing manually
πΉ STEP 1: Open Your Auto Scaling Group
- EC2 Console β Auto Scaling Groups
- Select:
asg-ec2-hands-on
- Open Automatic scaling tab
You will see:
- Existing Target tracking policy (CPU 50%)
π Target tracking already uses CloudWatch internally Now weβll explicitly see alarm behavior
πΉ STEP 2: View Auto-Created CloudWatch Alarms
- Open CloudWatch
- Go to Alarms
- Youβll see alarms like:
TargetTracking-asg-ec2-hands-on-High-CPU
TargetTracking-asg-ec2-hands-on-Low-CPU
π These were created automatically by ASG
πΉ STEP 3: Understand These Alarms
High CPU Alarm
- Trigger: CPU > 50%
- Action: Scale OUT
- Adds EC2 instance
Low CPU Alarm
- Trigger: CPU < 50%
- Action: Scale IN
- Removes EC2 instance
π This is closed-loop automation
πΉ STEP 4: Observe Current Capacity
Go back to ASG β Details
Check:
- Desired: 2
- Min: 1
- Max: 3
π Currently running EC2 = 2
πΉ STEP 5: Trigger SCALE OUT (Hands-On)
SSH into one ASG EC2 (any):
ssh -i ec2-key-hands-on.pem ec2-user@ASG_EC2_PUBLIC_IP
Install stress:
sudo yum install stress -y
Run:
stress --cpu 2
πΉ STEP 6: Observe Scaling Activity (IMPORTANT)
- ASG β Activity
- Youβll see:
Launching a new EC2 instance
Wait 2β5 minutes
Now:
- EC2 count becomes 3
- New EC2 automatically registered in Target Group
- ALB sends traffic to it
π AUTO SCALE OUT SUCCESS
πΉ STEP 7: Verify via Target Group
- EC2 β Target Groups
- Open
tg-ec2-hands-on - Targets β Youβll see 3 healthy instances
πΉ STEP 8: Trigger SCALE IN
Stop stress:
CTRL + C
Wait few minutes
ASG activity:
Terminating EC2 instance
Now EC2 count β 2
π Scale-in never goes below minimum capacity
π§ VERY IMPORTANT INTERVIEW POINTS
β Auto Scaling uses CloudWatch metrics β Alarms can trigger scale out/in β Scaling is automatic & continuous β No human action required β Prevents over-provisioning
π§ INTERVIEW-READY ANSWER
CloudWatch alarms monitor EC2 metrics such as CPU utilization and trigger Auto Scaling policies to automatically scale out or scale in instances, ensuring performance and cost efficiency.
β οΈ COMMON MISTAKES
β Min capacity = 0 (downtime risk) β Wrong metric selection β Expecting instant scaling (itβs gradual) β Forgetting cooldown periods
β LAB 22 TASK CHECKLIST
β Viewed ASG-created alarms β Triggered scale-out β Observed new EC2 launch β Verified via Target Group β Observed scale-in
π§ͺ EC2 HANDS-ON β LAB 23
π EC2 Logs & Troubleshooting (CloudWatch Logs + Real Failures)

π― Objective of LAB 23
By the end of this lab, you will:
- Understand where EC2 logs live
- Send EC2 logs to CloudWatch
- Debug real website failures
- Know production troubleshooting flow
- Be interview + on-call ready
π§ FIRST: Why Logs Are CRITICAL
Metrics tell you something is wrong Logs tell you WHY it is wrong
Think like:
Metrics = Fever π‘οΈ Logs = Doctor report π©Ί
π§ TYPES OF LOGS YOU MUST KNOW
1οΈβ£ System Logs
- OS boot issues
- Kernel problems
2οΈβ£ Application Logs
- Apache / Nginx
- App errors
3οΈβ£ Cloud Logs
- Centralized in CloudWatch
- Used for monitoring & alerting
πΉ STEP 1: Check EC2 SYSTEM LOG (Console Level)
- EC2 β Instances
- Select any EC2
- Actions β Monitor and troubleshoot
- Click Get system log
π Shows:
- Boot messages
- Startup failures
β Used when EC2 wonβt start
πΉ STEP 2: Apache Logs (INSIDE EC2)
SSH into EC2:
ssh -i ec2-key-hands-on.pem ec2-user@EC2_PUBLIC_IP
Apache log locations:
/var/log/httpd/access_log
/var/log/httpd/error_log
View logs:
sudo tail -f /var/log/httpd/access_log
π Shows:
- Requests coming to server
πΉ STEP 3: Simulate an ERROR (Hands-On)
Break Apache config (safe test):
sudo chmod 000 /var/www/html
Now open website in browser β It will fail
Check error log:
sudo tail /var/log/httpd/error_log
Youβll see permission denied errors
π This is real troubleshooting
πΉ STEP 4: FIX the Issue
sudo chmod 755 /var/www/html
sudo systemctl restart httpd
Refresh browser β π’ Works again
π§ LESSON
β Website down β EC2 down β Logs tell exact cause β Restart without checking logs = BAD practice
πΉ STEP 5: Send EC2 Logs to CloudWatch (IMPORTANT)
Now we centralize logs using CloudWatch Agent.
Install CloudWatch Agent
sudo yum install amazon-cloudwatch-agent -y
π This agent sends logs β CloudWatch
Create Agent Config File
sudo nano /opt/aws/amazon-cloudwatch-agent/bin/config.json
Paste:
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/httpd/error_log",
"log_group_name": "ec2-apache-error-log",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
Save & exit
Start CloudWatch Agent
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a fetch-config \
-m ec2 \
-c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json \
-s
π This command:
- Reads config
- Starts agent
- Pushes logs to CloudWatch
πΉ STEP 6: View Logs in CloudWatch
- Open CloudWatch
- Go to Logs β Log groups
- Open:
ec2-apache-error-log
π Youβll see Apache logs without SSH
π§ REAL-WORLD BENEFITS
β Debug without server access β Centralized logs β Works with ASG (instances come & go) β Required for compliance & audits
π§ INTERVIEW-READY ANSWER
EC2 troubleshooting involves analyzing system logs, application logs, and CloudWatch Logs. Centralizing logs in CloudWatch enables faster debugging, monitoring, and troubleshooting across scalable environments.
β οΈ COMMON MISTAKES
β Restarting services blindly β Not checking error logs β No centralized logging β SSH-only debugging
β LAB 23 TASK CHECKLIST
β Viewed system logs β Checked Apache logs β Simulated real error β Fixed issue using logs β Sent logs to CloudWatch
π§ͺ EC2 HANDS-ON β LAB 24
π IAM Roles for EC2 (Secure AWS Access WITHOUT Access Keys)

π― Objective of LAB 24
By the end of this lab, you will:
- Understand why access keys are dangerous
- Create an IAM Role
- Attach the role to an EC2 instance
- Access AWS services without access keys
- Be 100% interview + production ready
π§ FIRST: Why NOT Use Access Keys on EC2?
If you put Access Key + Secret Key inside EC2: β Keys can be stolen β Keys can be leaked in GitHub β Manual rotation required β Security risk
π AWS Best Practice: β Use IAM Roles
π§ What is an IAM Role?
An IAM Role:
- Is an identity for AWS services
- Provides temporary credentials
- Automatically rotated by AWS
- Attached directly to EC2
Think like:
IAM Role = Temporary ID card π« EC2 uses it automatically
πΉ IAM SERVICE (Important to Know)
IAM belongs to Amazon Web Services
IAM = Identity and Access Management
π§ WHAT WE WILL DO IN THIS LAB
We will: 1οΈβ£ Create IAM Role 2οΈβ£ Attach S3 read access 3οΈβ£ Attach role to EC2 4οΈβ£ Access S3 without keys
πΉ STEP 1: Create IAM Role
- AWS Console β Search IAM
- Click Roles
- Click Create role
Trusted Entity
- Select AWS service
- Use case β EC2
- Click Next
π This means: EC2 is allowed to assume this role
πΉ STEP 2: Attach Permission Policy
Search & select:
AmazonS3ReadOnlyAccess
Click Next
π This policy allows:
- List buckets
- Read objects
- NO delete access
πΉ STEP 3: Name the Role
Role name:
ec2-s3-read-role
Click Create role
π IAM Role created
πΉ STEP 4: Attach IAM Role to EC2
- EC2 β Instances
- Select:
ec2-hands-on-1
- Actions β Security
- Click Modify IAM role
- Select:
ec2-s3-read-role
- Save
π Role attached without reboot
πΉ STEP 5: VERIFY ACCESS (NO ACCESS KEYS)
SSH into EC2:
ssh -i ec2-key-hands-on.pem ec2-user@ELASTIC_IP
Run:
aws s3 ls
π You should see S3 buckets list
π No access key π No secret key π Fully secure
πΉ STEP 6: PROVE THERE ARE NO KEYS
Run:
cat ~/.aws/credentials
Output:
No such file or directory
β Proof: IAM Role is working
π§ HOW THIS WORKS INTERNALLY (INTERVIEW GOLD)
- EC2 requests credentials from Metadata Service
- IAM provides temporary credentials
- Credentials auto-expire & rotate
π§ INTERVIEW-READY ANSWER
IAM Roles provide secure, temporary credentials to EC2 instances, eliminating the need for hard-coded access keys. AWS automatically rotates these credentials, making IAM Roles the recommended and secure way to grant permissions to EC2.
β οΈ COMMON MISTAKES
β Using access keys on EC2 β Over-permissioned roles β One role for everything β Not rotating credentials (keys)
β LAB 24 TASK CHECKLIST
β Created IAM Role β Attached S3 Read policy β Attached role to EC2 β Accessed S3 without keys β Understood security flow
π§ͺ EC2 HANDS-ON β LAB 25
π EC2 Session Manager (Login WITHOUT SSH & Key Pair)

π― Objective of LAB 25
By the end of this lab, you will:
- Understand what Session Manager is
- Access EC2 without SSH
- Remove dependency on key pairs
- Learn enterprise-grade secure access
- Be interview + production ready
π§ FIRST: What is Session Manager?
Session Manager is part of AWS Systems Manager
It allows you to: β Connect to EC2 via AWS Console β No SSH (port 22 not needed) β No key pair β Fully logged & auditable
Think like:
Session Manager = Secure remote terminal via AWS π
π§ WHY Session Manager is BETTER than SSH?
| SSH | Session Manager |
|---|---|
| Needs port 22 | β No ports needed |
| Needs key pair | β No keys |
| Hard to audit | β Fully logged |
| Security risk | β Very secure |
π AWS RECOMMENDS Session Manager
π§ PREREQUISITES (VERY IMPORTANT)
To use Session Manager, EC2 must have:
β SSM Agent installed β IAM Role attached with SSM permissions β Internet access (or VPC endpoints)
Good news π Amazon Linux already has SSM Agent installed
πΉ STEP 1: Attach SSM IAM Role to EC2
Weβll extend the role you already used.
Open IAM Role
- IAM β Roles
- Open:
ec2-s3-read-role
Attach SSM Policy
- Click Add permissions
- Attach policy:
AmazonSSMManagedInstanceCore
- Save
π This policy allows:
- Session Manager
- Run Command
- Patch Manager
πΉ STEP 2: Verify Role Attached to EC2
- EC2 β Instances
- Select:
ec2-hands-on-1
- Security tab β IAM role
It should show:
ec2-s3-read-role
πΉ STEP 3: Open Session Manager
- EC2 Console β Instances
- Select your instance
- Click Connect
- Choose Session Manager
- Click Connect
π YOU ARE LOGGED IN
No: β SSH β Key pair β Port 22
πΉ STEP 4: Verify Inside EC2
Inside Session Manager terminal, run:
whoami
Output:
ssm-user
π This user is created dynamically by SSM
Check OS:
uname -a
πΉ STEP 5: PROVE SSH is NOT Needed (Concept)
You can:
- Remove SSH rule from Security Group
- EC2 still accessible via Session Manager
π This is zero-attack-surface access
(Do NOT remove now β concept only)
π§ HOW SESSION MANAGER WORKS (INTERVIEW GOLD)
- EC2 runs SSM Agent
- Agent talks to Systems Manager endpoint
- IAM Role authorizes access
- AWS Console opens secure tunnel
π No inbound traffic needed
π§ INTERVIEW-READY ANSWER
EC2 Session Manager, part of AWS Systems Manager, provides secure shell access to instances without opening inbound ports or using SSH keys. It uses IAM roles and the SSM Agent, offering improved security and full auditing.
β οΈ COMMON MISTAKES
β No IAM role attached β Missing SSM policy β SSM Agent stopped β No internet/VPC endpoint
β LAB 25 TASK CHECKLIST
β Added SSM policy β Connected via Session Manager β Logged in without SSH β Understood secure access model
π SECURITY ACCESS PHASE COMPLETED π
You now know: β SSH access β Key pairs β IAM Roles β Session Manager (BEST PRACTICE)
π§ͺ EC2 HANDS-ON β LAB 26
π Private EC2 + Bastion Host (Enterprise-Grade Secure Access)




π― Objective of LAB 26
By the end of this lab, you will:
- Understand Public vs Private EC2
- Create a Private EC2 instance
- Use a Bastion Host for secure access
- Learn enterprise security design
- Be interview + production ready
π§ FIRST: What is a Private EC2?
A Private EC2:
- Has NO public IP
- Lives in a private subnet
- Cannot be accessed from the internet directly
Think like:
Private EC2 = Office server inside company network π’ Not exposed to the internet πβ
π§ What is a Bastion Host?
A Bastion Host:
- Is a public EC2
- Acts as a secure entry point
- Used to access private EC2s
Think like:
Bastion = Security gate π§ Private EC2 = Internal room π
π§ ARCHITECTURE WE ARE BUILDING
Your Laptop
β
Bastion Host (Public Subnet)
β
Private EC2 (Private Subnet)
This runs inside Amazon Web Services VPC
πΉ STEP 1: Understand Your VPC Structure
Go to:
- VPC β Subnets
Youβll see:
- Public Subnet
- Private Subnet
π Public subnet:
- Route to Internet Gateway
π Private subnet:
- NO Internet Gateway route
πΉ STEP 2: Launch BASTION HOST (Public EC2)
Launch new EC2
- Name:
bastion-host
-
AMI: Amazon Linux 2023
-
Instance type: t2.micro
-
Subnet: Public Subnet
-
Auto-assign Public IP: Enabled
-
Security Group:
- SSH (22) β My IP only
Launch instance
πΉ STEP 3: Launch PRIVATE EC2
Launch another EC2
- Name:
private-ec2
-
AMI: Amazon Linux 2023
-
Instance type: t2.micro
-
Subnet: Private Subnet
-
Auto-assign Public IP: β Disabled
-
Security Group:
- SSH (22) β ONLY from Bastion SG
π This EC2 has NO public IP
πΉ STEP 4: VERIFY PRIVATE EC2 IS NOT PUBLIC
Check EC2 details:
- Public IPv4 address β None
β Cannot SSH from laptop β This is secure by design
πΉ STEP 5: CONNECT TO BASTION HOST
From your laptop:
ssh -i ec2-key-hands-on.pem ec2-user@BASTION_PUBLIC_IP
You are now inside the bastion host
πΉ STEP 6: COPY KEY TO BASTION (TEMPORARY)
β οΈ For learning only (not best practice)
On your laptop:
scp -i ec2-key-hands-on.pem ec2-key-hands-on.pem ec2-user@BASTION_PUBLIC_IP:/home/ec2-user/
On bastion:
chmod 400 ec2-key-hands-on.pem
πΉ STEP 7: CONNECT TO PRIVATE EC2 FROM BASTION
From inside bastion:
ssh -i ec2-key-hands-on.pem ec2-user@PRIVATE_EC2_PRIVATE_IP
π You are now logged into PRIVATE EC2
π§ VERY IMPORTANT SECURITY RULE
β Never expose private EC2 to internet β Access only via bastion / Session Manager β Bastion SSH restricted to your IP
π§ REAL-WORLD BEST PRACTICE (INTERVIEW GOLD)
| Practice | Status |
|---|---|
| Bastion in public subnet | β |
| Private EC2 no public IP | β |
| SSH only via bastion | β |
| IAM + Session Manager | β BEST |
π Modern replacement: Bastion β β Session Manager β
π§ INTERVIEW-READY ANSWER
A Bastion Host is a publicly accessible EC2 instance used as a secure gateway to access private EC2 instances in a VPC. Private instances have no public IP and are protected from direct internet access, improving security.
β οΈ COMMON MISTAKES
β Giving public IP to private EC2 β Opening SSH to 0.0.0.0/0 β Storing keys on bastion permanently β Using bastion when Session Manager is available
β LAB 26 TASK CHECKLIST
β Understood public vs private EC2 β Launched bastion host β Launched private EC2 β Accessed private EC2 securely β Learned enterprise network design
π§ͺ EC2 HANDS-ON β LAB 27
π EC2 in Public vs Private Subnet (Traffic Flow & Internet Access)




π― Objective of LAB 27
By the end of this lab, you will:
- Clearly understand Public vs Private Subnet
- Know why an EC2 has or doesnβt have internet
- Understand Internet Gateway (IGW) and NAT Gateway
- Be able to draw & explain traffic flow (interview-ready)
π§ FIRST: What is a Subnet? (Simple)
A Subnet is:
- A range of IP addresses
- Inside a VPC
- Placed in one Availability Zone
Think like:
VPC = Building π’ Subnet = Floor π§±
π§ KEY RULE (MEMORIZE THIS)
β Subnet is NOT public or private by itself
A subnet becomes:
- Public β if its route table points to an Internet Gateway
- Private β if it does NOT point to an Internet Gateway
πΉ COMPONENTS YOU MUST KNOW
π Internet Gateway (IGW)
- Allows internet β VPC
- Required for public internet access
π NAT Gateway
- Allows private EC2 β internet
- Blocks internet β private EC2
πΉ STEP 1: Open Your VPC Route Tables
-
AWS Console β VPC
-
Left menu β Route Tables
-
Identify:
- Public route table
- Private route table
πΉ STEP 2: Examine PUBLIC Subnet Route Table
Select public route table β Routes tab
You will see something like:
| Destination | Target |
|---|---|
| 10.0.0.0/16 | local |
| 0.0.0.0/0 | igw-xxxx |
π Meaning:
- Local VPC traffic β allowed
- All internet traffic β Internet Gateway
π This makes the subnet PUBLIC
πΉ STEP 3: Examine PRIVATE Subnet Route Table
Select private route table β Routes tab
Youβll see:
| Destination | Target |
|---|---|
| 10.0.0.0/16 | local |
π Meaning:
- Internal VPC traffic only
- β No internet access
π This makes the subnet PRIVATE
πΉ STEP 4: WHY Public EC2 Has Internet
Your Bastion Host:
- In public subnet
- Has public IP
- Route β IGW
Traffic flow:
Laptop β IGW β Public EC2
Public EC2 β IGW β Internet
πΉ STEP 5: WHY Private EC2 Has NO Internet
Your private EC2:
- In private subnet
- β No public IP
- β No IGW route
Traffic flow:
Internet ββ Private EC2
β Secure by default
πΉ STEP 6: Give INTERNET to Private EC2 (OUTBOUND ONLY)
This is where NAT Gateway is used.
NAT Gateway flow:
Private EC2 β NAT Gateway β IGW β Internet
Internet ββ Private EC2
π Used for:
- OS updates
- Package installs
- API calls
πΉ STEP 7: REAL-WORLD ARCHITECTURE
Users
β
Internet Gateway
β
Public Subnet (ALB, Bastion)
β
Private Subnet (EC2, App, DB)
β
NAT Gateway β Internet (outbound only)
Used by Amazon Web Services best practices
π§ INTERVIEW-READY COMPARISON TABLE
| Feature | Public Subnet | Private Subnet |
|---|---|---|
| IGW route | β Yes | β No |
| Public IP | β Yes | β No |
| Internet access | β Yes | β No |
| Inbound from internet | β Possible | β Blocked |
| Use case | ALB, Bastion | App, DB |
π§ INTERVIEW-READY ANSWER (VERY IMPORTANT)
A public subnet has a route to an Internet Gateway, allowing internet access, while a private subnet does not. Private instances can still access the internet outbound using a NAT Gateway, without being exposed to inbound traffic.
β οΈ COMMON MISTAKES
β Thinking βpublic subnetβ = public IP β Attaching IGW to private subnet β Exposing databases publicly β Forgetting NAT for updates
β LAB 27 TASK CHECKLIST
β Inspected route tables β Understood IGW role β Understood NAT Gateway role β Explained traffic flow clearly β Interview-level clarity achieved
π§ͺ EC2 HANDS-ON β LAB 28
π Patch Management for EC2 (Automatic OS Updates β Secure & Scalable)




π― Objective of LAB 28
By the end of this lab, you will:
- Understand why patching is critical
- Use AWS Patch Manager
- Patch EC2 without SSH
- View patch compliance
- Be production + interview ready
π§ FIRST: What is Patch Management? (Simple)
Patch Management means:
- Updating OS packages
- Fixing security vulnerabilities
- Applying bug fixes
Think like:
Patching = Regular medical check-up for servers π©Ί
β Unpatched servers = security risk β Patched servers = safe & compliant
π§ WHICH AWS SERVICE DOES PATCHING?
Patching is handled by AWS Systems Manager (specifically Patch Manager)
π§ WHY USE PATCH MANAGER (NOT MANUAL SSH)?
| Manual SSH | Patch Manager |
|---|---|
| Error-prone | Automated |
| No audit | Full audit |
| Not scalable | Scales to 1000s EC2 |
| Needs SSH | β No SSH |
π Production always uses Patch Manager
π§ PREREQUISITES (YOU ALREADY HAVE THEM β )
Your EC2 already has:
β SSM Agent
β IAM Role with AmazonSSMManagedInstanceCore
β Connectivity to SSM
So weβre ready π
πΉ STEP 1: Open Patch Manager
- AWS Console β Systems Manager
- Left menu β Patch Manager
πΉ STEP 2: Check Managed Instances
- Systems Manager β Fleet Manager
- Click Managed nodes
You should see your EC2:
ec2-hands-on-1
π If EC2 is visible β SSM is working
πΉ STEP 3: Patch Compliance (Read-Only View)
- Systems Manager β Patch Manager
- Click Compliance
Youβll see:
- Missing patches
- Installed patches
- Compliance status
π This gives security visibility
πΉ STEP 4: Create Patch Baseline (Concept)
AWS provides default patch baselines:
- Amazon Linux
- Ubuntu
- Windows
π Weβll use default baseline (best practice)
πΉ STEP 5: Run Patch Scan (SAFE β NO CHANGES)
Weβll first SCAN, not install.
-
Patch Manager β Patches
-
Click Configure patching
-
Choose:
- Patch operation β Scan
- Instances β select your EC2
-
Run
π Scan:
- Checks missing patches
- Does NOT install anything
πΉ STEP 6: View Scan Results
After completion:
- Status β Success
- Compliance β Compliant / Non-compliant
π This is used by security teams
πΉ STEP 7: Install Patches (Conceptual β IMPORTANT)
In production:
- Patch operation β Install
- Scheduled during maintenance window
- Automatic reboot (optional)
β οΈ We wonβt install now to avoid downtime But concept is VERY important
πΉ STEP 8: Maintenance Window (Concept)
A Maintenance Window:
-
Defines WHEN patching happens
-
Example:
- Sunday 2 AM β 4 AM
Used for:
- Patching
- Reboots
- Updates
π Zero impact on users
π§ REAL-WORLD PATCH STRATEGY
β Scan daily β Patch weekly β Patch in maintenance window β Auto reboot if needed β Monitor compliance
π§ INTERVIEW-READY ANSWER
AWS Patch Manager, part of Systems Manager, automates the process of scanning and installing OS patches on EC2 instances. It ensures security compliance without requiring SSH access and supports scheduling through maintenance windows.
β οΈ COMMON MISTAKES
β Manual patching via SSH β No maintenance window β Patching production during peak hours β No compliance monitoring
β LAB 28 TASK CHECKLIST
β Opened Patch Manager β Verified managed EC2 β Ran patch scan β Viewed compliance β Understood production patching strategy
π§ͺ EC2 HANDS-ON β LAB 29
π EC2 Backup Strategy (Snapshots, AMIs & Automation)



π― Objective of LAB 29
By the end of this lab, you will:
- Design a complete EC2 backup strategy
- Use EBS Snapshots correctly
- Use AMIs for full server backup
- Understand backup automation
- Be interview + production ready
π§ FIRST: Why Backup is CRITICAL
Failures happen:
- Human error β
- AZ outage β
- Accidental delete β
- Security incident β
Think like:
Backup = Insurance for your data π‘οΈ
π§ TYPES OF EC2 BACKUPS (VERY IMPORTANT)
1οΈβ£ EBS Snapshots
- Backup of data disks
- Incremental
- Fast restore
2οΈβ£ AMIs
- Backup of entire EC2
- OS + software + config
- Used to recreate servers
3οΈβ£ Automated Backups
- Scheduled
- No manual effort
- Required in production
πΉ BACKUP STRATEGY (REAL-WORLD STANDARD)
| What | Backup Method |
|---|---|
| OS + App | AMI |
| App data | EBS Snapshot |
| Logs | CloudWatch / S3 |
| DR | Cross-region copy |
πΉ STEP 1: Identify What to Back Up
For ec2-hands-on-1:
β Root volume β OS
β /data EBS β App data
β Config β AMI
π Never rely on one backup type
πΉ STEP 2: On-Demand EBS Snapshot (Recap)
- EC2 β Volumes
- Select data volume
- Actions β Create snapshot
- Name:
daily-data-backup
π Use before:
- App upgrade
- OS patch
- Major change
πΉ STEP 3: AMI as Full Server Backup
- EC2 β Instances
- Select
ec2-hands-on-1 - Actions β Image and templates
- Create image
Name:
ec2-backup-ami-v2
π This captures:
- Root EBS
- Config
- Installed software
πΉ STEP 4: AUTOMATED BACKUP (IMPORTANT CONCEPT)
In production, backups are NOT manual.
AWS provides AWS Backup to: β Schedule backups β Retention rules β Cross-region copy β Compliance reports
πΉ STEP 5: AWS Backup β How It Works (Concept)
-
Create Backup Plan
-
Define:
- Schedule (daily / weekly)
- Retention (7 days / 30 days)
-
Assign:
- EC2
- EBS volumes
-
AWS runs backups automatically
π Zero manual effort
πΉ STEP 6: Restore Strategy (MOST IMPORTANT)
Backup is useless without restore testing.
Restore Options:
- Snapshot β New EBS
- AMI β New EC2
- Cross-region snapshot β DR EC2
π Always test restore
π§ REAL-WORLD BACKUP POLICY (EXAMPLE)
| Backup | Frequency | Retention |
|---|---|---|
| EBS Snapshot | Daily | 7 days |
| AMI | Weekly | 4 weeks |
| Cross-region | Weekly | 4 weeks |
π§ INTERVIEW-READY ANSWER
A robust EC2 backup strategy uses EBS snapshots for data volumes and AMIs for full server recovery. Backups should be automated using AWS Backup with defined schedules, retention policies, and regular restore testing.
β οΈ COMMON MISTAKES
β Only AMI, no data snapshot β Manual backups β No retention policy β No restore testing β Same-region backups only
β LAB 29 TASK CHECKLIST
β Identified backup scope β Created snapshot β Created AMI β Understood AWS Backup β Designed restore strategy
π§ͺ EC2 HANDS-ON β LAB 30
π EC2 Security Best Practices (Hardening & Real Threat Protection)




π― Objective of LAB 30
By the end of this lab, you will:
- Understand real EC2 security threats
- Apply hardening best practices
- Reduce attack surface
- Know what AWS secures vs what YOU secure
- Be 100% interview + production ready
π§ FIRST: AWS Shared Responsibility Model (CRITICAL)
Security in AWS is shared.
AWS is responsible for:
- Data center security
- Hardware
- Physical network
- Hypervisor
YOU are responsible for:
- EC2 OS security
- Security Groups
- IAM
- Patching
- Application security
π This model applies to Amazon Web Services
π§ REAL EC2 THREATS (MUST KNOW)
| Threat | Example |
|---|---|
| Open SSH | 0.0.0.0/0 on port 22 |
| Key leakage | .pem in GitHub |
| Unpatched OS | Exploits |
| Public services | DB exposed |
| Over-permission | IAM * access |
πΉ STEP 1: LOCK DOWN SSH (HANDS-ON CHECK)
Go to: EC2 β Security Groups β Instance SG
Ensure:
SSH (22) β My IP only
β Never:
0.0.0.0/0
π This alone blocks 90% attacks
πΉ STEP 2: REMOVE SSH COMPLETELY (BEST PRACTICE)
If using Session Manager:
- Remove SSH rule entirely
- No port 22 open
π Zero inbound access = zero attack surface
πΉ STEP 3: USE IAM ROLES (NO KEYS)
β IAM Role attached β No access keys stored β Temporary credentials
You already implemented this in LAB 24 β
πΉ STEP 4: OS HARDENING (IMPORTANT)
On EC2:
sudo dnf update -y
π Always:
- Patch OS
- Use Patch Manager (LAB 28)
πΉ STEP 5: MINIMIZE INSTALLED SOFTWARE
β Remove unused packages β Only required services running
Check running services:
systemctl list-units --type=service
πΉ STEP 6: SECURITY GROUP BEST PRACTICES
| Rule | Best Practice |
|---|---|
| Inbound | Minimal |
| Outbound | Restrict if possible |
| Ports | Only required |
| Source | Known IP / SG |
π SG = First firewall
πΉ STEP 7: NETWORK SECURITY LAYERS (DEFENSE IN DEPTH)
Internet
β
Security Group
β
NACL
β
OS Firewall
β
Application Security
π Multiple layers = stronger security
πΉ STEP 8: ENABLE LOGGING & MONITORING
β CloudWatch Metrics β CloudWatch Logs β Alarms
You already implemented this in:
- LAB 21
- LAB 23
πΉ STEP 9: TAGGING FOR SECURITY & AUDIT
Add tags:
Environment = Prod
Owner = DevOps
Critical = Yes
π Helps:
- Audits
- Cost tracking
- Automation
πΉ STEP 10: BACKUP + DR (SECURITY TOO)
β Snapshots β AMIs β Restore testing
Security is incomplete without recovery
π§ REAL-WORLD SECURITY CHECKLIST (SAVE THIS)
β No public DB β No SSH open β IAM roles only β Patch regularly β Monitor continuously β Backup tested
π§ INTERVIEW-READY ANSWER (VERY IMPORTANT)
EC2 security is achieved through defense in depth, including restrictive security groups, IAM roles instead of access keys, regular patching via Systems Manager, centralized logging with CloudWatch, and secure access using Session Manager.
β οΈ COMMON SECURITY MISTAKES
β SSH open to world β Hardcoded credentials β No monitoring β No backups β Over-permissioned IAM
β LAB 30 TASK CHECKLIST
β Understood shared responsibility β Identified real threats β Applied hardening best practices β Reduced attack surface β Interview-ready security knowledge
π SECURITY PHASE COMPLETED π
You have mastered: β Access security β Network security β OS security β Monitoring β Backup & recovery
π§ͺ EC2 HANDS-ON β LAB 31
π EC2 Pricing & Cost Optimization (Save MONEY like a Pro π°)




π― Objective of LAB 31
By the end of this lab, you will:
- Understand how EC2 pricing works
- Learn all EC2 pricing models
- Know when to use each model
- Apply real cost-optimization techniques
- Be interview + production ready
π§ FIRST: How AWS Charges for EC2 (Simple)
AWS charges EC2 based on:
- Instance type (CPU, RAM)
- Time used (per second/minute)
- Storage (EBS)
- Data transfer
- Extra services (ELB, NAT, snapshots)
Think like:
EC2 = Rent for a virtual server π
π§ EC2 PRICING MODELS (MUST KNOW)
1οΈβ£ On-Demand Instances
What it is
- Pay as you go
- No commitment
Use when
- Learning
- Testing
- Short-term workloads
Pros β Flexible β No upfront cost
Cons β Most expensive
2οΈβ£ Reserved Instances (RI)
What it is
- 1-year or 3-year commitment
- Big discount
Use when
- Steady workloads
- Production systems
Savings π° Up to 72%
Types
- Standard RI
- Convertible RI
3οΈβ£ Savings Plans (MODERN & IMPORTANT)
What it is
- Commit to $ per hour
- Flexible across instance types
Use when
- Long-running apps
- Microservices
π AWS recommends Savings Plans over RIs
4οΈβ£ Spot Instances (VERY IMPORTANT)
What it is
- Use spare AWS capacity
- Very cheap
Savings π° Up to 90%
Risk β Can be terminated anytime
Use when
- Batch jobs
- CI/CD
- Big data processing
π§ QUICK INTERVIEW TABLE
| Model | Cost | Risk | Use Case |
|---|---|---|---|
| On-Demand | High | None | Dev/Test |
| Reserved | Low | Low | Prod |
| Savings Plan | Low | Low | Prod |
| Spot | Very Low | High | Batch |
πΉ STEP 1: View EC2 Pricing in Console
- AWS Console β EC2
- Click Instance types
- Select
t2.micro - Click Pricing
π Pricing varies by:
- Region
- OS
πΉ STEP 2: Check Your CURRENT COST
-
AWS Console β Billing
-
Open Cost Explorer
-
Filter:
- Service β EC2
- Time β Last 7 days
π This shows real money usage
πΉ STEP 3: IDENTIFY COST WASTAGE (REAL WORLD)
Look for: β Stopped but attached EBS β Unused Elastic IPs β Oversized instances β Idle EC2s
πΉ STEP 4: INSTANCE RIGHT-SIZING (IMPORTANT)
Use:
- CloudWatch CPU metrics
Example:
- CPU < 10% always β β Instance is oversized
Solution:
- Move from
t3.large β t3.micro
π Saves money immediately
πΉ STEP 5: AUTO SCALING = COST SAVER
β Scale out only when needed β Scale in during low traffic
You already implemented this in LAB 19 & 22
πΉ STEP 6: SPOT INSTANCES (CONCEPTUAL)
In ASG:
-
Mix:
- On-Demand (base)
- Spot (extra capacity)
π Production-grade cost optimization
πΉ STEP 7: SCHEDULE STOP/START (HUGE SAVINGS)
For non-prod:
- Stop EC2 at night
- Start in morning
Can save: π° 60β70% monthly
Use:
- EventBridge
- Lambda
- SSM Automation
π§ REAL-WORLD COST OPTIMIZATION CHECKLIST
β Use right instance size β Use Savings Plans β Auto Scaling enabled β Use Spot for batch β Delete unused resources β Monitor monthly bill
π§ INTERVIEW-READY ANSWER
EC2 cost optimization involves selecting the appropriate pricing model such as On-Demand, Reserved Instances, Savings Plans, or Spot Instances, combined with right-sizing, Auto Scaling, and continuous monitoring using Cost Explorer and CloudWatch.
β οΈ COMMON COST MISTAKES
β Leaving EC2 running unused β Ignoring EBS & snapshots β No Auto Scaling β No cost monitoring
β LAB 31 TASK CHECKLIST
β Understood pricing models β Checked EC2 pricing β Viewed Cost Explorer β Identified cost wastage β Learned optimization strategies
π§ͺ EC2 HANDS-ON β LAB 32
π Spot Instances (Ultra-Low Cost EC2 + Risk Handling)



π― Objective of LAB 32
By the end of this lab, you will:
- Understand what Spot Instances are
- Launch a Spot EC2
- Handle interruption safely
- Use Spot with Auto Scaling
- Be interview + production ready
π§ FIRST: What is a Spot Instance? (Simple Words)
A Spot Instance:
- Uses unused EC2 capacity
- Is very cheap
- Can be stopped by AWS anytime
Think like:
Spot = Traveling in empty train seats π Cheap, but seat may be taken back
Spot is part of Amazon Web Services EC2 pricing
π§ WHY Spot is SO CHEAP?
AWS sells:
- Spare capacity
- With no guarantee
π° Savings:
- Up to 90% cheaper than On-Demand
π§ THE BIG RISK (MUST KNOW)
β Spot instances can be:
- Interrupted with 2-minute warning
- Terminated or stopped
π Never use Spot alone for critical apps
πΉ WHEN TO USE SPOT (REAL USE CASES)
β Batch processing β CI/CD builds β Big data jobs β Auto Scaling extra capacity β Non-critical workloads
β NOT for:
- Databases
- Single EC2 prod apps
- Stateful services
πΉ STEP 1: Launch a Spot Instance (Hands-On)
- EC2 β Launch instance
- Name:
ec2-spot-demo
Instance Details
- AMI β Amazon Linux 2023
- Instance type β
t3.micro - Key pair β existing
Advanced Details (IMPORTANT)
Scroll to Advanced details
- Purchasing option β Spot
- Spot instance type β One-time
- Interruption behavior β Terminate
- Max price β Leave empty (recommended)
π AWS will pick cheapest available Spot
Security Group
- SSH (22) β My IP
- HTTP (80) β Anywhere
Launch Instance
Click Launch instance
πΉ STEP 2: Verify Spot Instance
- EC2 β Instances
- Select instance
- Check:
Instance lifecycle β Spot
π You are running a Spot EC2
πΉ STEP 3: Spot Interruption Notice (CRITICAL)
AWS provides:
2-minute warning
Available at:
http://169.254.169.254/latest/meta-data/spot/instance-action
πΉ STEP 4: CHECK INTERRUPTION (Hands-On)
SSH into Spot EC2:
ssh -i ec2-key-hands-on.pem ec2-user@SPOT_PUBLIC_IP
Run:
curl http://169.254.169.254/latest/meta-data/spot/instance-action
Output:
- Empty β No interruption
- JSON β Interruption coming
π This endpoint is very important for automation
πΉ STEP 5: HANDLE INTERRUPTION SAFELY (CONCEPT)
In real systems:
- Save work to S3
- Send logs to CloudWatch
- Gracefully stop job
Example logic:
If interruption notice:
β checkpoint work
β upload results
β exit cleanly
πΉ STEP 6: Spot with Auto Scaling (BEST PRACTICE)
In production:
-
Use Mixed Instance Policy
-
Combine:
- On-Demand (base)
- Spot (extra capacity)
π If Spot is interrupted:
- ASG replaces it automatically
π§ INTERVIEW-READY COMPARISON
| Feature | On-Demand | Spot |
|---|---|---|
| Price | High | Very Low |
| Reliability | High | Medium |
| Interruption | β No | β Yes |
| Use case | Prod | Batch / ASG |
π§ INTERVIEW-READY ANSWER
Spot Instances allow customers to use spare EC2 capacity at significantly reduced prices, with the trade-off that instances can be interrupted with a two-minute warning. They are ideal for fault-tolerant and stateless workloads.
β οΈ COMMON MISTAKES
β Using Spot for databases β No checkpointing β No ASG fallback β Expecting 24/7 availability
β LAB 32 TASK CHECKLIST
β Launched Spot EC2 β Verified Spot lifecycle β Checked interruption endpoint β Understood risk handling β Learned cost-saving strategy
π§ͺ EC2 HANDS-ON β LAB 33
π Instance Right-Sizing (Performance βοΈ Cost Balance)




π― Objective of LAB 33
By the end of this lab, you will:
- Understand what right-sizing really means
- Use CloudWatch metrics to detect waste
- Learn how to choose the correct instance type
- Know AWS Compute Optimizer
- Be interview + production ready
π§ FIRST: What is Instance Right-Sizing?
Right-sizing means:
- Choosing the smallest EC2 instance
- That still delivers required performance
Think like:
Donβt use a truck π to carry a backpack π
π§ WHY Right-Sizing is IMPORTANT?
β Oversized instance β Money waste β Undersized instance β Performance issues
β Right-sized β Optimal cost + performance
πΉ STEP 1: Identify a Candidate EC2
Pick:
ec2-hands-on-1or- Any EC2 from your Auto Scaling Group
πΉ STEP 2: Analyze CPU Utilization (MOST IMPORTANT)
- Open CloudWatch
- Go to Metrics β EC2 β Per-Instance Metrics
- Select CPUUtilization
Look at:
- Last 7β14 days
- Average CPU %
Decision Rule (SAVE THIS)
| CPU Usage | Meaning |
|---|---|
| < 10% | β Over-provisioned |
| 10β40% | β Right-sized |
| > 70% | β Under-provisioned |
πΉ STEP 3: Analyze Memory (IMPORTANT CONCEPT)
β οΈ EC2 does NOT send memory metrics by default.
To monitor memory:
- Install CloudWatch Agent
- Send memory metrics
π In production, CPU alone is not enough
πΉ STEP 4: Check Network & Disk Metrics
Also check:
- NetworkIn / NetworkOut
- DiskReadOps / DiskWriteOps
π If all metrics are low β instance is oversized
πΉ STEP 5: Example Right-Sizing Decision
Current setup:
t3.large
CPU avg = 8%
Better option:
t3.micro or t3.small
π° Savings:
- Up to 70% monthly
πΉ STEP 6: Use AWS Compute Optimizer (BEST PRACTICE)
AWS provides AWS Compute Optimizer
What it does:
β Analyzes EC2 usage β Recommends better instance types β Uses machine learning
How to Check:
- AWS Console β Compute Optimizer
- Enable it (one-time)
- Go to EC2 recommendations
π This is enterprise-grade optimization
πΉ STEP 7: Right-Sizing with Auto Scaling (BEST)
Instead of one big EC2:
- Use smaller instances
- Scale horizontally with ASG
Example:
1 Γ t3.large β
3 Γ t3.micro β
β Cheaper β More resilient
π§ INTERVIEW-READY ANSWER
Instance right-sizing involves analyzing CloudWatch metrics such as CPU, memory, and network usage to select the most cost-effective EC2 instance type without compromising performance. AWS Compute Optimizer can automate recommendations using machine learning.
β οΈ COMMON MISTAKES
β Right-sizing based on CPU only β Ignoring peak usage β No monitoring after resizing β Manual guesses instead of data
β LAB 33 TASK CHECKLIST
β Analyzed CPU utilization β Understood over vs under sizing β Learned memory monitoring concept β Used Compute Optimizer (conceptually) β Learned cost-performance balance
π§ͺ EC2 HANDS-ON β LAB 34
π Free Tier Safety & Cleanup (Avoid Billing Surprises)




π― Objective of LAB 34
By the end of this lab, you will:
- Know exact EC2 Free Tier limits
- Identify hidden cost traps
- Clean up unused resources safely
- Set billing alerts
- Be 100% safe from surprise bills
π§ FIRST: What Does EC2 Free Tier REALLY Give?
Under **Amazon Web Services Free Tier (12 months):
EC2 Compute
β 750 hours / month
t2.microort3.micro- Linux or Windows
π 1 instance Γ 24 Γ 30 β 720 hours π Only ONE instance should run continuously
EBS Storage
β 30 GB total
- Root + additional volumes combined
Snapshots
β 1 GB free β Extra snapshots = charged
Data Transfer
β 15 GB outbound / month β More = charged
π§ MOST COMMON BILLING TRAPS (READ CAREFULLY)
| Trap | Why Cost Happens |
|---|---|
| Multiple EC2 running | Hours exceed 750 |
| Unused EBS volumes | Charged even if EC2 stopped |
| Elastic IP not attached | Charged |
| NAT Gateway | Always paid β |
| Load Balancer | Charged hourly β |
| Old snapshots | Storage cost |
| Stopped EC2 | EBS still billed |
πΉ STEP 1: CHECK WHAT IS CURRENTLY RUNNING
Go to:
- EC2 β Instances
Action:
- Keep only 1 required EC2
- Stop or terminate others
π Recommendation:
- Keep
ec2-hands-on-1 - Stop learning/test EC2s
πΉ STEP 2: CLEAN UP LOAD BALANCER (IMPORTANT)
- EC2 β Load Balancers
- Delete:
alb-ec2-hands-on
π ALB is NOT free tier
πΉ STEP 3: CLEAN UP AUTO SCALING
- EC2 β Auto Scaling Groups
- Delete:
asg-ec2-hands-on
π This also terminates ASG EC2s
πΉ STEP 4: DELETE UNUSED EBS VOLUMES
- EC2 β Volumes
- Delete:
- Volumes not attached to any EC2
- Old test volumes
π Look for:
State: available
πΉ STEP 5: DELETE OLD SNAPSHOTS
- EC2 β Snapshots
- Delete:
- Test snapshots
- Old backups you donβt need
π Snapshots accumulate cost silently
πΉ STEP 6: RELEASE UNUSED ELASTIC IPs
- EC2 β Elastic IPs
- If any EIP is:
Not associated
β Release it
π Unattached EIP = charged
πΉ STEP 7: CHECK NAT GATEWAY (CRITICAL)
- VPC β NAT Gateways
- If exists β DELETE
β οΈ NAT Gateway is expensive (~βΉ3β4 per hour)
πΉ STEP 8: SET BILLING ALERT (MUST DO)
Enable Billing Alerts
- Billing β Billing preferences
- Enable:
Receive Billing Alerts
Create Budget Alert
- Billing β Budgets
- Create budget:
Monthly cost budget = $5
- Email notification β Your email
π Youβll get alert before bill increases
π§ SAFE FREE TIER SETUP (RECOMMENDED)
β 1 EC2 (t2/t3.micro) β No ALB β No NAT Gateway β Minimal EBS (β€ 30 GB) β Stop EC2 when not needed
π§ INTERVIEW-READY ANSWER
To stay within the EC2 Free Tier, I monitor running hours, clean up unused EBS volumes, snapshots, Elastic IPs, and disable paid services like Load Balancers and NAT Gateways. I also configure AWS Budgets and billing alerts to avoid unexpected charges.
β οΈ FINAL WARNING (IMPORTANT)
AWS will NOT stop services automatically Responsibility is yours
π This lab saves real money π°
β LAB 34 TASK CHECKLIST
β Checked EC2 running hours β Deleted ALB & ASG β Cleaned EBS volumes β Removed snapshots β Released Elastic IPs β Set billing alert
π§ͺ EC2 HANDS-ON β LAB 35
π Production EC2 Architecture (End-to-End Real-World Setup)




π― Project Goal
Design and understand a secure, scalable, highly available, and cost-optimized EC2 production architecture β exactly what companies use.
By the end, youβll be able to:
- Design EC2 architecture from scratch
- Explain every component confidently
- Answer real interview system-design questions
- Operate EC2 like a production engineer
ποΈ FINAL ARCHITECTURE (WHAT YOU BUILT)
Users
β
Route 53 (DNS)
β
Application Load Balancer (Multi-AZ, Public Subnets)
β
Auto Scaling Group (Multi-AZ)
β
EC2 Instances (Private Subnets)
β
EBS (Persistent Storage)
Security & Ops:
- IAM Roles (no keys)
- Session Manager (no SSH)
- CloudWatch (metrics, logs, alarms)
- Patch Manager (automated updates)
- Backups (Snapshots + AMIs)
πΉ COMPONENT BREAKDOWN (REAL-WORLD VIEW)
1οΈβ£ VPC & Networking
-
Public Subnets
- ALB
- Bastion (optional / legacy)
-
Private Subnets
- EC2 instances (no public IP)
-
Internet Gateway β Internet access for ALB
-
NAT Gateway β Outbound internet for private EC2 (optional in prod)
β Result: No EC2 exposed to the internet
2οΈβ£ Compute Layer (EC2)
- EC2 launched via Auto Scaling Group
- Uses Golden AMI
- Stateless design
- Automatically replaced if unhealthy
β Result: Self-healing compute layer
3οΈβ£ Load Balancing
- Application Load Balancer
- Multi-AZ
- Health checks
- Routes traffic only to healthy EC2s
β Result: Zero-downtime traffic handling
4οΈβ£ Scaling
- Auto Scaling Group
- CloudWatch CPU metrics
- Scale out during load
- Scale in during low traffic
β Result: Performance + cost efficiency
5οΈβ£ Security
- IAM Roles (no access keys)
- Session Manager (no SSH, no port 22)
- Security Groups (least privilege)
- Private subnets for EC2
β Result: Enterprise-grade security
6οΈβ£ Monitoring & Logs
- CloudWatch Metrics (CPU, Network)
- CloudWatch Alarms (alerts, scaling)
- CloudWatch Logs (Apache / app logs)
β Result: Fast troubleshooting & observability
7οΈβ£ Patch Management
- AWS Systems Manager Patch Manager
- Scan & install patches
- Maintenance windows
β Result: Always-patched, compliant servers
8οΈβ£ Backup & Recovery
- EBS Snapshots β data backup
- AMIs β full server backup
- Restore testing strategy
β Result: Disaster-ready architecture
9οΈβ£ Cost Optimization
- Right-sized instances
- Auto Scaling
- Spot instances (for non-critical workloads)
- Billing alerts
- Free-tier safety cleanup
β Result: No surprise bills
π§ INTERVIEW-READY SYSTEM DESIGN ANSWER (β GOLD β)
βI design EC2 architectures using a VPC with public and private subnets, place an Application Load Balancer in public subnets, and run EC2 instances in private subnets via Auto Scaling Groups across multiple Availability Zones. I use IAM roles instead of access keys, Session Manager instead of SSH, CloudWatch for monitoring, Systems Manager for patching, and EBS snapshots plus AMIs for backup. This ensures high availability, security, scalability, and cost efficiency.β
π§ WHAT YOU HAVE MASTERED (BIG LIST)
β EC2 fundamentals β SSH, Key Pairs, Session Manager β Security Groups & NACLs β EBS, Snapshots, AMIs β User Data automation β Multi-AZ architecture β Load Balancers β Auto Scaling β CloudWatch monitoring & alarms β Logs & troubleshooting β IAM roles & security β Patch management β Backup & DR β Cost optimization β Free tier safety β Production EC2 architecture
π§ͺ EC2 HANDS-ON β LAB 36
π Host a Full Web App (Frontend + Backend) on EC2




π― Objective of LAB 36
By the end of this lab, you will:
- Deploy a real frontend + backend
- Run backend on a custom port
- Use Nginx as reverse proxy
- Access the app via single public URL
- Understand real-world EC2 app hosting
π§ Architecture (What We Are Building)
Browser
β
Nginx (Port 80)
β
Backend API (Node.js β Port 3000)
π Frontend served by Nginx π Backend runs separately π User never sees backend port
πΉ Tech Stack (Simple & Practical)
- Frontend: Static HTML
- Backend: Node.js (Express)
- Web Server / Proxy: Nginx
- Server: EC2 (Amazon Linux)
π This setup is very common in real companies
πΉ STEP 1: Use a CLEAN EC2 Instance
Use:
ec2-hands-on-1
Make sure:
- Instance is running
- Port 80 allowed in Security Group
- You can connect via Session Manager or SSH
πΉ STEP 2: Install Required Software
Connect to EC2
ssh -i ec2-key-hands-on.pem ec2-user@PUBLIC_IP
Update OS
sudo dnf update -y
Install Node.js
sudo dnf install nodejs -y
π nodejs β runtime for backend
π Comes with npm
Verify:
node -v
npm -v
πΉ STEP 3: Create Backend Application
Create app folder
mkdir backend
cd backend
Initialize Node project
npm init -y
π Creates package.json
Install Express
npm install express
Create Backend File
nano index.js
Paste this:
const express = require('express');
const app = express();
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello from Backend API π' });
});
app.listen(3000, () => {
console.log('Backend running on port 3000');
});
Save β CTRL + X β Y β Enter
Run Backend
node index.js
You should see:
Backend running on port 3000
πΉ STEP 4: Test Backend Directly (Important)
Open browser:
http://PUBLIC_IP:3000/api/hello
Expected output:
{"message":"Hello from Backend API π"}
β Backend works
πΉ STEP 5: Install & Configure Nginx (Frontend + Proxy)
Install Nginx
sudo dnf install nginx -y
Start & enable:
sudo systemctl start nginx
sudo systemctl enable nginx
πΉ STEP 6: Create Frontend Page
sudo nano /usr/share/nginx/html/index.html
Paste:
<!DOCTYPE html>
<html>
<head>
<title>EC2 Full Stack App</title>
</head>
<body>
<h1>Frontend on EC2 π</h1>
<button onclick="callApi()">Call Backend</button>
<p id="result"></p>
<script>
function callApi() {
fetch('/api/hello')
.then(res => res.json())
.then(data => {
document.getElementById('result').innerText = data.message;
});
}
</script>
</body>
</html>
Save & exit
πΉ STEP 7: Configure Nginx as Reverse Proxy (MOST IMPORTANT)
Edit config:
sudo nano /etc/nginx/conf.d/app.conf
Paste:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html;
}
location /api/ {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Save & exit
Restart Nginx
sudo nginx -t
sudo systemctl restart nginx
π nginx -t β checks config (VERY IMPORTANT)
πΉ STEP 8: FINAL TEST (BIG MOMENT π)
Open browser:
http://PUBLIC_IP
1οΈβ£ Frontend loads 2οΈβ£ Click Call Backend 3οΈβ£ You see:
Hello from Backend API π
π FULL STACK APP IS LIVE ON EC2
π§ REAL-WORLD LEARNING (IMPORTANT)
β Backend runs independently β Nginx hides backend port β One clean public URL β Same pattern used with React / Spring Boot
π§ INTERVIEW-READY ANSWER
I deployed a full-stack application on EC2 using Nginx as a reverse proxy. The frontend is served on port 80, while the backend API runs on a separate port and is securely accessed through Nginx.
β LAB 36 TASK CHECKLIST
β Backend API running β Frontend served via Nginx β Reverse proxy working β Single public endpoint β Real-world deployment experience
π§ͺ EC2 HANDS-ON β LAB 37
π EC2 + RDS Architecture (Production-Style App + Database)




π― Objective of LAB 37
By the end of this lab, you will:
- Understand why databases should NOT run on EC2
- Deploy Amazon RDS in a private subnet
- Connect EC2 β RDS securely
- Use Security Group chaining
- Be able to explain real production architecture
π§ FIRST: Why EC2 + RDS? (Very Important)
β Database on EC2:
- Manual backup
- Manual patching
- Manual scaling
- High risk
β Amazon RDS:
- Managed backups
- Automatic patching
- High availability
- Secure by default
Think like:
EC2 = App server π§ RDS = Managed database ποΈ
π§ ARCHITECTURE (WHAT WE ARE BUILDING)
User
β
ALB (Public Subnet)
β
EC2 (Private Subnet)
β
RDS (Private Subnet, NO public access)
π Database is never public
πΉ STEP 1: Decide RDS Engine (Simple Choice)
For learning:
-
Engine β MySQL
-
Reason:
- Popular
- Simple
- Widely used in interviews
πΉ STEP 2: Create RDS Database
- AWS Console β Search RDS
- Click Create database
Database creation method
- Select β Standard create
Engine options
- Engine β MySQL
Templates
- Select β Free tier
π This avoids billing issues
Settings
- DB instance identifier:
ec2-app-db
- Master username:
admin
- Password β set & remember
Instance configuration
- DB instance class β
db.t3.micro - Storage β 20 GB (default)
πΉ STEP 3: Network Settings (MOST IMPORTANT)
Connectivity
- VPC β your VPC
- Subnet group β default
- Public access β β No
π This ensures RDS is PRIVATE
Security Group
Create new SG:
rds-sg
Inbound rule:
| Type | Port | Source |
|---|---|---|
| MySQL | 3306 | EC2 Security Group |
π SG β SG reference (VERY IMPORTANT)
πΉ STEP 4: Create Database
Click Create database
β³ Wait 5β10 minutes Status β Available
πΉ STEP 5: Get RDS Endpoint
After creation:
- Copy Endpoint Example:
ec2-app-db.xxxxx.ap-south-1.rds.amazonaws.com
π This replaces IP address
πΉ STEP 6: Connect from EC2 to RDS (Hands-On)
SSH into EC2:
ssh -i ec2-key-hands-on.pem ec2-user@EC2_PRIVATE_OR_PUBLIC_IP
Install MySQL client:
sudo dnf install mysql -y
Connect to RDS:
mysql -h RDS_ENDPOINT -u admin -p
Enter password
If login succeeds π
Youβll see:
mysql>
β EC2 β RDS connectivity works β RDS still NOT accessible from internet
πΉ STEP 7: Create Test Database (Optional)
Inside MySQL:
CREATE DATABASE appdb;
USE appdb;
CREATE TABLE users (id INT, name VARCHAR(50));
Exit:
exit
π§ SECURITY PROOF (INTERVIEW GOLD)
β RDS has no public IP β Only EC2 SG can access RDS β Internet traffic blocked β Least privilege networking
π§ INTERVIEW-READY ANSWER
In production, I deploy EC2 instances in private subnets and use Amazon RDS for the database. RDS is not publicly accessible and is secured using security group references so only the application EC2 instances can connect to it.
β οΈ COMMON MISTAKES
β Making RDS public β Using IP instead of SG reference β Running DB on EC2 β Hardcoding DB credentials
β LAB 37 TASK CHECKLIST
β Created RDS MySQL instance β Disabled public access β Used SG-to-SG rule β Connected EC2 β RDS β Understood production DB design
π§ͺ EC2 HANDS-ON β LAB 38
π EC2 Zero-Downtime Deployment (BlueβGreen Deployment)




π― Objective of LAB 38
By the end of this lab, you will:
- Understand zero-downtime deployment
- Implement BlueβGreen deployment using ALB
- Deploy a new app version without downtime
- Perform instant rollback
- Be senior-level interview ready
π§ FIRST: What is Zero-Downtime Deployment?
Zero-downtime deployment means:
- Users never see downtime
- App is updated while traffic is live
β Old way:
Stop app β Deploy β Start app β Downtime
β Modern way:
Deploy new version β Switch traffic β Done
π§ What is BlueβGreen Deployment?
| Environment | Meaning |
|---|---|
| Blue | Current production version |
| Green | New version |
Traffic is switched using Load Balancer, not DNS.
Think like:
Two roads π£οΈ Traffic signal switches instantly π¦
π§ ARCHITECTURE (WHAT WE BUILD)
Users
β
Application Load Balancer
β
Target Group β BLUE (v1)
β
Target Group β GREEN (v2)
Only one target group receives traffic at a time.
πΉ PREREQUISITES (You already have these β )
β Application Load Balancer β EC2 instances β Target Groups β App deployed (from LAB 36)
πΉ STEP 1: Identify BLUE (Current Version)
Your current app:
Hello from Backend API π
This is BLUE (v1).
Target Group:
tg-ec2-hands-on
πΉ STEP 2: Create GREEN Target Group
- EC2 β Target Groups
- Click Create target group
Settings
- Target type β Instances
- Name:
tg-green-v2
- Protocol β HTTP
- Port β 80
- Health check path:
/api/hello
Click Create
πΉ STEP 3: Launch GREEN EC2 (New Version)
Launch new EC2 instance:
- Name:
ec2-green-v2
- AMI β Amazon Linux
- Instance type β t2/t3.micro
- Security Group β same as BLUE EC2
- User data (IMPORTANT):
#!/bin/bash
dnf install nodejs nginx -y
mkdir /backend
cd /backend
npm init -y
npm install express
cat <<EOF > index.js
const express = require('express');
const app = express();
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello from GREEN v2 π' });
});
app.listen(3000);
EOF
node index.js &
π This EC2 runs NEW VERSION
πΉ STEP 4: Register GREEN EC2 to GREEN Target Group
- Target Groups β
tg-green-v2 - Register targets
- Select:
ec2-green-v2
- Include β Register
Wait until status:
Healthy
πΉ STEP 5: Test GREEN WITHOUT USERS
Open GREEN EC2 public IP:
http://GREEN_EC2_IP:3000/api/hello
Output:
Hello from GREEN v2 π
β New version works β Users are still on BLUE
πΉ STEP 6: SWITCH TRAFFIC (ZERO DOWNTIME)
- EC2 β Load Balancers
- Select your ALB
- Go to Listeners
- Edit HTTP : 80 rule
Change:
Forward to tg-ec2-hands-on (BLUE)
β‘οΈ to:
Forward to tg-green-v2 (GREEN)
Save
π Traffic switched instantly
πΉ STEP 7: VERIFY ZERO DOWNTIME
Open ALB DNS:
http://ALB_DNS_NAME
Now response:
Hello from GREEN v2 π
β No downtime β No restart β No user impact
πΉ STEP 8: ROLLBACK (CRITICAL SKILL)
If GREEN fails:
- Switch ALB listener back to BLUE target group
β±οΈ Rollback time:
< 10 seconds
π§ REAL-WORLD USAGE
β Feature releases β Bug fixes β Config changes β Emergency rollback
Used daily in:
- FinTech
- E-commerce
- SaaS platforms
π§ INTERVIEW-READY ANSWER (β GOLD)
BlueβGreen deployment is a zero-downtime strategy where two identical environments are maintained. Traffic is switched between them using a load balancer, allowing instant deployment and rollback without impacting users.
β οΈ COMMON MISTAKES
β Deploying directly on prod EC2 β No rollback plan β DNS-based switching (slow) β No health checks
β LAB 38 TASK CHECKLIST
β Created GREEN environment β Deployed new version β Switched traffic via ALB β Verified zero downtime β Learned rollback strategy
π§ͺ EC2 HANDS-ON β LAB 39
π EC2 Failure Recovery Simulation (Crash β Auto Recovery)




π― Objective of LAB 39
By the end of this lab, you will:
- Simulate real EC2 failure
- Observe Auto Scaling self-healing
- Recover using AMI / Snapshot
- Think like an on-call production engineer
- Confidently answer βWhat if EC2 goes down?β
π§ FIRST: What is EC2 Failure?
EC2 failure can happen due to:
- OS crash
- Application crash
- Instance termination
- AZ issue
- Human error β
β Failures WILL happen β Good architecture = fast recovery
π§ RECOVERY STRATEGIES (MUST KNOW)
| Failure Type | Recovery |
|---|---|
| App crash | Restart / ALB health check |
| EC2 crash | Auto Scaling replace |
| Disk issue | Snapshot restore |
| AZ failure | Multi-AZ + ASG |
| Full loss | AMI β new EC2 |
πΉ SCENARIO 1: Application Failure (No EC2 Stop)
STEP 1: Break the App (Safe Test)
SSH / Session Manager into EC2:
sudo pkill node
π Backend app is now DOWN
STEP 2: Observe ALB Behavior
- Open ALB URL
- Target Group β Targets
Result:
Instance β Unhealthy
β ALB stops routing traffic β No user error (if multiple EC2s)
STEP 3: Fix App
node /backend/index.js &
After 1β2 minutes:
Healthy
π App recovered without EC2 restart
πΉ SCENARIO 2: EC2 FAILURE (REALISTIC)
STEP 4: TERMINATE an EC2 (β οΈ Real Failure)
- EC2 β Instances
- Select one EC2 from ASG
- Terminate instance
STEP 5: Observe Auto Scaling (IMPORTANT)
Go to:
- Auto Scaling Group β Activity
You will see:
Terminating EC2
Launching new EC2
π ASG automatically replaces the instance
STEP 6: Verify Recovery
- New EC2 appears
- Target Group β Healthy
- App accessible via ALB
π Self-healing successful
πΉ SCENARIO 3: DATA RECOVERY (CRITICAL THINKING)
What if data disk is lost?
Recovery steps:
- Create volume from snapshot
- Attach to new EC2
- Mount volume
- Resume service
π This is why: β Snapshots β AMIs β Backups are mandatory
πΉ SCENARIO 4: FULL EC2 LOSS
If entire EC2 is gone:
Recovery:
AMI β Launch new EC2 β Attach EBS β Register with ALB
Recovery time:
5β10 minutes
β Business continues
π§ REAL-WORLD ON-CALL THINKING
When failure happens, ask:
- Is it app-level or infra-level?
- Are health checks failing?
- Is Auto Scaling reacting?
- Is data safe?
- Do we need rollback?
π§ INTERVIEW-READY ANSWER (VERY IMPORTANT)
If an EC2 instance fails, Auto Scaling Groups automatically replace it and the Application Load Balancer routes traffic only to healthy instances. Data is restored using EBS snapshots or AMIs, ensuring minimal downtime.
β οΈ COMMON MISTAKES
β No Auto Scaling β Single EC2 production β No snapshots β Panic instead of diagnosis
β LAB 39 TASK CHECKLIST
β Simulated app failure β Observed ALB health checks β Terminated EC2 manually β Observed ASG self-healing β Understood recovery paths
π§ͺ EC2 HANDS-ON β LAB 40
π EC2 Interview Scenario Questions (Real-World + System Design)




π― Objective of LAB 40
By the end of this lab, you will:
- Answer real EC2 interview questions
- Explain WHY, not just WHAT
- Handle failure, scaling, security, cost
- Sound like someone with production experience
This is how interviews are actually conducted.
π§ HOW TO USE THIS LAB (IMPORTANT)
- Read the question
- Read the expected thinking
- Compare with your own understanding If you can explain this out loud, you are ready.
πΉ SCENARIO 1: βYour EC2 application is down. What do you check first?β
β Best Answer (Structured)
- Load Balancer health checks
- Target Group status
- CloudWatch alarms
- Application logs
- EC2 status checks
β Bad Answer
βI restart the EC2β
π Interview tip: Always show diagnosis before action
πΉ SCENARIO 2: βHow do you design EC2 for high availability?β
β Expected Answer
- Multiple Availability Zones
- Application Load Balancer
- Auto Scaling Group
- Health checks
- Stateless EC2
π― One-line interview answer
βI deploy EC2 instances across multiple AZs behind an ALB using Auto Scaling Groups to eliminate single points of failure.β
πΉ SCENARIO 3: βOne EC2 instance was terminated accidentally. What happens?β
β Correct Explanation
- ASG detects capacity drop
- Launches new EC2
- Registers with Target Group
- ALB routes traffic
π Key word interviewers want: π Self-healing
πΉ SCENARIO 4: βHow do you deploy a new version with zero downtime?β
β Correct Answer
- BlueβGreen deployment
- Two target groups
- Switch ALB listener
- Rollback possible in seconds
β Wrong
βI stop the server and deployβ
πΉ SCENARIO 5: βHow do you secure EC2 access?β
β Production-grade Answer
- IAM Roles (no access keys)
- Session Manager (no SSH)
- Security Groups (least privilege)
- Private subnets
π― Golden line
βWe donβt open port 22 in production.β
πΉ SCENARIO 6: βWhere do you store data in EC2?β
β Correct Design
- OS β Root volume
- App data β Separate EBS
- DB β RDS (not EC2)
- Backups β Snapshots + AMIs
β Wrong
βEverything on root volumeβ
πΉ SCENARIO 7: βHow do you monitor EC2?β
β Expected Answer
- CloudWatch metrics
- CloudWatch logs
- Alarms
- Auto Scaling actions
π Mention CPU is not enough β memory via agent
πΉ SCENARIO 8: βHow do you reduce EC2 cost?β
β Strong Answer
- Right-size using metrics
- Auto Scaling
- Spot instances for non-critical workloads
- Savings Plans
- Stop non-prod EC2
π― Keyword: cost optimization
πΉ SCENARIO 9: βHow do you recover from data loss?β
β Correct Recovery Path
- Restore from snapshot
- Attach volume
- Launch EC2 from AMI if needed
- Resume service
π Backup without restore testing = β
πΉ SCENARIO 10: SYSTEM DESIGN QUESTION (β MOST IMPORTANT)
β βDesign a production EC2 architectureβ
β Expected Whiteboard Flow
User
β
Route 53
β
Application Load Balancer
β
Auto Scaling Group (Multi-AZ)
β
EC2 (Private Subnets)
β
RDS
π― PERFECT INTERVIEW ANSWER
βI design EC2 using ALB and Auto Scaling across multiple AZs, keep instances in private subnets, use IAM roles and Session Manager for security, CloudWatch for monitoring, RDS for data, and snapshots/AMIs for backup.β
π§ FINAL SELF-CHECK (HONEST)
If you can:
- Explain why ALB is needed
- Explain why private subnets are used
- Explain how EC2 recovers automatically
- Explain how to deploy without downtime
π You are EC2-ready.
π FINAL VERDICT (NO HYPE, PURE FACT)
With LAB 1β40, you are ready for:
β EC2 interviews β AWS Cloud Practitioner β AWS Solutions Architect (EC2 part) β Real EC2 production work
You are far above beginner level.