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
An EC2 instance moves through specific states during its lifecycle. Understanding what happens at each state is critical for managing data, billing, and recovery.
| State | Billing? | EBS Data? | Instance Store? | Notes |
|---|---|---|---|---|
| pending | No | โ | โ | Instance launching โ allocating resources |
| running | Yes | โ Persists | โ Available | Active โ accepting connections, executing code |
| stopping | No | โ Persists | โ Lost | Transitioning to stopped โ instance store data lost here |
| stopped | No (compute) Yes (EBS) | โ Persists | โ Gone | No compute charges โ EBS volume charges continue |
| shutting-down | No | Depends* | โ Gone | *EBS deleted if DeleteOnTermination=true (default for root) |
| terminated | No | โ Deleted* | โ Gone | Instance gone forever โ cannot recover |
๐ Stop vs Terminate: Stop = pause (EBS data safe, no compute cost). Terminate = delete (everything gone unless EBS volumes have DeleteOnTermination=false). Enable termination protection for production instances.
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.
Instance Metadata Service (IMDS) provides information about the running instance โ its IP address, instance type, IAM role credentials, and more. Accessible from within the instance at http://169.254.169.254/latest/meta-data/.
| Feature | IMDSv1 (Legacy) | IMDSv2 (Recommended) |
|---|---|---|
| Request method | Simple GET request | Session-based: PUT to get token, then GET with token header |
| Security | Vulnerable to SSRF attacks (attacker can access metadata via app) | Token-based โ prevents SSRF exploitation |
| Hop limit | Unlimited (containers/proxies can reach it) | Default hop=1 โ only the instance itself can reach it |
| AWS recommendation | Disable in new launches | Enforce as required (block IMDSv1) |
Common Metadata Endpoints
/instance-idโ unique instance identifier/public-ipv4โ public IP address/iam/security-credentials/โ temporary IAM role credentials/hostnameโ internal DNS name/placement/availability-zoneโ which AZ
IMDSv2 Best Practices
- Set
HttpTokens=requiredto enforce IMDSv2 - Set
HttpPutResponseHopLimit=1to block container access - Use in launch template for all new instances
- Audit with AWS Config rule:
ec2-imdsv2-check - Capital One breach (2019) exploited IMDSv1 via SSRF
๐ Exam tip: If the question mentions SSRF, metadata access, or credential theft โ the answer involves IMDSv2. Always enforce IMDSv2 on new instances.
- 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
| Feature | Auto-assigned Public IP | Elastic IP (EIP) |
|---|---|---|
| Assigned when | Launch in subnet with auto-assign enabled | You allocate and associate manually |
| Persists on stop? | โ Released on stop โ new IP on start | โ Stays until you release it |
| Cost when attached | Free (while running) | Free (while attached to running instance) |
| Cost when NOT attached | N/A | $0.005/hour โ AWS charges for wasted IPs |
| Quota | Unlimited | 5 per region (default, can request increase) |
| Best for | Ephemeral instances, auto-scaling | Static endpoints: NAT, bastion, failover |
๐ Default subnets auto-assign public IPs. Custom subnets do NOT by default โ you must enable it or use an Elastic IP. An unused EIP costs $3.60/month โ always release unattached EIPs.
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).
Multi-Attach allows a single EBS volume to be attached to up to 16 EC2 instances simultaneously โ but only within the same AZ and only for io1/io2 Provisioned IOPS volumes.
| Feature | Details |
|---|---|
| Supported types | io1, io2 only (NOT gp2, gp3, st1, sc1) |
| Max instances | 16 simultaneous attachments |
| AZ constraint | Same Availability Zone only |
| Filesystem requirement | Must use cluster-aware filesystem (GFS2, OCFS2) โ NOT ext4/xfs |
| Use cases | Clustered databases, shared storage for HA applications |
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)
| Feature | Launch Template โ | Launch Configuration (Legacy) โ |
|---|---|---|
| Versioning | Yes โ multiple versions, default version | No โ immutable, create new for any change |
| Mixed instance types | Yes โ combine On-Demand + Spot in one ASG | No โ single instance type only |
| T2/T3 Unlimited | Yes | No |
| Spot options | Full control (max price, allocation strategy) | Limited |
| Network interfaces | Multiple ENIs supported | Limited |
| Placement groups | Yes | Yes (limited) |
| AWS recommendation | Use this always | Deprecated โ do not use for new setups |
Placement Groups control how EC2 instances are physically placed on underlying hardware. Choose based on whether you need low latency, fault isolation, or large-scale distribution.
Cluster
- All instances in same rack, same AZ
- Lowest latency โ 10 Gbps between instances
- Risk: rack failure = all instances fail
- Use for: HPC, ML training, real-time analytics
- Best with enhanced networking (ENA)
Spread
- Each instance on separate hardware
- Max 7 instances per AZ per group
- Maximizes fault isolation
- Use for: critical instances that must not fail together
- Small number of high-value instances
Partition
- Instances grouped into partitions on different racks
- Up to 7 partitions per AZ
- Hundreds of instances per group
- Use for: HDFS, HBase, Cassandra, Kafka
- Partition-aware apps know which partition they're in
| Placement Group | Use Case | Max Per AZ | Risk |
|---|---|---|---|
| Cluster | Low latency, high throughput between instances | Unlimited (same rack) | Single rack failure = all fail |
| Spread | Critical instances that must be isolated | 7 instances max | Low โ separate hardware per instance |
| Partition | Large distributed workloads (Kafka, HDFS) | 7 partitions, many instances | Partition failure = subset fails |
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
Capacity Reservations guarantee that EC2 capacity will be available when you need it โ without the pricing discount of Reserved Instances. Use them when you need assured capacity during peak events or compliance requirements.
| Feature | On-Demand Capacity Reservation | Reserved Instances |
|---|---|---|
| Pricing discount? | No โ pay On-Demand rate | Yes โ up to 72% off |
| Capacity guaranteed? | Yes โ capacity reserved in specific AZ | Optional (zonal RI only) |
| Commitment | None โ cancel anytime | 1 or 3 years |
| Best for | DR readiness, launch events, compliance | Steady-state production workloads |
| Can combine with Savings Plans? | Yes โ get discount + guaranteed capacity | โ |
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 |
| Feature | Basic Monitoring (Default) | Detailed Monitoring |
|---|---|---|
| Interval | 5 minutes | 1 minute |
| Cost | Free | $2.10/instance/month (7 metrics ร $0.30) |
| Use case | General monitoring | ASG rapid scaling, alerting on fast changes |
CloudWatch Agent collects metrics that AWS cannot see from outside the instance:
- Memory utilization โ not available in default CloudWatch metrics
- Disk space โ filesystem-level usage
- Process counts โ running/zombie processes
- Custom application metrics โ anything you emit
๐ Exam tip: "How to monitor memory on EC2?" โ Answer: Install CloudWatch Agent. Memory and disk usage are NOT available in default CloudWatch metrics โ they require the agent.
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