πŸ’» AWS Compute

Amazon EC2
Theory Notes

Elastic Compute Cloud β€” virtual servers on demand. The most-used AWS service. Complete beginner-friendly guide for 1st semester students.

1

What is Amazon EC2?

Before cloud computing, if you wanted to run a web application you had to buy physical servers β€” spend lakhs of rupees, wait weeks for delivery, set up a data center, hire staff to maintain it. And if traffic was low, those servers sat idle wasting money.


Amazon EC2 (Elastic Compute Cloud) solves this. It gives you virtual servers (called instances) in minutes, on demand, and you pay only for what you use. Need a server for 2 hours? Pay for 2 hours. Need 100 servers for 10 minutes? Done β€” and you pay only for those 10 minutes.

🏨 Analogy β€” Hotel vs Buying a House

Buying a physical server = buying a house outright.

Huge upfront cost. You own it permanently. Even when you're not home (server is idle), you still own it and pay maintenance. If your family grows (traffic spikes), you can't just add rooms overnight.


EC2 = booking a hotel room. Pay per night. If you need more rooms, book more. Check out when done β€” stop paying instantly. The hotel (AWS) manages electricity, cleaning, security. You just use the room.


Elastic in the name means you can scale up or down at any time β€” like being able to add or remove hotel rooms on demand.

2

EC2 Instance Types

EC2 instances come in many types β€” each optimized for different workloads. The naming pattern is: family + generation + size.


Example: t3.micro β†’ Family: t (general purpose) Β· Generation: 3 Β· Size: micro (smallest)

General Purpose

Balanced CPU + RAM

Good for most applications: web servers, small databases, development environments.

t2, t3, t3a, m5, m6i
Compute Optimized

High CPU power

Heavy computation: video encoding, gaming servers, scientific modeling, batch processing.

c5, c6i, c6g
Memory Optimized

Large RAM

In-memory databases, real-time big data processing, large cache workloads (Redis).

r5, r6i, x1e, z1d
Storage Optimized

High disk I/O

NoSQL databases (Cassandra), data warehouses, distributed file systems.

i3, i4i, d3, h1
Accelerated Computing

GPU / hardware accelerators

Machine learning training, deep learning, graphics rendering, video transcoding.

p3, p4, g4dn, inf1
Free Tier

t2.micro / t3.micro

1 vCPU, 1 GB RAM. Free for 750 hours/month for 12 months. Perfect for learning.

Always start here for labs!
Size Naming β€” Easy Rule
nano < micro < small < medium < large < xlarge < 2xlarge < 4xlarge...
Each step up roughly doubles the CPU and RAM. For all labs: always use t2.micro or t3.micro (free tier).
3

AMI β€” Amazon Machine Image

Before you can launch an EC2 instance, AWS needs to know what operating system and software to install on it. That template is called an AMI (Amazon Machine Image).

πŸ’Ώ Analogy β€” OS Installation DVD

AMI = pre-made OS installation image.

When you buy a new laptop, you need to install Windows or Linux. You use a DVD/USB image. AMI is the same thing for EC2 β€” it's the "disk image" that defines what the server looks like when it first boots. But unlike a DVD, an AMI can include your entire application stack already configured.

🐧

Amazon Linux 2023

AWS's own Linux. Free, optimized for AWS, updated regularly. Best for most labs.

🍊

Ubuntu

Popular open-source Linux. Familiar to most developers. Great for web servers.

πŸͺŸ

Windows Server

Microsoft Windows. For .NET apps, IIS web servers. Costs more (Windows license fee).

πŸ—οΈ

Custom AMI

You build your own. Install your software, configure everything, save as AMI. Launch 100 identical servers in seconds.

4

Key Pairs β€” SSH Access

EC2 instances don't have a password login by default. Instead, AWS uses a Key Pair β€” a pair of cryptographic keys that prove your identity.

πŸ”‘

Private Key (.pem file)

You download this once. Keep it safe β€” AWS never stores it. If you lose it, you lose SSH access.

πŸ”’

Public Key

AWS places this on your EC2 instance. Acts like a lock. Only your private key can open it.

How SSH Login Works
Your laptop has the private key (.pem). The EC2 instance has the matching public key. When you SSH, your laptop proves it has the private key β†’ EC2 lets you in. No password needed.
SSH Command
ssh -i "my-key.pem" ec2-user@<PUBLIC_IP>
5

Security Groups

A Security Group is a virtual firewall that controls what traffic can enter (inbound) and leave (outbound) your EC2 instance. Think of it as a whitelist β€” everything is blocked by default, you only allow what you specifically permit.

πŸͺ Analogy β€” Shop Entrance Rules

Security Group = rules for who can enter your shop.

"Only customers are allowed in through the front door (port 80). Staff can enter through the back door (port 22). No one else."

Inbound rules = who can come in. Outbound rules = who can go out. Default: all inbound blocked, all outbound allowed.

Common Inbound RulePortUse Case
SSH22Admin/developer login to Linux servers
RDP3389Admin login to Windows servers
HTTP80Web traffic (website accessible over internet)
HTTPS443Secure web traffic (SSL/TLS)
MySQL/Aurora3306Database connections
Custom TCPAnyYour application's specific port
Security Best Practice
Never open SSH (port 22) to 0.0.0.0/0 (the entire internet). Always restrict SSH to My IP β€” only your specific IP address can connect. Hackers constantly scan the internet for open port 22.
6

Instance States (Lifecycle)

An EC2 instance goes through different states during its life. Understanding these states is critical for cost management.

Pending

Starting up

β†’

Running

βœ… Active β€” billed per second

β†’

Stopping

Shutting down

β†’

Stopped

Off β€” not billed for compute

β†’

Terminated

⚠️ Deleted forever

StateCompute Billed?EBS Billed?Data Saved?
Runningβœ… Yesβœ… YesYes
Stopped❌ Noβœ… YesYes
Terminated❌ No❌ No (deleted)❌ Lost forever
⚠️ Critical β€” Terminate vs Stop
Stop = like shutting down your laptop. Data is preserved. You can start it again.
Terminate = like throwing your laptop in the bin. Instance + data are gone forever.

Always Stop instances after lab to save costs. Only Terminate when you truly want to delete.
7

EC2 Pricing Models

AWS offers 4 ways to pay for EC2. Choosing the right one can save up to 90% of your compute costs.

On-Demand

Pay per second/hour

No commitment. Start/stop anytime. Most expensive per hour. Best for: testing, unpredictable workloads.

Reserved Instances

1 or 3 year contract

Up to 72% discount vs On-Demand. You commit to using a specific instance type. Best for: steady, predictable workloads.

Spot Instances

Bid on spare capacity

Up to 90% discount. AWS can terminate with 2-min notice. Best for: batch jobs, CI/CD, fault-tolerant workloads.

Dedicated Hosts

Physical server for you

Entire physical machine reserved. Required for some software licenses (Oracle, Windows Server). Most expensive.

✈️ Analogy β€” Flight Tickets

Same seat, very different prices depending on how you book.

On-Demand = buying a ticket at the airport counter. Available right now, pay full price.
Reserved = booking 6 months ahead. Big discount because you committed.
Spot = last-minute unsold seats at huge discount β€” but the airline can cancel your seat if someone pays full price (AWS can terminate your instance).
Dedicated Host = chartering the entire plane for yourself.

8

User Data β€” Auto-Configure at Launch

User Data is a script you provide when launching an EC2 instance. It runs automatically when the instance first boots β€” this is called bootstrapping. No need to SSH in and manually install software.

User Data Script (Bash)
#!/bin/bash
# This runs automatically when EC2 starts for the first time

yum update -y                      # update all packages
yum install -y httpd               # install Apache web server
systemctl start httpd              # start the web server
systemctl enable httpd             # auto-start on reboot

# Write a test page
echo "<h1>Hello from EC2!</h1>" > /var/www/html/index.html
Why User Data Matters
With User Data, you can launch 100 EC2 instances β€” all with Apache, Node.js, or any software β€” automatically in minutes. No manual setup. This is the foundation of cloud automation.
9

Elastic IP β€” Fixed Public Address

By default, an EC2 instance gets a new public IP address every time it starts. If you stop and restart it, the IP changes β€” breaking any DNS records or bookmarks pointing to it.


An Elastic IP is a static, reserved public IP address that stays the same. You associate it with your instance β€” even if you stop and start it, the public IP never changes.

Cost Warning
Elastic IPs are free only when associated with a running instance. If you allocate one and don't use it (or associate it to a stopped instance), AWS charges you per hour. Always release Elastic IPs you don't need.
10

AWS EC2 vs Azure VMs

ConceptAWS EC2Azure
Virtual MachineEC2 InstanceAzure Virtual Machine
OS ImageAMI (Amazon Machine Image)Azure Marketplace Image
Instance Sizet2.micro, m5.large, c5.xlarge…Standard_B1s, Standard_D2s_v3…
Instance-level FirewallSecurity Group (stateful)Network Security Group (stateful)
SSH AccessKey Pair (.pem)SSH key or username/password
Startup ScriptUser DataCustom Script Extension / cloud-init
Static Public IPElastic IPStatic Public IP Address
Disk TypeEBS (Elastic Block Store)Managed Disk
Free Tiert2.micro β€” 750 hrs/monthB1s β€” 750 hrs/month
11

Key Summary

EC2 = virtual server AMI = OS template Key Pair = SSH login Security Group = firewall Stop β‰  Terminate t2.micro = free tier User Data = startup script Elastic IP = fixed IP Spot = cheapest (90% off)
AWS Official Docs

Official Documentation β€” EC2

All links go to docs.aws.amazon.com β€” official AWS reference.

Topic AWS Docs URL
EC2 OverviewEC2/concepts.html
Instance TypesEC2/instance-types.html
AMI β€” Amazon Machine ImagesEC2/AMIs.html
Key Pairs (SSH Access)EC2/ec2-key-pairs.html
Security GroupsEC2/ec2-security-groups.html
Pricing Models (On-Demand / Reserved / Spot)EC2/ec2-instance-purchasing-options.html
User Data (Bootstrap Scripts)EC2/user-data.html
Elastic IP AddressesEC2/elastic-ip-addresses-eip.html
Free Hands-On Labs AWS Skill Builder β€” free browser-based EC2 labs, no credit card needed. Also see the EC2 Pricing page for current free tier details.