Back to: AWS-Basics-Advanced
What is Amazon EC2 (Elastic Compute Cloud)?
In cloud computing, Amazon EC2 (Elastic Compute Cloud) is one of the most important and widely used services on AWS.
Think of EC2 as your virtual server in the cloud. Instead of buying and managing physical computers, AWS allows you to rent virtual machines (instances) on-demand. You can choose the amount of CPU, memory, storage, and operating system based on your requirements.
EC2 makes it possible to run applications, host websites, process big data, or deploy AI models — all with full control, scalability, and pay-as-you-go pricing.
Why Use EC2?
-
⚡ Scalable – You can scale instances up or down anytime.
-
💸 Cost-Effective – Pay only for the compute you use.
-
🔒 Secure – Integrated with IAM roles, firewalls, and encryption.
-
🌍 Global – Launch instances in any AWS region or availability zone.
-
🧠Customizable – Choose OS (Linux, Windows), instance size, and storage type.
Main Components of EC2
Component | Description |
---|---|
Amazon Machine Image (AMI) | A template that defines the OS, application server, and software used to launch an instance. |
Instance Type | Specifies the CPU, memory, and network capacity (e.g., t2.micro, m5.large). |
EBS Volume (Elastic Block Store) | Persistent storage attached to your instance (like a virtual hard drive). |
Key Pair | Used for secure login (SSH or RDP). Includes a public and private key. |
Security Groups | Acts as a virtual firewall — controls inbound and outbound traffic to your EC2 instance. |
Elastic IP | A static public IP address that can be attached to your instance. |
VPC (Virtual Private Cloud) | The private network in which your EC2 instance runs. |
User Data | Optional script that runs automatically when your instance launches (for auto-installing software, etc.). |
Elastic Load Balancer (ELB) | Distributes traffic evenly across multiple EC2 instances. |
Auto Scaling Group (ASG) | Automatically adjusts the number of running EC2 instances based on load or demand. |
Steps to Create an EC2 Instance (Step-by-Step Guide)
Follow these simple steps to create your first EC2 instance in the AWS Management Console 👇
Step 1: Log in to AWS Console
Go to AWS Management Console and log in to your account.
Step 2: Open EC2 Dashboard
Search for “EC2” in the AWS console search bar and click on EC2 under “Compute Services”.
Step 3: Launch an Instance
Click on “Launch Instance” button.
You’ll now configure your virtual machine.
Step 4: Choose a Name and Tag
Give your instance a name — for example:
Name: MyWebServer
Tags help organize your resources in AWS.
Step 5: Choose an Amazon Machine Image (AMI)
Select the operating system you want:
-
Amazon Linux 2 (default and free tier eligible)
-
Ubuntu
-
Windows Server
Step 6: Choose Instance Type
Select your desired instance type.
For example:
-
t2.micro – Free tier eligible (1 vCPU, 1GB RAM)
-
t3.medium – For higher workloads
Step 7: Configure Key Pair (Login)
Choose Create New Key Pair → Download the .pem
file (for Linux) or .ppk
file (for Windows).
This key will be used to SSH or RDP into your instance securely.
Step 8: Configure Network Settings
Choose:
-
VPC: Default or custom
-
Subnet: Choose any availability zone
-
Auto-assign Public IP: Enable
-
Security Group: Create a new one and allow:
-
SSH (port 22) for Linux
-
RDP (port 3389) for Windows
-
HTTP (port 80) if you’re hosting a website
-
Step 9: Configure Storage
By default, you’ll get an 8 GB EBS volume. You can increase it based on your needs.
Step 10: Launch Instance
Click Launch Instance — AWS will start creating your EC2 instance.
You can check the status under EC2 → Instances → Instance State.
Step 11: Connect to Instance
Once your instance is running, connect to it:
-
For Linux:
Use the following SSH command from your terminal:
EC2 Instance Pricing Models — Overview
AWS offers multiple pricing models for EC2 so you can choose the right balance between cost, flexibility, and commitment.
The main models are:
Pricing Model | Description | Best For | Cost Efficiency |
---|---|---|---|
On-Demand Instances | Pay per hour/second for compute with no long-term commitment. | Short-term, unpredictable workloads. | đź’° Moderate |
Reserved Instances (RI) | Commit to a specific instance type for 1 or 3 years to get discounts. | Steady-state workloads. | đź’°đź’° High |
Savings Plans | Flexible alternative to RIs; commit to a consistent spend ($/hour). | Long-term, consistent usage. | đź’°đź’° High |
Spot Instances | Bid for unused EC2 capacity at up to 90% discount. | Fault-tolerant, flexible workloads (batch, ML, CI/CD). | đź’°đź’°đź’° Very High |
Dedicated Hosts/Instances | Physically isolated servers for compliance or licensing needs. | Regulated industries or BYOL (bring your own license). | đź’° High |
Capacity Reservations | Reserve capacity in a specific AZ for immediate availability. | Mission-critical workloads needing guaranteed capacity. | đź’° Moderate |
1. On-Demand Instances
Description
-
Pay only for compute time you use (per second or per hour).
-
No upfront cost, no long-term commitment.
 Example Use Case
-
Development, testing, or short-term projects.
-
Startups experimenting with workloads.
 Example
-
You run a t2.micro instance for 10 hours/day → pay only for those 10 hours.
2. Reserved Instances (RI)
 Description
-
Commit to a specific instance type, region, and OS for 1 or 3 years.
-
Get up to 75% discount compared to On-Demand.
 Types of RIs
Type | Flexibility | Description |
---|---|---|
Standard RI | ❌ Fixed | Best discounts, fixed instance type. |
Convertible RI | âś… Flexible | Change instance type/family if needed. |
Scheduled RI | ⏰ Time-based | Run only during specific time windows. |
 Example Use Case
-
Long-term applications like databases, backend servers.
3. Savings Plans
 Description
-
Flexible alternative to RIs — commit to spend $/hour for 1 or 3 years.
-
Works across instance families, sizes, regions.
 Types of Savings Plans
Plan Type | Flexibility | Description |
---|---|---|
Compute Savings Plan | âś… Most Flexible | Apply to any instance, region, or even Lambda/Fargate. |
EC2 Instance Savings Plan | ⚙️ Moderate | Locked to an instance family in a region. |
 Example Use Case
-
Enterprises with predictable cloud spend but dynamic workloads.
4. Spot Instances
 Description
-
Purchase unused EC2 capacity at up to 90% discount.
-
AWS can terminate with a 2-minute notice when capacity is needed.
Example Use Case
-
Big data processing, CI/CD pipelines, machine learning training, simulations.
 Example
-
Launch 50 Spot instances for a batch job → if AWS needs capacity back, instances stop — you can resume later.
5. Dedicated Hosts / Instances
 Description
-
Dedicated Instances: Run on hardware dedicated to you.
-
Dedicated Hosts: Gives control of physical servers for license management.
 Example Use Case
-
Compliance (HIPAA, PCI), software licenses tied to physical hardware.
6. Capacity Reservations
 Description
-
Reserve capacity in a specific Availability Zone.
-
Combine with RIs or Savings Plans for discounts.
 Example Use Case
-
Critical applications that must launch anytime (e.g., payment systems).
Amazon EC2 Instance Types — Based on Use Case
Amazon EC2 offers a wide range of instance families, each optimized for a specific purpose like compute, memory, storage, or GPU-based workloads.
1. General Purpose Instances
Description
Balanced mix of compute, memory, and networking — ideal for most workloads.
 Best For
-
Web servers
-
Development and test environments
-
Small to medium databases
 Examples
Family | Example Types | Highlights |
---|---|---|
t-series | t3, t4g, t2 | Burst performance, cost-effective for low-traffic apps. |
m-series | m6i, m5, m7g | Balanced for compute & memory. Great for app servers. |
Example Use Case
A startup runs its Node.js web app on an m5.large instance for balanced cost and performance.
2. Compute Optimized Instances
Description
Provide high-performance processors for compute-intensive tasks.
 Best For
-
High-performance web servers
-
Batch processing
-
Scientific modeling
-
Gaming servers
 Examples
Family | Example Types | Highlights |
---|---|---|
c-series | c6i, c7g, c5 | High compute-to-memory ratio, ideal for CPU-heavy apps. |
 Example Use Case
A company runs 3D rendering and high-performance APIs using c6i.xlarge instances.
3. Memory Optimized Instances
 Description
Designed for memory-intensive workloads needing fast performance for large datasets.
 Best For
-
In-memory databases (Redis, Memcached)
-
Real-time big data analytics
-
High-performance computing (HPC) with large data sets
 Examples
Family | Example Types | Highlights |
---|---|---|
r-series | r6g, r6i, r7i | High memory per vCPU, ideal for databases. |
x-series | x2idn, x2iedn | Very large memory footprint, suitable for SAP HANA. |
u-series | u-6tb1.metal | Ultra-high memory for enterprise-grade workloads. |
 Example Use Case
A financial analytics company runs SAP HANA on x2idn.32xlarge for in-memory data processing.
4. Storage Optimized Instances
Description
Designed for high, sequential read/write access to large datasets on local storage (NVMe/SSD).
Best For
-
NoSQL databases (Cassandra, MongoDB)
-
Data warehousing
-
Big data processing
-
Log or file storage
 Examples
Family | Example Types | Highlights |
---|---|---|
i-series | i3, i4i | High IOPS SSD storage. |
d-series | d3, d2 | High HDD storage capacity. |
h-series | h1 | Optimized for throughput-heavy workloads. |
 Example Use Case
An analytics platform processes Petabytes of log data using i4i.4xlarge instances for high disk I/O.
5. Accelerated Computing Instances (GPU & FPGA)
 Description
Provide GPU or FPGA hardware acceleration for demanding compute workloads.
 Best For
-
Machine Learning & Deep Learning
-
Video rendering & encoding
-
Scientific simulations
-
Game streaming
Examples
Family | Example Types | Highlights |
---|---|---|
p-series | p4, p5 | GPU-optimized for deep learning. |
g-series | g5, g6 | Graphics and ML inference workloads. |
inf-series | inf1, inf2 | AWS Inferentia chips for ML inference. |
f-series | f1 | Custom FPGA acceleration. |
🚀 Example Use Case
A research lab trains a Computer Vision model on p4d.24xlarge GPUs.
6. High Performance Computing (HPC) Instances
Description
Built for massively parallel, high-speed computing tasks that require strong CPU/GPU interconnects.
 Best For
-
Computational fluid dynamics (CFD)
-
Genomics
-
Weather forecasting
-
Seismic modeling
 Examples
Family | Example Types | Highlights |
---|---|---|
hpc-series | hpc6a, hpc7g | High networking bandwidth (up to 200 Gbps). |
Example Use Case
A research institute runs climate modeling simulations using hpc7g instances.
7. Arm-based Instances (Graviton)
 Description
Use AWS Graviton processors (ARM-based) — lower cost and power-efficient.
 Best For
-
Web servers, containers, and microservices
-
Open-source databases
-
Cloud-native applications
 Examples
Family | Example Types | Highlights |
---|---|---|
t4g, m7g, c7g, r7g | Graviton3-based | Up to 40% better price-performance ratio. |
Example Use Case
A SaaS startup migrates microservices to t4g.medium instances to save 30% on costs.
Quick Reference Table
Category | Instance Families | Key Feature | Example Use Case |
---|---|---|---|
General Purpose | t, m | Balanced | Web servers, dev/test |
Compute Optimized | c | High CPU | Gaming, APIs, HPC |
Memory Optimized | r, x, u | High RAM | Databases, analytics |
Storage Optimized | i, d, h | Fast I/O | Data lakes, NoSQL |
Accelerated (GPU) | p, g, inf, f | GPU/FPGA | ML, rendering |
High Performance Computing | hpc | Parallel compute | Scientific modeling |
Graviton (ARM) | t4g, m7g, r7g | Cost-efficient | Containers, web apps |
How to Choose the Right Instance Type
-
Identify workload type (compute, memory, storage, GPU).
-
Decide performance needs (throughput, latency).
-
Estimate budget (On-Demand vs Savings Plans).
-
Start small and scale using Auto Scaling Groups.
-
Benchmark performance before production deployment.
#!/bin/bash
# Update packages
yum update -y
# Install Apache
yum install httpd -y
# Start Apache service
systemctl start httpd
systemctl enable httpd
# Fetch instance details
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
PUBLIC_IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
AVAIL_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)