AWS EC2 - Elastic Compute Cloud Explained

Amazon EC2 (Elastic Compute Cloud) is one of the most popular AWS services that provides virtual servers (called instances) in the cloud. You can launch Linux or Windows machines on demand, deploy your applications, and pay only for the compute power you use.

What is EC2?

EC2 is a cloud service that lets you create and manage virtual servers. These servers come with CPU, RAM, storage, and networking capabilities like a physical machine, but without owning any hardware. You can launch, scale, or delete instances within seconds.

AWS even provides free-tier eligible instances (like t2.micro or t3.micro) for the first 12 months, making it perfect for beginners.

Why use EC2 instead of physical servers?

  • No hardware purchase or maintenance
  • Launch servers in minutes
  • Scale up or down instantly
  • Pay only while the instance is running
  • Highly reliable and secure AWS infrastructure

Key EC2 Concepts: AMI, Instance Type, Key Pair, Security Group

1. AMI (Amazon Machine Image)

An AMI is like a template used to launch EC2 instances. It includes:

  • Operating system (Amazon Linux, Ubuntu, Windows)
  • Base packages
  • Pre-installed software

Choosing an AMI is like selecting which OS to install on a laptop.

2. Instance Types

Instance types define the hardware specifications of your virtual server:

  • vCPUs
  • RAM
  • Network performance

Examples (T2 family):

  • t2.nano – 1 vCPU, 0.5 GB RAM
  • t2.micro – 1 vCPU, 1 GB RAM (✔ Free Tier)
  • t2.small – 1 vCPU, 2 GB RAM
  • t2.medium – 2 vCPUs, 4 GB RAM

Other instance families:

  • C-series – Compute optimized
  • R-series – Memory optimized
  • I/D-series – Storage optimized

3. Key Pair

A key pair is used for secure SSH login:

  • Public key stored on AWS
  • Private key (.pem) downloaded to your computer

You MUST store the private key safely. AWS will not allow downloading it again later.

4. Security Group

A security group is a virtual firewall that controls traffic to your EC2 instance.

Common rules:

  • Port 22 – SSH
  • Port 80 – HTTP

If port 80 is blocked, your website won’t load. If port 22 is blocked, you cannot SSH into the instance.

Storage & User Data

EBS Volume (Storage)

  • Acts like the SSD/HDD of your virtual machine
  • You can choose size (e.g., 8 GB) and type (gp3 SSD)

User Data

User-data scripts run automatically when the instance launches. Example script to install Apache:

#!/bin/bash yum update -y yum install httpd -y systemctl start httpd systemctl enable httpd echo "

Welcome to my EC2 website

" > /var/www/html/index.html

How to Launch and Test an EC2 Instance

  1. Open the AWS console → EC2 → Launch Instance
  2. Set instance name (example: "my-first-ec2")
  3. Choose an AMI (Amazon Linux 2023)
  4. Select instance type (t2.micro)
  5. Create/select a key pair
  6. Configure security group (SSH + HTTP)
  7. Add user data (Apache installation script)
  8. Launch

After it is running:

  • Copy the public IP
  • Paste it in a browser
  • You will see your HTML page served by Apache

Connecting to EC2 via SSH

On Linux/Mac Terminal

chmod 400 your-key.pem ssh -i your-key.pem ec2-user@your-public-dns.amazonaws.com

On Windows

Use PuTTY with the converted key (.ppk).

Cleaning Up

  • Stop Instance – stops billing for compute
  • Terminate Instance – deletes permanently

What’s Next?

Now that you understand EC2 basics — AMI, instance types, security groups, SSH — you are ready to deploy your Spring Boot application on EC2.

The next tutorial: Running Spring Boot on an EC2 instance and exposing it to the internet.

Comments

Popular posts from this blog

Spring Boot on AWS EC2: Upload to S3 Securely Using IAM Role

Java Streams Intermediate Operations Explained with Examples

ConcurrentHashMap vs Synchronized HashMap in Java