Complete Step-by-Step Guide
Welcome to Day 2 of the AWS EC2 Series a hands-on journey to mastering EC2 for DevOps and Cloud Engineers.
π HANDS-ON LAB SCENARIO
Lab: Multi-Instance Deployment & Management
Objective: Launch instances across AZs and practice lifecycle operations.
Step 1: Launch Instances
# Create key pair first
aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem
chmod 400 MyKeyPair.pem
# Launch instances in different AZs
aws ec2 run-instances \
--image-id ami-0c02fb55956c7d316 \
--count 3 \
--instance-type t3.micro \
--key-name MyKeyPair \
--placement AvailabilityZone=us-east-1a
Step 2: Instance Operations
# Get instance IDs
INSTANCE_IDS=$(aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' --output text)
# Stop one instance
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
# Change instance type (must be stopped)
aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --instance-type "{\"Value\": \"t3.small\"}"
# Terminate instance
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
Step 3: Monitoring & Verification
# Check instance states
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,InstanceType,State.Name]' --output table
# View pricing information
aws ec2 describe-spot-price-history --instance-types t3.micro --product-descriptions "Linux/UNIX" --start-time 2024-01-01T00:00:00Z
REAL-WORLD PROBLEM & SOLUTION
Problem Statement:
Company XYZ spends $5,000/month on EC2 with predictable 24/7 workload.
How to reduce costs by 40%?
Solution Implementation
Step 1: Analyze Current Usage
# Get current instance inventory
aws ec2 describe-instances \
--query 'Reservations[].Instances[].[InstanceId,InstanceType,State.Name]' \
--output table
# Check Cost Explorer (via AWS Console)
Step 2: Implement Reserved Instances
# Purchase Reserved Instances (conceptual)
aws ec2 purchase-reserved-instances-offering \
--reserved-instances-offering-id <offering-id> \
--instance-count 10 \
--instance-type m5.large
###Step 3: Cost Monitoring Setup
# Create budget alert
aws budgets create-budget \
--account-id 123456789012 \
--budget file://budget.json \
--notifications-with-subscribers file://notifications.json
budget.json
{
"BudgetName": "ec2-monthly-budget",
"BudgetLimit": {
"Amount": "3000",
"Unit": "USD"
},
"CostFilters": {
"Service": "Amazon EC2"
},
"TimeUnit": "MONTHLY",
"BudgetType": "COST"
}
π CORE CONCEPTS Q&A
Q1: What's the difference between stopping vs terminating an instance?
Answer:
STOPPING:
- OS gracefully shuts down
- EBS root volume persists
- Instance can be restarted
- Keeps same private IP (usually)
- Charging stops for instance, continues for EBS
TERMINATING:
- Instance is permanently deleted
- EBS root volume deleted by default
- Cannot be recovered
- All data on instance store lost
- All charging stops
Q2: Explain EC2 purchasing options
Answer:
On-Demand:
- Pay by second/hour
- No long-term commitment
- Most flexible, highest cost
- Use case: unpredictable workloads
Reserved Instances:
- 1-3 year commitment
- 30-60% cost savings
- Types: Standard, Convertible, Scheduled
- Use case: predictable, steady-state
Spot Instances:
- Up to 90% savings
- Can be terminated with 2-minute warning
- Use case: fault-tolerant, flexible workloads
Savings Plans:
- 1-3 year commitment
- Flexible across instance family/region
- Use case: consistent usage patterns
π― COMMON INTERVIEW QUESTIONS
Technical Questions
π‘ "What happens to EBS volumes when you terminate an instance?"
By default, root EBS volume is deleted, additional volumes persist.
π‘ "How do you change instance types?"
Stop instance β Change instance type β Start instance.
π‘ "What's the difference between reboot and stop/start?"
Reboot = OS restart (same hardware).
Stop/Start = may move to new hardware, new public IP.
Scenario-Based Question
"A company has applications with different reliability requirements. How would you recommend instance types?"**
Critical production: On-Demand/Reserved
Testing/Dev: Spot Instances
Batch processing: Spot Fleets
Long-running services: Reserved Instances
Certification-Style Questions
Question 1
Your company needs to run a critical database server for 3 years. Which EC2 option provides the lowest total cost?
Options:
- A) On-Demand Instances
- B) Spot Instances
- C) Reserved Instances (All Upfront)
- D) Savings Plans
Answer: C - Reserved Instances with All Upfront payment for predictable long-term workloads.
Question 2
What happens to data on an instance store volume when you stop an instance?
Answer: Data is preserved on EBS volumes but lost on instance store volumes when instance is stopped/terminated.
Question 3
Which instance would be most cost-effective for a batch processing job that can handle interruptions?
Answer: Spot Instances β up to 90% savings for interruptible workloads.
Troubleshooting Common Issues
Issue: Instance failed to launch due to insufficient capacity
Solutions:
# Try different instance type
aws ec2 run-instances --instance-type t3.small ...
# Try different AZ
aws ec2 run-instances --placement AvailabilityZone=us-east-1b ...
# Use capacity-optimized allocation strategy for Spot
Issue: Cannot Connect to Instance via SSH
β Checklist
- β Security group allows SSH (port 22)
- β Correct key pair
- β Instance is in running state
- β Public IP address is correct
π Day2 Hands-On Checklist
- β Launch 3 instances in different AZs
- β Practice stop/start/terminate operations
- β Change instance type on stopped instance
- β Create and attach EBS volume
- β Configure security groups for SSH access
- β Set up basic CloudWatch monitoring
- β Calculate potential Reserved Instance savings
Top comments (0)