Introduction to Amazon EC2
What is Amazon EC2?
Amazon EC2 (Elastic Compute Cloud) is the fundamental computing service of AWS. It allows you to rent virtual servers (called instances) in the cloud.
┌─────────────────────────────────────────────────────────────┐
│ AMAZON EC2 │
├─────────────────────────────────────────────────────────────┤
│ │
│ "On-demand virtual servers" │
│ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ EC2 Instance │ │
│ │ │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ CPU │ │ RAM │ │ Storage │ │ │
│ │ │ (vCPU) │ │ (GiB) │ │ (EBS) │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ │ │
│ │ │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ OS │ │ Network │ │ Security │ │ │
│ │ │Linux/Win │ │ (IP) │ │ (SG) │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────┘ │
│ │
│ You control: │
│ • Instance type/size │
│ • Operating system │
│ • Installed software │
│ • Network and security configuration │
└─────────────────────────────────────────────────────────────┘
Analogy: Apartment Rental
┌────────────────────────────────────────────────────────────────┐
│ TRADITIONAL RENTAL vs AMAZON EC2 │
├────────────────────────────────────────────────────────────────┤
│ │
│ 🏠 Apartment 💻 EC2 Instance │
│ ───────────── ───────────── │
│ Size (m²) Instance type (t2, m5) │
│ Number of rooms vCPU and RAM │
│ Location (city) AWS Region │
│ Lease (1 year minimum) On-demand (per second) │
│ Optional furniture AMI (pre-installed OS) │
│ Lock/Alarm Security Group │
│ │
│ 💰 Fixed monthly rent 💰 Pay-as-you-go │
│ 📋 3-month notice 🔄 Immediate stop possible │
└────────────────────────────────────────────────────────────────┘
EC2 Use Cases
When to Use EC2?
| Use Case | Description | Example |
|---|---|---|
| Web Servers | Hosting websites and applications | Apache, Nginx |
| Business Applications | ERP, CRM, internal software | SAP, Oracle |
| Databases | Self-managed DBs | MySQL, PostgreSQL |
| Development/Test | Temporary environments | CI/CD, QA |
| High Performance Computing | Data processing | Analytics, ML |
| Gaming | Game servers | Minecraft, game servers |
EC2 vs Other Services
┌─────────────────────────────────────────────────────────────┐
│ CHOOSING THE RIGHT COMPUTE SERVICE │
├─────────────────────────────────────────────────────────────┤
│ │
│ EC2 Lambda ECS/EKS │
│ ┌────────────┐ ┌────────────┐ ┌────────────┐│
│ │ Full │ │ Serverless │ │ Containers ││
│ │ control │ │ Events │ │ Docker ││
│ │ │ │ │ │ ││
│ │ Full OS │ │ Code only │ │ Images ││
│ │ 24/7 │ │ On-demand │ │ Orchestrated ││
│ └────────────┘ └────────────┘ └────────────┘│
│ │
│ Use EC2 if: │
│ ✅ You need a full OS │
│ ✅ Long-running applications (24/7) │
│ ✅ Software that cannot be containerized │
│ ✅ Total control over configuration │
│ │
│ Do NOT use EC2 if: │
│ ❌ Short and event-driven tasks → Lambda │
│ ❌ Containerized applications → ECS/EKS │
│ ❌ Static sites → S3 + CloudFront │
└─────────────────────────────────────────────────────────────┘
EC2 Instance Lifecycle
┌─────────────────────────────────────────────────────────────┐
│ EC2 LIFECYCLE │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────┐ │
│ │ PENDING │ ← Launching │
│ └────┬────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ ┌─────────┐ │
│ │STOPPING │◄──────►│ RUNNING │ ← Running │
│ └────┬────┘ └────┬────┘ (active billing) │
│ │ │ │
│ ▼ │ │
│ ┌─────────┐ │ │
│ │ STOPPED │◄────────────┘ │
│ └────┬────┘ ← Stopped (no compute billing) │
│ │ │
│ │ Terminate │
│ ▼ │
│ ┌─────────────┐ │
│ │ TERMINATING │ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ TERMINATED │ ← Permanently deleted │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
States and Billing
| State | Description | Billing |
|---|---|---|
| Pending | Starting up | No |
| Running | Running | ✅ Yes |
| Stopping | Stopping | Yes |
| Stopped | Stopped | No (but EBS billed) |
| Terminating | Being deleted | No |
| Terminated | Deleted | No |
Attention
A Stopped instance does not incur compute charges (CPU/RAM), but EBS storage is still billed!
EC2 Pricing Models
1. On-Demand
┌─────────────────────────────────────────────────────────────┐
│ ON-DEMAND │
├─────────────────────────────────────────────────────────────┤
│ │
│ 💰 Payment: Per hour or per second │
│ 📋 Commitment: None │
│ ⚡ Flexibility: Maximum │
│ 💲 Price: Highest │
│ │
│ Ideal for: │
│ • Development and testing │
│ • Unpredictable workloads │
│ • Short-term applications │
│ │
│ Example: t3.micro = ~$0.0104/hour (us-east-1) │
└─────────────────────────────────────────────────────────────┘
2. Reserved Instances
┌─────────────────────────────────────────────────────────────┐
│ RESERVED INSTANCES │
├─────────────────────────────────────────────────────────────┤
│ │
│ 💰 Payment: 1 or 3-year commitment │
│ 📋 Commitment: High │
│ ⚡ Flexibility: Limited │
│ 💲 Price: Up to 72% discount │
│ │
│ Types: │
│ • Standard: Best price, less flexible │
│ • Convertible: Medium price, can change type │
│ │
│ Ideal for: │
│ • Stable and predictable workloads │
│ • 24/7 production applications │
│ │
└─────────────────────────────────────────────────────────────┘
3. Spot Instances
┌─────────────────────────────────────────────────────────────┐
│ SPOT INSTANCES │
├─────────────────────────────────────────────────────────────┤
│ │
│ 💰 Payment: Bidding on unused capacity │
│ 📋 Commitment: None │
│ ⚡ Flexibility: Variable (possible interruption) │
│ 💲 Price: Up to 90% discount! │
│ │
│ ⚠️ WARNING: AWS can reclaim the instance │
│ with 2 minutes notice! │
│ │
│ Ideal for: │
│ • Batch processing │
│ • Interruption-tolerant workloads │
│ • CI/CD, testing │
│ │
└─────────────────────────────────────────────────────────────┘
Price Comparison (Example t3.large, us-east-1)
On-Demand ████████████████████████████████ $0.0832/h (100%)
Reserved 1yr █████████████████████ $0.0528/h (63%)
Reserved 3yr ███████████████ $0.0333/h (40%)
Spot ████████ $0.0250/h (30%)
Components of an EC2 Instance
┌─────────────────────────────────────────────────────────────┐
│ COMPONENTS OF AN EC2 INSTANCE │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ EC2 INSTANCE │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ 1. AMI (Amazon Machine Image) │ │ │
│ │ │ Operating system + software │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ 2. Instance Type │ │ │
│ │ │ CPU (vCPU) + RAM (GiB) │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ 3. Storage (EBS or Instance Store) │ │ │
│ │ │ Attached disks │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ 4. Network Configuration │ │ │
│ │ │ VPC, Subnet, Public/Private IP │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ 5. Security Group │ │ │
│ │ │ Firewall rules │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ 6. Key Pair │ │ │
│ │ │ SSH Authentication │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Instance Metadata
Each EC2 instance has access to its own metadata via a special URL:
http://169.254.169.254/latest/meta-data/
Available Information
| Endpoint | Information |
|---|---|
/instance-id | Instance ID |
/instance-type | Type (t3.micro, etc.) |
/ami-id | AMI ID |
/local-ipv4 | Private IP |
/public-ipv4 | Public IP |
/placement/availability-zone | Availability zone |
Usage Example
# From the EC2 instance
curl http://169.254.169.254/latest/meta-data/instance-id
# Result: i-0123456789abcdef0
curl http://169.254.169.254/latest/meta-data/instance-type
# Result: t3.micro
Utility
Metadata is useful for:
- Automatic configuration scripts
- Applications that need to know their environment
- Retrieving temporary IAM credentials
Summary
| Concept | Description |
|---|---|
| EC2 | AWS virtual machine service |
| Instance | An individual virtual server |
| AMI | System image (OS + software) |
| Type | CPU/RAM configuration |
| On-Demand | Pay-as-you-go, flexible |
| Reserved | Long-term commitment, savings |
| Spot | Unused capacity, very economical but interruptible |
Validation Quiz
- What is an EC2 instance?
- What is the difference between the "Stopped" and "Terminated" states?
- Which pricing model offers the greatest flexibility?
- Which model offers the greatest savings with a risk of interruption?
- What are the 6 main components of an EC2 instance?
- What is the purpose of instance metadata?
Next section: Regions and Availability Zones