Amazon EC2
LearningTree ยท AWS ยท Compute

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
01
Chapter One

What is EC2

Introduction Introductory

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.

Why EC2 Exists Introductory
โš ๏ธ

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
Where EC2 Fits Introductory

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.

Mental Model Core

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
AWS Nitro System In-Depth

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.

Concept Diagram Introductory
EC2 โ€” User connects to instance inside AWS Cloud
๐Ÿ‘ค USER INTERNET AWS CLOUD VPC PUBLIC SUBNET INSTANCE Hypervisor ยท Hardware Power ยท Cooling โ†‘ AWS manages โ† YOU manage AWS manages โ†’
๐Ÿ‘‰ Key Takeaway

EC2 is a virtual server that gives full control over compute in the cloud

02
Chapter Two

Core Building Blocks

EC2 Instance Core

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
AMI (Amazon Machine Image) Core

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
Instance Types Core

Defines the hardware characteristics of your virtual machine:

FamilyOptimized ForExample Use Case
General purpose (t3, m5)Balanced CPU/memoryWeb servers, small databases
Compute optimized (c5, c6g)High CPUBatch processing, gaming servers
Memory optimized (r5, x1)High RAMIn-memory databases, caching
Storage optimized (i3, d2)High disk I/OData warehousing, HDFS
Accelerated (p4, g5)GPU / FPGAML training, video encoding
Key Pair Core

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 (Bootstrap Scripts) In-Depth

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.

EC2 Bootstrap Flow โ€” User Data Execution
AMI OS template LAUNCH Instance starts USER DATA Script runs once APP READY Traffic accepted Runs as root ยท First boot only ยท Use IMDSv2 to access instance metadata
Security Group (Intro) Introductory
  • 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:

TypePortSourcePurpose
SSH22Your IPRemote access
HTTP800.0.0.0/0Web traffic
HTTPS4430.0.0.0/0Secure web traffic
๐Ÿ‘‰ Key Takeaway

EC2 is built from modular components that define compute, access, and security

03
Chapter Three

Networking & Access

VPC (Virtual Private Cloud) Core

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)
Subnets Core

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
Internet Access Core

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 Role for EC2 In-Depth

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.

Security Groups vs NACLs In-Depth
FeatureSecurity GroupNetwork ACL
LevelInstanceSubnet
TypeStatefulStateless
RulesAllow onlyAllow and Deny
EvaluationAll rules evaluatedRules evaluated in order
DefaultDeny all inboundAllow all
Access Flow Core

When a user connects to an EC2 instance, traffic passes through multiple checkpoints:

Traffic Flow โ€” Internet โ†’ VPC โ†’ EC2
VPC ๐Ÿ‘ค USER IGW ๐Ÿ›ก๏ธ NACL Subnet level ๐Ÿ”’ SECURITY GROUP EC2 Stateless ยท Subnet Allow+Deny ยท Order Stateful ยท Allow only Your instance
๐Ÿ‘‰ Key Takeaway

Networking controls how EC2 instances communicate with the world and each other

04
Chapter Four

Storage & Data Layer

EBS (Elastic Block Store) Core

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 Volume Types In-Depth

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.

TypeCategoryMax IOPSBest For
gp3General Purpose SSD16,000Default for most workloads โ€” web servers, dev environments
gp2General Purpose SSD (older)16,000Legacy โ€” prefer gp3 (cheaper + better baseline IOPS)
io1 / io2Provisioned IOPS SSD64,000+High-performance databases (MySQL, Oracle, SQL Server)
st1Throughput Optimized HDD500Big data, log processing, data warehouses (sequential access)
sc1Cold HDD250Infrequently 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).

Instance Store Core

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
Snapshots In-Depth

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
Storage Comparison Core
FeatureEBSInstance Store
PersistenceYes โ€” survives stopNo โ€” lost on stop
PerformanceMedium to HighVery High (local NVMe)
DetachableYesNo
Snapshot supportYesNo
Use caseDatabases, boot volumesCaches, temporary data
๐Ÿ‘‰ Key Takeaway

Storage choice impacts durability, performance, and cost of your EC2 workloads

05
Chapter Five

Scaling & High Availability

Why Scaling Matters Core

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
Launch Templates In-Depth

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 (ASG) In-Depth

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
Scaling Policies In-Depth
Policy TypeHow It WorksBest For
Dynamic ScalingReact to metrics (CPU > 70%, request count)Variable traffic patterns
Scheduled ScalingScale at fixed times (e.g., 9am Mโ€“F)Predictable daily/weekly patterns
Predictive ScalingML-based forecasting from historical dataRecurring spikes with lead time
Load Balancing In-Depth

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).

High Availability (Multi-AZ) Core

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
Health Checks & Self-Healing Core
  • 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
Architecture Flow In-Depth
Multi-AZ EC2 with ALB & Auto Scaling Group
AUTO SCALING GROUP AVAILABILITY ZONE โ€” A EC2 Instance 1 EC2 Instance 2 EBS EBS AVAILABILITY ZONE โ€” B EC2 Instance 3 EC2 Instance 4 EBS EBS ALB Load Balancer ๐Ÿ‘ค USERS
๐Ÿ‘‰ Key Takeaway

Scalable systems are built using Auto Scaling + Load Balancing + Multi-AZ design

06
Chapter Six

Cost & Optimization

Pricing Models In-Depth

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 In-Depth

Savings Plans are a flexible pricing model that offers similar discounts to Reserved Instances but with greater flexibility across services and regions.

FeatureReserved InstancesSavings Plans
CommitmentSpecific instance type + regionHourly spend commitment (e.g., $10/hr)
FlexibilityLow โ€” locked to instance familyHigh โ€” applies across EC2, Lambda, Fargate
DiscountUp to 72%Up to 66%
Best forStable, known instance typesMixed 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.

Cost Optimization Strategies Core
  • 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
Performance Optimization Core
  • 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
Monitoring Core

Track key metrics to optimize both cost and performance:

MetricWhat It Tells YouAction
CPU UtilizationIs the instance over/under-provisioned?Right-size or scale
Network In/OutTraffic patterns and throughput needsUpgrade instance or add ELB
Disk I/OStorage bottlenecksSwitch EBS type or add IOPS
Status ChecksHardware/software failuresAuto-replace via ASG
๐Ÿ‘‰ Key Takeaway

Efficient EC2 usage balances performance needs with cost optimization strategies

07
Chapter Seven

Architecture Patterns

Pattern 1 โ€” Single Instance Introductory
๐Ÿ–ฅ๏ธ

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
Pattern 2 โ€” High Availability Web App In-Depth
๐Ÿ—๏ธ

Architecture

  • Application Load Balancer
  • Multiple EC2 instances
  • Auto Scaling Group
  • Multi-AZ deployment
โœ…

Benefits

  • Fault tolerance
  • Automatic scaling
  • High availability
  • Self-healing
Pattern 3 โ€” Scalable Production System In-Depth

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
Design Best Practices Core
  • 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
Security Best Practices Core
๐Ÿ”’

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/0 on port 22
Common Mistakes Introductory
MistakeWhy It's BadFix
Single instance in productionOne failure = total downtimeUse ASG + Multi-AZ
Not using Auto ScalingOver-pay or under-serveDefine scaling policies
Ignoring cost optimization10x overspend vs. right-sizedMonitor + right-size + RI/Spot
Poor security configOpen ports = attack surfaceLeast-privilege SGs + private subnets
No monitoringBlind to failures and wasteCloudWatch + alarms + dashboards
๐Ÿ‘‰ Key Takeaway

EC2 becomes powerful when combined with scaling, networking, and architecture patterns โ€” not when used alone