Skip to content

Amazon EC2 (Elastic Compute Cloud)

Introduction

Amazon EC2 provides resizable compute capacity in the cloud as virtual servers called instances. It's the backbone of AWS compute services and offers complete control over computing resources.

Key Features

  • Elasticity - Scale up/down in minutes
  • Multiple instance types - Optimized for different workloads
  • Various pricing models - On-demand, Reserved, Spot, Dedicated
  • Integration - Works seamlessly with other AWS services
  • Global availability - Available in all AWS regions

When to Use

Ideal Use Cases

  • Web applications - Host websites and web servers
  • Application servers - Backend processing, APIs
  • Development/Test environments - Flexible, disposable environments
  • High-performance computing - Scientific modeling, simulations
  • Big data processing - Hadoop, Spark clusters
  • Gaming servers - Low-latency game backends
  • Machine learning training - GPU-powered instances

Signs EC2 is Right for You

  • Need full control over the operating system
  • Require specific software installations
  • Need persistent, long-running workloads
  • Have variable or unpredictable workloads
  • Need to lift-and-shift existing applications

Instance Types

Family Optimized For Use Cases
T (T3, T3a, T4g) Burstable Dev/test, small DBs, web servers
M (M5, M6i, M7g) General purpose Web servers, app servers, small DBs
C (C5, C6i, C7g) Compute Batch processing, gaming, scientific modeling
R (R5, R6i, R7g) Memory In-memory DBs, real-time analytics
X (X1, X2) Memory intensive SAP HANA, large in-memory DBs
I (I3, I4i) Storage NoSQL DBs, data warehousing
D (D2, D3) Dense storage Hadoop, distributed file systems
G (G4, G5) Graphics ML inference, video encoding
P (P3, P4, P5) GPU compute ML training, HPC
Inf (Inf1, Inf2) Inference ML inference at scale

Instance Naming Convention

EC2 Instance Naming


What to Be Careful About

Cost Management

  • Idle instances - Instances running but not being used still incur charges
  • Over-provisioning - Using larger instances than needed
  • EBS volumes - Storage persists and costs money even when instances stop
  • Data transfer - Outbound data transfer costs can add up
  • Elastic IPs - Charged when not associated with a running instance

Security

  • Security Groups - Default denies all inbound; ensure proper rules
  • Key pairs - Losing the private key means losing SSH access
  • Public IPs - Don't expose instances unnecessarily
  • IMDSv2 - Use Instance Metadata Service v2 to prevent SSRF attacks
  • Patch management - You're responsible for OS security updates

Architecture

  • Single AZ risk - Instances in one AZ are vulnerable to AZ failures
  • Instance store - Data is lost when instance stops/terminates
  • Placement groups - Understand cluster vs spread vs partition
  • ENI limits - Each instance type has a max number of network interfaces

Operational

  • Instance limits - Default limits vary by instance type
  • Spot interruptions - Spot instances can be terminated with 2-min warning
  • Scheduled maintenance - AWS may require reboots for maintenance
  • AMI management - Keep AMIs updated and clean up old ones

Pricing Models

Model Discount Commitment Best For
On-Demand None None Unpredictable workloads, testing
Reserved (Standard) Up to 72% 1-3 years Steady-state workloads
Reserved (Convertible) Up to 66% 1-3 years Steady workloads with flexibility
Spot Up to 90% None Fault-tolerant, flexible workloads
Savings Plans Up to 72% 1-3 years Flexible commitment across services
Dedicated Hosts Varies Hourly or Reserved Compliance, licensing requirements

Key Components

AMI (Amazon Machine Image)

  • Template for instance OS and software
  • Can be public, private, or marketplace
  • Region-specific (can be copied across regions)

EBS (Elastic Block Store)

  • Persistent block storage
  • Types: gp3, gp2, io2, io1, st1, sc1
  • Snapshots for backup (stored in S3)

Security Groups

  • Virtual firewall at instance level
  • Stateful (return traffic automatically allowed)
  • Can reference other security groups

Elastic IPs

  • Static public IPv4 addresses
  • Can be remapped between instances
  • Charged when not in use

Common Interview Questions

  1. What's the difference between stopping and terminating an instance?
  2. Stop: Instance halts, EBS data persists, no compute charges
  3. Terminate: Instance deleted, EBS root volume deleted (by default)

  4. How do you make an EC2 instance highly available?

  5. Deploy across multiple AZs
  6. Use Auto Scaling groups
  7. Place behind a load balancer

  8. What's the difference between instance store and EBS?

  9. Instance store: Ephemeral, highest performance, data lost on stop
  10. EBS: Persistent, can be detached/attached, supports snapshots

  11. How do Spot instances work?

  12. Bid on unused EC2 capacity
  13. Can be interrupted with 2-minute warning
  14. Use Spot Fleet for availability across pools

  15. What is a placement group?

  16. Cluster: Low latency, single AZ
  17. Spread: Distinct hardware, multi-AZ
  18. Partition: Groups of instances on separate racks

Alternatives

AWS Alternatives

Service When to Use Instead
Lambda Short-running, event-driven code
Fargate Containerized workloads without managing servers
Lightsail Simple applications with predictable pricing
App Runner Containerized web apps with minimal configuration
Elastic Beanstalk Quick deployment without infrastructure management

External Alternatives

Provider Service
Google Cloud Compute Engine
Azure Virtual Machines
DigitalOcean Droplets
Linode Linodes

Best Practices

  1. Use the latest generation - Better performance per dollar
  2. Right-size instances - Use CloudWatch metrics to optimize
  3. Use Auto Scaling - Match capacity to demand
  4. Spread across AZs - Improve availability
  5. Use IMDSv2 - Enhanced security for metadata
  6. Tag everything - For cost allocation and organization
  7. Use launch templates - Consistent, versioned configurations
  8. Enable detailed monitoring - 1-minute CloudWatch metrics