Complete Guide to Amazon EC2 (Elastic Compute Cloud)


🚀 Complete Guide to Amazon EC2 (Elastic Compute Cloud)

A Detailed Blog with Hands-on Lab Exercises


📌 What is Amazon EC2?

Amazon EC2 (Elastic Compute Cloud) is a core service of Amazon Web Services that provides scalable virtual servers in the cloud. It allows you to launch, configure, and manage virtual machines (called instances) based on your workload requirements.

EC2 gives you:

  • Resizable compute capacity
  • Pay-as-you-go pricing
  • Multiple operating systems
  • Secure and isolated environments
  • Integration with other AWS services

🔎 Key Concepts of EC2

Instance

A virtual server running in AWS cloud.

AMI (Amazon Machine Image)

A pre-configured template that contains:

  • OS
  • Application server
  • Applications

Instance Types

Different combinations of:

  • CPU
  • Memory
  • Storage
  • Networking

Examples:

  • General Purpose (t3, t4g)
  • Compute Optimized (c5)
  • Memory Optimized (r5)

EBS (Elastic Block Store)

Persistent storage attached to EC2.

Security Groups

Virtual firewall controlling inbound and outbound traffic.

Key Pair

Used for secure SSH login.


🏗 EC2 Architecture Overview

User → Internet → Security Group → EC2 Instance → EBS Storage
EC2 runs inside a VPC (Virtual Private Cloud).


💰 EC2 Pricing Models

  • On-Demand
  • Reserved Instances
  • Spot Instances
  • Savings Plans

🧪 HANDS-ON LABS (Step-by-Step Practical)


🔬 Lab 1: Launch Your First EC2 Instance (Linux)

🎯 Objective:

Launch a Linux EC2 instance and connect via SSH.


📝 Step 1: Login to AWS Console

Go to:
👉 https://console.aws.amazon.com

Search for:
EC2


📝 Step 2: Launch Instance

  1. Click Launch Instance
  2. Enter Name:
  3. MyFirstEC2
  4. Choose AMI:
    • Amazon Linux 2
  5. Choose Instance Type:
    • t2.micro (Free Tier eligible)
  6. Create Key Pair:
    • Name: MyKeyPair
    • Download .pem file
  7. Network Settings:
    • Allow SSH (Port 22)
  8. Storage:
    • 8GB (default)
  9. Click Launch Instance

📝 Step 3: Connect to EC2 (Windows)

Using Git Bash or Terminal:

chmod 400 MyKeyPair.pem

 

ssh -i MyKeyPair.pem ec2-user@<Public-IP>

You are now connected 🎉


🔬 Lab 2: Install Apache Web Server

🎯 Objective:

Host a website on EC2.


Step 1: Install Apache

Inside EC2:

sudo yum update -y

sudo yum install httpd -y

sudo systemctl start httpd

sudo systemctl enable httpd


Step 2: Create Web Page

cd /var/www/html

sudo nano index.html

Add:

<h1>Welcome to My EC2 Server</h1>

Save and exit.


Step 3: Allow HTTP Traffic

  1. Go to EC2 → Security Groups
  2. Edit inbound rules
  3. Add:
    • Type: HTTP
    • Port: 80
    • Source: Anywhere

Step 4: Access Website

Open browser:

http://<Public-IP>

🎉 Website is live!


🔬 Lab 3: Create and Attach EBS Volume

🎯 Objective:

Add extra storage.


Step 1: Create Volume

  1. Go to EC2 → Elastic Block Store → Volumes
  2. Click Create Volume
  3. Size: 5GB
  4. AZ: Same as EC2
  5. Create

Step 2: Attach Volume

  1. Select volume
  2. Click Attach
  3. Choose instance

Step 3: Mount Volume

Inside EC2:

lsblk

sudo mkfs -t ext4 /dev/xvdf

sudo mkdir /data

sudo mount /dev/xvdf /data

Now storage is mounted.


🔬 Lab 4: Create Custom AMI

🎯 Objective:

Create backup image of your instance.

  1. Select EC2 instance
  2. Actions → Image → Create Image
  3. Name: MyCustomAMI
  4. Create

Now you can launch identical instances anytime.


🔬 Lab 5: Stop vs Terminate Instance

Stop

  • Instance stops
  • EBS remains
  • Can restart

Terminate

  • Instance deleted permanently
  • Data lost (unless backed up)

🔐 Best Practices for EC2

  • Use IAM Roles instead of access keys
  • Restrict Security Group ports
  • Enable monitoring
  • Use Auto Scaling for production
  • Backup using AMI or snapshots

📊 Real-World Use Cases

  • Hosting websites
  • Application servers
  • Dev/Test environments
  • Big data processing
  • Machine Learning workloads

📈 Advanced Topics to Explore

  • Auto Scaling Groups
  • Load Balancer
  • CloudWatch Monitoring
  • IAM Roles
  • VPC Networking

🏁 Conclusion

Amazon EC2 is one of the most powerful and flexible compute services in the cloud. It provides complete control over your infrastructure while offering scalability, security, and cost optimization.

By completing the above labs, you now know how to:

Launch EC2
Connect securely
Host a website
Add storage
Create AMIs


If you'd like, I can also provide:

  • Interview Questions on EC2
  • Architecture Diagrams
  • Terraform automation for EC2
  • HTML formatted version of this blog
  • PowerPoint training slides

Just tell me what you need next 🚀

 

No comments:

Post a Comment