Elastic Compute Cloud β virtual servers on demand. The most-used AWS service. Complete beginner-friendly guide for 1st semester students.
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.
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.
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)
Good for most applications: web servers, small databases, development environments.
Heavy computation: video encoding, gaming servers, scientific modeling, batch processing.
In-memory databases, real-time big data processing, large cache workloads (Redis).
NoSQL databases (Cassandra), data warehouses, distributed file systems.
Machine learning training, deep learning, graphics rendering, video transcoding.
1 vCPU, 1 GB RAM. Free for 750 hours/month for 12 months. Perfect for learning.
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).
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.
AWS's own Linux. Free, optimized for AWS, updated regularly. Best for most labs.
Popular open-source Linux. Familiar to most developers. Great for web servers.
Microsoft Windows. For .NET apps, IIS web servers. Costs more (Windows license fee).
You build your own. Install your software, configure everything, save as AMI. Launch 100 identical servers in seconds.
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.
You download this once. Keep it safe β AWS never stores it. If you lose it, you lose SSH access.
AWS places this on your EC2 instance. Acts like a lock. Only your private key can open it.
ssh -i "my-key.pem" ec2-user@<PUBLIC_IP>
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.
"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 Rule | Port | Use Case |
|---|---|---|
| SSH | 22 | Admin/developer login to Linux servers |
| RDP | 3389 | Admin login to Windows servers |
| HTTP | 80 | Web traffic (website accessible over internet) |
| HTTPS | 443 | Secure web traffic (SSL/TLS) |
| MySQL/Aurora | 3306 | Database connections |
| Custom TCP | Any | Your application's specific port |
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.
An EC2 instance goes through different states during its life. Understanding these states is critical for cost management.
Starting up
β Active β billed per second
Shutting down
Off β not billed for compute
β οΈ Deleted forever
| State | Compute Billed? | EBS Billed? | Data Saved? |
|---|---|---|---|
| Running | β Yes | β Yes | Yes |
| Stopped | β No | β Yes | Yes |
| Terminated | β No | β No (deleted) | β Lost forever |
AWS offers 4 ways to pay for EC2. Choosing the right one can save up to 90% of your compute costs.
No commitment. Start/stop anytime. Most expensive per hour. Best for: testing, unpredictable workloads.
Up to 72% discount vs On-Demand. You commit to using a specific instance type. Best for: steady, predictable workloads.
Up to 90% discount. AWS can terminate with 2-min notice. Best for: batch jobs, CI/CD, fault-tolerant workloads.
Entire physical machine reserved. Required for some software licenses (Oracle, Windows Server). Most expensive.
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.
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.
#!/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
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.
| Concept | AWS EC2 | Azure |
|---|---|---|
| Virtual Machine | EC2 Instance | Azure Virtual Machine |
| OS Image | AMI (Amazon Machine Image) | Azure Marketplace Image |
| Instance Size | t2.micro, m5.large, c5.xlarge⦠| Standard_B1s, Standard_D2s_v3⦠|
| Instance-level Firewall | Security Group (stateful) | Network Security Group (stateful) |
| SSH Access | Key Pair (.pem) | SSH key or username/password |
| Startup Script | User Data | Custom Script Extension / cloud-init |
| Static Public IP | Elastic IP | Static Public IP Address |
| Disk Type | EBS (Elastic Block Store) | Managed Disk |
| Free Tier | t2.micro β 750 hrs/month | B1s β 750 hrs/month |
All links go to docs.aws.amazon.com β official AWS reference.
| Topic | AWS Docs URL |
|---|---|
| EC2 Overview | EC2/concepts.html |
| Instance Types | EC2/instance-types.html |
| AMI β Amazon Machine Images | EC2/AMIs.html |
| Key Pairs (SSH Access) | EC2/ec2-key-pairs.html |
| Security Groups | EC2/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 Addresses | EC2/elastic-ip-addresses-eip.html |