AWS Builder Center
AWS EC2: A Complete Beginner-Friendly Guide

AWS EC2: A Complete Beginner-Friendly Guide

AWS EC2 is a flexible cloud computing service that lets you launch and manage virtual servers on demand. Learn how EC2 works, including instance types, AMIs, storage, networking, security, pricing, scaling, and real-world DevOps use cases.

Cloud & DevOps learner
Introduction
In the world of cloud computing, businesses need servers to host websites, applications, APIs, databases, and other services. Traditionally, companies had to purchase physical servers, install operating systems, maintain hardware, and manage networking infrastructure.
Amazon EC2 (Elastic Compute Cloud) solves this problem by providing virtual servers in the cloud. Instead of purchasing a physical server, you can create a virtual server within minutes and pay for the resources you use.
AWS EC2 is one of the most widely used services in Amazon Web Services (AWS) and is a fundamental service for anyone learning Cloud Computing, DevOps, System Administration, or Cybersecurity.



What is AWS EC2?

Amazon Elastic Compute Cloud (EC2) is an AWS service that provides resizable compute capacity in the cloud.
In simple terms:
EC2 is a virtual computer running inside AWS's cloud infrastructure.
You can create an EC2 instance, choose its CPU, RAM, storage, operating system, networking configuration, and security settings, and then use it similarly to a physical server.
For example, you can create an EC2 server running:
  • Ubuntu
  • Amazon Linux
  • Windows Server
  • Red Hat Enterprise Linux
  • SUSE Linux
  • Other supported operating systems
After launching the instance, you can connect to it and install software such as:
  • Nginx
  • Apache
  • Docker
  • Kubernetes tools
  • Python
  • Node.js
  • Java
  • Databases
  • Monitoring agents
  • Security tools



Why is EC2 Called "Elastic"?

The word Elastic is important.
Traditional physical servers are difficult to scale. If your application suddenly receives millions of users, you may need to purchase additional hardware.
With EC2, you can increase or decrease computing resources according to your requirements.
For example:
Low Traffic
↓
2 EC2 Instances
↓
Traffic increases
↓
5 EC2 Instances
↓
Traffic decreases
↓
2 EC2 Instances

This ability to adjust infrastructure according to demand is one of the major advantages of cloud computing.



How Does EC2 Work?

A simplified EC2 architecture looks like this:
Internet
│
▼
┌─────────────┐
│ AWS │
│ Region │
└──────┬──────┘
│
▼
┌─────────────┐
│ VPC │
│ Network │
└──────┬──────┘
│
┌────────┴────────┐
│ │
▼ ▼
Public Subnet Private Subnet
│
▼
┌─────────────┐
│ EC2 Instance│
│ │
│ CPU │
│ RAM │
│ OS │
│ Application │
└─────────────┘

An EC2 instance normally exists inside an Amazon VPC (Virtual Private Cloud).
The VPC provides networking capabilities such as:
  • IP addresses
  • Subnets
  • Route tables
  • Internet connectivity
  • Security controls



Important EC2 Components

When working with EC2, you will encounter several important concepts.

1. AMI — Amazon Machine Image

An AMI (Amazon Machine Image) is a template used to launch an EC2 instance.
It can contain:
  • Operating system
  • Pre-installed software
  • Configuration
  • Application files
For example:
AMI
│
├── Ubuntu
├── Nginx
├── Python
└── Application configuration
│
▼
EC2 Instance

You can use an existing AWS-provided AMI or create your own custom AMI.



2. EC2 Instance

An instance is the actual virtual server created from an AMI.
For example:
Ubuntu AMI
│
▼
EC2 Instance
│
├── 2 vCPU
├── 4 GB RAM
├── 30 GB Storage
└── Ubuntu Linux

You can launch multiple instances from the same AMI.



3. Instance Types

EC2 provides many different instance types optimized for different workloads.
An instance type determines resources such as:
  • CPU
  • Memory
  • Network performance
  • Storage capabilities
Common categories include:
Category
Purpose
General Purpose
Web servers and common applications
Compute Optimized
CPU-intensive workloads
Memory Optimized
Large-memory applications
Storage Optimized
High-performance storage workloads
Accelerated Computing
GPU/ML workloads
For example, a small development server doesn't need the same resources as a large machine-learning workload.



4. EBS — Elastic Block Store

EC2 instances commonly use Amazon EBS (Elastic Block Store) for persistent block storage.
Think of EBS as the virtual hard drive attached to your EC2 instance.
EC2 Instance
┌───────────────┐
│ CPU + RAM │
│ Ubuntu │
│ Application │
└───────┬───────┘
│
▼
┌─────────┐
│ EBS │
│ 50 GB │
└─────────┘

EBS volumes can persist independently of the lifecycle of an EC2 instance, depending on configuration.



5. Security Groups

A Security Group acts as a virtual firewall for your EC2 instance.
For example, suppose you have a web server.
You might allow:
HTTP → Port 80
HTTPS → Port 443
SSH → Port 22

Example:
Internet
│
├── Port 80 ──► ALLOW
├── Port 443 ──► ALLOW
└── Port 22 ──► Restricted
│
▼
EC2

A good security practice is to avoid exposing SSH/RDP to the entire internet unless there is a specific reason.



6. Key Pair

When launching an EC2 instance, you may create or select a key pair for secure login.
For Linux instances, SSH authentication commonly uses a private key.
Example:
ssh -i my-key.pem ubuntu@SERVER_IP

The private key should be protected carefully.
Never share your private SSH key publicly.



7. Elastic IP

An EC2 instance can have a public IP address.
However, public IP addresses can change when an instance is stopped and started, depending on the configuration.
An Elastic IP address is a static public IPv4 address that you can associate with AWS resources such as an EC2 instance.
This can be useful when an application requires a stable public IP.



Launching an EC2 Instance

A basic EC2 launch process looks like this:
1. Open AWS Console
↓
2. Select EC2
↓
3. Choose AMI
↓
4. Select Instance Type
↓
5. Configure Network
↓
6. Configure Security Group
↓
7. Configure Storage
↓
8. Select Key Pair
↓
9. Launch Instance
↓
10. Connect to Server

After launching, you can connect to your server using SSH for Linux or RDP for Windows.



Example: Hosting a Website on EC2

Suppose you want to host a website.
You could create the following architecture:
Users
│
▼
Internet
│
▼
┌──────────────┐
│ ALB │
│ Load Balancer│
└──────┬───────┘
│
┌───────┴───────┐
│ │
▼ ▼
EC2 #1 EC2 #2
Web Server Web Server
│ │
└───────┬───────┘
│
▼
Database

This architecture provides better scalability and availability than running the application on a single server.



EC2 and Auto Scaling

One of the powerful features of AWS is Auto Scaling.
Imagine your website normally needs two servers:
Normal Traffic

EC2 #1
EC2 #2

During a major traffic spike:
High Traffic

EC2 #1
EC2 #2
EC2 #3
EC2 #4
EC2 #5

When traffic decreases, unnecessary instances can be terminated:
Low Traffic

EC2 #1
EC2 #2

This can help applications maintain performance while controlling infrastructure costs.



EC2 Pricing

EC2 uses different pricing models depending on the workload and purchasing option.
Some major options include:

On-Demand

You pay for compute capacity without a long-term commitment.
Good for:
  • Testing
  • Development
  • Unpredictable workloads

Reserved Instances

Useful when you have predictable long-term workloads and want potential discounts in exchange for a commitment.

Savings Plans

Offer pricing savings in exchange for committing to a certain amount of AWS compute usage over a specified term.

Spot Instances

Spot Instances use spare AWS compute capacity and can be significantly cheaper, but AWS can interrupt them when the capacity is needed.
They are useful for workloads that can tolerate interruption, such as certain:
  • Batch jobs
  • Data processing
  • CI workloads
  • Research workloads



EC2 vs Traditional Server

Feature
Traditional Server
EC2
Hardware Purchase
Required
Not required
Provisioning
Can take days/weeks
Usually minutes
Scaling
Difficult
Easier
Maintenance
Hardware + software
Primarily software/configuration
Location
Physical data center
AWS infrastructure
Cost Model
Capital expenditure
Usage/commitment based
Automation
Limited
Highly automatable



EC2 in DevOps

EC2 is extremely important in DevOps.
A typical DevOps environment might look like:
Developer
│
▼
GitHub
│
▼
CI/CD Pipeline
│
▼
Docker Image
│
▼
Container Registry
│
▼
EC2 / Kubernetes
│
▼
Application
│
▼
Monitoring

EC2 can be used for:
  • Application servers
  • CI/CD runners
  • Docker hosts
  • Self-hosted GitHub Actions runners
  • Monitoring systems
  • Bastion hosts
  • Development environments



EC2 Security Best Practices

Security should be considered from the beginning.

1. Restrict SSH Access

Avoid:
0.0.0.0/0 → Port 22

when possible.
Instead, restrict SSH access to trusted IP addresses or use more secure management mechanisms.

2. Use IAM Roles

Instead of storing AWS access keys on servers, use IAM Roles whenever possible.

3. Keep the OS Updated

Regularly apply:
  • Security patches
  • Operating system updates
  • Application updates

4. Encrypt Storage

Use EBS encryption where appropriate.

5. Monitor the Instance

Use services and tools such as:
  • Amazon CloudWatch
  • AWS CloudTrail
  • AWS Systems Manager
  • Security monitoring tools

6. Follow Least Privilege

Give users and applications only the permissions they actually need.



EC2 vs Lambda

EC2 and Lambda are both AWS compute services, but they work differently.
EC2
Lambda
Virtual server
Serverless compute
You manage the OS
AWS manages infrastructure
Long-running workloads
Event-driven functions
More control
Less infrastructure management
Can run arbitrary software
Function-based execution
Suitable for many server workloads
Great for event-driven applications
For example:
EC2:
Application → Virtual Server → Operating System

Lambda:
Event → Function → Result




EC2 vs Containers

EC2 provides the underlying virtual machine.
Containers such as Docker run applications in isolated environments.
For example:
AWS
│
└── EC2
│
└── Docker
│
├── Frontend Container
├── Backend Container
└── Worker Container

For larger containerized environments, AWS services such as Amazon ECS and Amazon EKS can be used.



Real-World EC2 Architecture

A more production-oriented architecture could look like:
Users
│
▼
CloudFront
│
▼
Application Load
Balancer
│
┌─────────────┴─────────────┐
│ │
▼ ▼
EC2 #1 EC2 #2
Application Application
│ │
└─────────────┬─────────────┘
│
▼
Cache
│
▼
Database

Monitoring → CloudWatch
Security → IAM + Security Groups
Logs → CloudWatch Logs

This type of architecture can provide:
  • Scalability
  • High availability
  • Better security
  • Load distribution
  • Monitoring
  • Fault tolerance



Advantages of AWS EC2

The major advantages include:
1. Flexibility You have significant control over the server environment.
2. Scalability You can launch additional instances or use Auto Scaling.
3. Wide Choice of Configurations Different instance families support different workloads.
4. Global Infrastructure You can deploy workloads across AWS Regions and Availability Zones.
5. Integration EC2 integrates with many AWS services.
6. Automation EC2 infrastructure can be managed through APIs, CLI, Infrastructure as Code, and CI/CD pipelines.



Disadvantages of EC2

EC2 also comes with responsibilities.
You may need to manage:
  • Operating system updates
  • Server configuration
  • Application deployment
  • Security hardening
  • Monitoring
  • Capacity planning
  • Networking
  • Instance lifecycle
This is why managed services and serverless services can sometimes be preferable when you don't need direct server control.



Common EC2 Commands

After connecting to a Linux EC2 instance, you may use commands such as:
# Check operating system
uname -a

# Check disk usage
df -h

# Check memory
free -h

# Check CPU/processes
top

# Update packages
sudo apt update

# Install Nginx
sudo apt install nginx

For an Amazon Linux system, package-management commands may differ.



EC2: The Big Picture

The easiest way to understand EC2 is to think of it as:
AWS EC2
│
┌───────────┼───────────┐
│ │ │
▼ ▼ ▼
CPU RAM Storage
│ │ │
└───────────┼───────────┘
│
▼
Operating System
│
▼
Application
│
▼
Users

But in a real AWS environment, EC2 works together with networking, security, storage, load balancing, monitoring, identity, and automation services.



Conclusion

Amazon EC2 is one of the foundational services of AWS. It provides virtual computing resources that can be used to run websites, APIs, applications, containers, development environments, and many other workloads.
To become comfortable with EC2, don't just learn how to click "Launch Instance." Learn the surrounding concepts:
EC2
│
├── AMI
├── Instance Types
├── EBS
├── Security Groups
├── Key Pairs
├── Elastic IP
├── VPC
├── Subnets
├── IAM Roles
├── Load Balancer
├── Auto Scaling
├── CloudWatch
└── Systems Manager

Once you understand these concepts, EC2 becomes much easier to use—and you'll have a strong foundation for learning AWS, Cloud Engineering, DevOps, and Cybersecurity.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article