Amazon EC2 โ
Elastic Compute Cloud
Virtual servers in the cloud with full OS control. The foundation of most AWS architectures โ from web apps to enterprise workloads.
โก EC2 in 30 Seconds
- Virtual server in AWS โ full OS control, provisioned in seconds
- Scales horizontally using Auto Scaling Groups (add/remove instances on demand)
- Highly available using Multi-AZ deployment behind a Load Balancer
- Cost optimized via Spot (up to 90% off), Reserved (up to 72% off), or Savings Plans
- Secured through Security Groups, NACLs, IAM Roles, and private subnets
What is EC2
Amazon EC2 provides virtual servers in the cloud. It allows you to run applications without owning physical hardware.
๐ Think of EC2 as: A computer you can create, configure, and destroy anytime
EC2 instances are virtual machines running on AWS-managed physical hosts. You choose the operating system, install software, configure networking โ just like a physical server, but provisioned in seconds and billed by the second.
Before Cloud
- One server per application
- High cost and low utilization
- Slow provisioning (weeks/months)
- Over-provisioning for peak demand
EC2 Solves
- On-demand infrastructure in seconds
- Elastic scaling up and down
- Pay-as-you-go pricing
- Right-size to actual demand
EC2 sits in the compute layer of AWS:
- Cloud โ Compute โ Virtual Machines
It is the foundation for:
Web Applications
Host websites, APIs, and frontend applications behind load balancers.
Backend Systems
Run business logic, batch processing, and background workers.
Enterprise Workloads
Databases, ERP systems, and legacy applications migrated to cloud.
Think of EC2 like renting a machine:
You Manage
- Choose hardware (instance type)
- Install operating system (AMI)
- Run your applications
- Patch and secure the OS
AWS Manages
- Physical servers and racks
- Hypervisor (Nitro)
- Power, cooling, networking
- Data center security
Every modern EC2 instance runs on the AWS Nitro System โ AWS's purpose-built hypervisor platform that replaced the older Xen hypervisor.
Lightweight Hypervisor
Nitro offloads virtualization to dedicated hardware, consuming almost no host resources. Bare-metal-level CPU and memory performance for your instance.
Dedicated Hardware
Separate Nitro cards handle networking and storage I/O โ removing the overhead from the main CPU. Enables consistently high network and EBS throughput.
Security & Isolation
Nitro enforces strong instance isolation. The hypervisor itself is locked down โ even AWS operators cannot access the guest OS. Required for bare-metal instances.
๐ Nitro is why modern EC2 instances offer near bare-metal performance. The Bare Metal instance types (e.g., m5.metal) run directly on Nitro with no hypervisor overhead at all.
EC2 is a virtual server that gives full control over compute in the cloud
Core Building Blocks
A virtual machine running in AWS.
- Runs an OS (Linux or Windows)
- Executes your applications
- Scales based on instance type selection
- Can be started, stopped, and terminated on demand
A template used to launch instances. Think of it as a "snapshot" of a configured machine.
Includes
- Operating system
- Preinstalled software
- Configuration
Sources
- AWS marketplace
- Community AMIs
- Your own custom AMIs
Use Cases
- Consistent deployments
- Golden image pattern
- Disaster recovery
Defines the hardware characteristics of your virtual machine:
| Family | Optimized For | Example Use Case |
|---|---|---|
| General purpose (t3, m5) | Balanced CPU/memory | Web servers, small databases |
| Compute optimized (c5, c6g) | High CPU | Batch processing, gaming servers |
| Memory optimized (r5, x1) | High RAM | In-memory databases, caching |
| Storage optimized (i3, d2) | High disk I/O | Data warehousing, HDFS |
| Accelerated (p4, g5) | GPU / FPGA | ML training, video encoding |
Used for secure access to your instance:
Public Key
Stored in AWS โ placed on the instance at launch time.
Private Key
Downloaded by you โ used to SSH into the instance. Never share this.
User Data is a script that runs automatically when an EC2 instance starts for the first time. It enables fully automated instance setup without manual SSH access.
Install Software
Run yum install or apt-get to install packages, web servers (Apache/Nginx), runtimes (Node.js, Java), or agents on boot.
Configure Environment
Set environment variables, write config files, pull secrets from SSM Parameter Store, or configure application settings from S3.
Start Application
Start your application service automatically after install โ so the instance is ready to serve traffic as soon as it joins the ASG.
- Controls inbound traffic (who can reach the instance)
- Controls outbound traffic (what the instance can reach)
- Rules are stateful โ if you allow inbound, return traffic is automatically allowed
Example rules:
| Type | Port | Source | Purpose |
|---|---|---|---|
| SSH | 22 | Your IP | Remote access |
| HTTP | 80 | 0.0.0.0/0 | Web traffic |
| HTTPS | 443 | 0.0.0.0/0 | Secure web traffic |
EC2 is built from modular components that define compute, access, and security
Networking & Access
A Virtual Private Cloud is your isolated network in AWS.
- You define IP address ranges (CIDR blocks)
- Control traffic flow between resources
- Isolate environments (dev, staging, prod)
Divide your VPC into smaller networks:
Public Subnet
- Internet accessible
- Has route to Internet Gateway
- Use for: web servers, load balancers
Private Subnet
- Internal only โ no direct internet
- No route to Internet Gateway
- Use for: databases, app servers
To access an EC2 instance from the internet:
- Public IP โ temporary, changes on stop/start
- Elastic IP โ static, persists across stop/start
- Internet Gateway โ must be attached to VPC
IAM Roles allow EC2 instances to securely access other AWS services without storing credentials on the instance. AWS automatically rotates temporary credentials via the Instance Metadata Service (IMDS).
Use IAM Roles (Correct)
- No credentials stored on instance
- Temporary credentials auto-rotated by AWS STS
- Credentials available via
http://169.254.169.254/ - Access revoked instantly by modifying the role
Access Keys on Instance (Wrong)
- Credentials leak if instance is compromised
- Hard-coded in env files or code = disaster
- Rotation is manual and error-prone
- Violates AWS security best practices
Common IAM role use cases for EC2:
- Read objects from an S3 bucket
- Write logs to CloudWatch Logs
- Read secrets from Secrets Manager or SSM Parameter Store
- Publish messages to SQS or SNS
๐ Always use IAM roles instead of access keys on EC2. This is an exam favourite and a real-world security requirement.
| Feature | Security Group | Network ACL |
|---|---|---|
| Level | Instance | Subnet |
| Type | Stateful | Stateless |
| Rules | Allow only | Allow and Deny |
| Evaluation | All rules evaluated | Rules evaluated in order |
| Default | Deny all inbound | Allow all |
When a user connects to an EC2 instance, traffic passes through multiple checkpoints:
Networking controls how EC2 instances communicate with the world and each other
Storage & Data Layer
Persistent block storage attached to EC2 instances.
- Acts like a hard disk attached to your instance
- Survives instance stop and restart
- Can be detached and reattached to another instance
- Supports snapshots for backup
EBS offers multiple volume types optimized for different performance and cost requirements. Choosing the wrong type is a common source of both performance problems and unnecessary cost.
| Type | Category | Max IOPS | Best For |
|---|---|---|---|
| gp3 | General Purpose SSD | 16,000 | Default for most workloads โ web servers, dev environments |
| gp2 | General Purpose SSD (older) | 16,000 | Legacy โ prefer gp3 (cheaper + better baseline IOPS) |
| io1 / io2 | Provisioned IOPS SSD | 64,000+ | High-performance databases (MySQL, Oracle, SQL Server) |
| st1 | Throughput Optimized HDD | 500 | Big data, log processing, data warehouses (sequential access) |
| sc1 | Cold HDD | 250 | Infrequently accessed data โ lowest cost option |
๐ gp3 is the right default for most workloads. Upgrade to io2 only when your database requires guaranteed high IOPS. Use st1 for large sequential read/write patterns (Hadoop, Kafka logs).
Temporary, high-performance storage physically attached to the host:
- Extremely high I/O performance
- Data is lost when instance stops or terminates
- Ideal for temporary data: caches, buffers, scratch files
Point-in-time backup of EBS volumes:
- Stored in S3 (managed by AWS โ you don't see the bucket)
- Incremental โ only changed blocks are saved after first snapshot
- Used to restore volumes or create new volumes in different AZs
- Enable cross-region disaster recovery
| Feature | EBS | Instance Store |
|---|---|---|
| Persistence | Yes โ survives stop | No โ lost on stop |
| Performance | Medium to High | Very High (local NVMe) |
| Detachable | Yes | No |
| Snapshot support | Yes | No |
| Use case | Databases, boot volumes | Caches, temporary data |
Storage choice impacts durability, performance, and cost of your EC2 workloads
Scaling & High Availability
A single EC2 instance is a single point of failure.
Problems with Single Instance
- Cannot handle traffic spikes
- Fails if instance crashes
- No redundancy
- No fault tolerance
Solution
- Horizontal scaling (add more instances)
- Distribute across AZs
- Auto-replace unhealthy instances
- Load balance traffic
A Launch Template defines exactly how EC2 instances should be created โ capturing all the configuration in one reusable, versioned document. Auto Scaling Groups require a Launch Template (or the older Launch Configuration) to know what to launch.
What It Defines
- AMI ID (which OS/image to use)
- Instance type (e.g.,
t3.medium) - Key pair for SSH access
- Security groups and IAM role
- User data bootstrap script
- EBS volume configuration
Why Use Templates
- Versioned โ roll back to previous config
- Reusable across multiple ASGs and regions
- Supports mixed instance types for Spot + On-Demand
- Required for Spot Fleet and EC2 Fleet
- Replaces the older Launch Configuration (no versions)
Auto Scaling Groups manage a fleet of EC2 instances automatically:
- Maintain desired capacity โ always keep N instances running
- Replace unhealthy instances โ detect failures and launch replacements
- Scale based on demand โ add/remove instances as load changes
| Policy Type | How It Works | Best For |
|---|---|---|
| Dynamic Scaling | React to metrics (CPU > 70%, request count) | Variable traffic patterns |
| Scheduled Scaling | Scale at fixed times (e.g., 9am MโF) | Predictable daily/weekly patterns |
| Predictive Scaling | ML-based forecasting from historical data | Recurring spikes with lead time |
Load balancers distribute incoming traffic across EC2 instances:
- Prevent any single instance from being overloaded
- Improve fault tolerance โ unhealthy instances are removed
- Enable horizontal scaling transparently to users
ALB
Application Load Balancer โ HTTP/HTTPS, path-based routing, WebSockets.
NLB
Network Load Balancer โ TCP/UDP, ultra-low latency, static IPs.
GLB
Gateway Load Balancer โ for third-party virtual appliances (firewalls, IDS).
Deploy instances across multiple Availability Zones:
- Protects against AZ failure (data center-level outage)
- Ensures system uptime even if one AZ goes down
- ALB automatically routes to healthy AZs
- ELB health checks โ ping instances on a configured path
- EC2 status checks โ monitor hardware and software health
- ASG integration โ automatically terminates and replaces failed instances
Scalable systems are built using Auto Scaling + Load Balancing + Multi-AZ design
Cost & Optimization
EC2 offers multiple pricing options to match different workload patterns:
On-Demand
- Pay per second/hour
- No commitment
- Most flexible
- Highest per-unit cost
Reserved Instances
- 1 or 3-year commitment
- Up to 72% discount
- Best for predictable workloads
- Convertible or Standard
Spot Instances
- Up to 90% discount
- Can be interrupted (2-min notice)
- Best for fault-tolerant jobs
- Batch, CI/CD, HPC
Savings Plans are a flexible pricing model that offers similar discounts to Reserved Instances but with greater flexibility across services and regions.
| Feature | Reserved Instances | Savings Plans |
|---|---|---|
| Commitment | Specific instance type + region | Hourly spend commitment (e.g., $10/hr) |
| Flexibility | Low โ locked to instance family | High โ applies across EC2, Lambda, Fargate |
| Discount | Up to 72% | Up to 66% |
| Best for | Stable, known instance types | Mixed or evolving workloads |
๐ Compute Savings Plans are the most flexible โ they apply automatically across any EC2 instance type, region, OS, and also cover Lambda and Fargate. Start here if unsure.
- Use Spot for batch processing, CI/CD, dev environments
- Use Reserved for steady-state production workloads
- Stop unused instances โ don't pay for idle compute
- Right-size instances โ monitor and downsize over-provisioned ones
- Use Savings Plans โ flexible alternative to Reserved Instances
- Choose the correct instance family for your workload
- Use SSD-backed EBS (gp3) for latency-sensitive workloads
- Use enhanced networking for high-throughput applications
- Place instances in the same AZ as their data when possible
Track key metrics to optimize both cost and performance:
| Metric | What It Tells You | Action |
|---|---|---|
| CPU Utilization | Is the instance over/under-provisioned? | Right-size or scale |
| Network In/Out | Traffic patterns and throughput needs | Upgrade instance or add ELB |
| Disk I/O | Storage bottlenecks | Switch EBS type or add IOPS |
| Status Checks | Hardware/software failures | Auto-replace via ASG |
Efficient EC2 usage balances performance needs with cost optimization strategies
Architecture Patterns
Architecture
- One EC2 instance
- Public IP or Elastic IP
- Security Group
Limitations
- Single point of failure
- No scaling
- Not production-ready
- Suitable for testing only
Architecture
- Application Load Balancer
- Multiple EC2 instances
- Auto Scaling Group
- Multi-AZ deployment
Benefits
- Fault tolerance
- Automatic scaling
- High availability
- Self-healing
A complete production architecture includes:
- Frontend tier: ALB + Auto Scaling Group across 2+ AZs
- Application tier: Private subnet EC2 instances with internal ALB
- Database tier: RDS Multi-AZ or Aurora in private subnet
- Monitoring: CloudWatch alarms + SNS notifications
- Security: NACLs + Security Groups + IAM roles
- Avoid single points of failure โ always multi-AZ
- Scale horizontally (add instances) not vertically (bigger instance)
- Use automation โ ASG, Launch Templates, IaC (CloudFormation/Terraform)
- Monitor system health โ CloudWatch metrics + alarms
- Decouple components โ use SQS, SNS between tiers
Network Security
- Use least-privilege security groups โ open only the ports you need
- Avoid public IPs for backend/database instances
- Place app and DB tiers in private subnets
- Use a Bastion Host (jump server) for SSH to private instances
- Use AWS Systems Manager Session Manager instead of SSH where possible
Identity & Patch
- Use IAM roles โ never store access keys on instances
- Keep OS and software regularly patched โ use SSM Patch Manager
- Enable CloudTrail to audit API calls
- Enable GuardDuty for threat detection
- Restrict inbound SSH to specific IPs โ never
0.0.0.0/0on port 22
| Mistake | Why It's Bad | Fix |
|---|---|---|
| Single instance in production | One failure = total downtime | Use ASG + Multi-AZ |
| Not using Auto Scaling | Over-pay or under-serve | Define scaling policies |
| Ignoring cost optimization | 10x overspend vs. right-sized | Monitor + right-size + RI/Spot |
| Poor security config | Open ports = attack surface | Least-privilege SGs + private subnets |
| No monitoring | Blind to failures and waste | CloudWatch + alarms + dashboards |
EC2 becomes powerful when combined with scaling, networking, and architecture patterns โ not when used alone