AWS EC2 (Elastic Compute Cloud)

AWS EC2 stands for Elastic Compute Cloud. It is the service that provides virtual servers — called instances — in the AWS cloud. EC2 is one of the most widely used AWS services and forms the foundation of most cloud applications.

Instead of buying physical servers, EC2 allows renting virtual machines in minutes. These virtual machines run operating systems (Linux or Windows), and applications deploy on them just like on any real server.

What Is an EC2 Instance?

An EC2 instance is a virtual machine running in an AWS data center. It has a CPU, RAM, storage, and a network connection — just like a laptop or server — but it exists entirely in software.

The word "Elastic" in the name means the instance can be started, stopped, resized, or terminated at any time. No commitment is required for most uses.

EC2 Instance Types

AWS offers hundreds of instance types, each optimized for different workloads. They are grouped into families:

FamilyOptimized ForExample InstancesUse Case
General PurposeBalanced CPU + RAMt3.micro, m5.largeWeb servers, small databases
Compute OptimizedHigh CPU powerc5.xlarge, c6g.largeVideo encoding, batch processing
Memory OptimizedLarge RAMr5.large, x1e.xlargeIn-memory databases, analytics
Storage OptimizedHigh disk I/Oi3.large, d2.xlargeData warehouses, Hadoop
Accelerated ComputingGPU processingp3.2xlarge, g4dn.xlargeMachine learning, graphics rendering

Instance names follow a naming convention. For example, t3.micro:

  • t = family (general purpose, burstable)
  • 3 = generation (3rd generation hardware)
  • micro = size (very small — 2 vCPUs, 1 GB RAM)

EC2 Pricing Models

EC2 offers multiple pricing options to optimize cost based on usage patterns:

Pricing ModelHow It WorksBest ForDiscount vs On-Demand
On-DemandPay per second/hour, no commitmentUnpredictable workloads, testingNo discount — full price
Reserved InstancesCommit to 1 or 3 yearsSteady, predictable workloadsUp to 72% cheaper
Savings PlansCommit to hourly spend for 1–3 yearsFlexible, long-term usageUp to 66% cheaper
Spot InstancesUse unused AWS capacity at deep discountBatch jobs, fault-tolerant workloadsUp to 90% cheaper
Dedicated HostsPhysical server reserved entirely for one accountCompliance, software licensingMost expensive option

Key EC2 Concepts

AMI — Amazon Machine Image

An AMI is a pre-configured template that defines what the EC2 instance looks like when it starts. It contains the operating system, software, and configuration. Think of an AMI as a snapshot of a computer that can be used to launch new identical computers instantly.

  • AWS provides standard AMIs: Amazon Linux 2, Ubuntu, Windows Server, Red Hat, etc.
  • Custom AMIs can be created after configuring an instance — useful for launching identical servers in an Auto Scaling setup.

Key Pairs

A key pair is used to securely connect to a Linux EC2 instance via SSH. It consists of a public key (stored on the instance) and a private key (downloaded by the user). Without the private key file (.pem), the instance cannot be accessed remotely.

Security Groups

A Security Group acts as a virtual firewall for an EC2 instance. It controls inbound and outbound traffic based on rules. For example:

  • Allow inbound port 22 (SSH) from the office IP only.
  • Allow inbound port 80 (HTTP) from anywhere (0.0.0.0/0).
  • Allow inbound port 443 (HTTPS) from anywhere.
  • Block all other inbound traffic.

Elastic IP

By default, an EC2 instance gets a new public IP address every time it is stopped and restarted. An Elastic IP is a fixed, static public IP address that stays assigned to an account. Attaching an Elastic IP to an instance ensures the IP never changes.

EBS — Elastic Block Store

EBS is the storage attached to an EC2 instance — like the hard drive of a computer. EC2 instances store their operating system, application files, and data on EBS volumes. EBS volumes persist even after the instance is stopped, unlike temporary storage (instance store) which is lost when the instance stops.

Launching an EC2 Instance — Step by Step

1. Go to AWS Console → EC2 → Launch Instance
        |
2. Choose AMI (e.g., Amazon Linux 2023)
        |
3. Choose Instance Type (e.g., t2.micro — Free Tier eligible)
        |
4. Configure Instance Details (VPC, subnet, IAM role)
        |
5. Add Storage (EBS volume — default 8 GB root volume)
        |
6. Configure Security Group (allow SSH port 22 + HTTP port 80)
        |
7. Create or Select Key Pair (download .pem file)
        |
8. Launch Instance — it starts in about 60 seconds
        |
9. Connect via SSH:
   ssh -i "my-key.pem" ec2-user@

EC2 User Data — Automate Setup at Launch

EC2 User Data is a script that runs automatically when an instance first starts. It is used to automate the installation of software and configuration without manually logging in.

Example — install and start a web server automatically on a Linux EC2 instance:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello from EC2!</h1>" > /var/www/html/index.html

After launch, visiting the public IP of the instance in a browser shows "Hello from EC2!" — no manual setup required.

EC2 Instance Lifecycle

[Launch] → [Running] → [Stop] → [Stopped] → [Start] → [Running]
                          |
                       [Terminate] → [Terminated] (permanently deleted)
  • Running: Instance is active and billed per second.
  • Stopped: Instance is not running. No CPU/RAM charge. EBS storage is still billed.
  • Terminated: Instance and its root EBS volume are permanently deleted.

Real-World Example — Deploying a Node.js Web App

  1. Launch a t3.micro EC2 instance with Amazon Linux AMI.
  2. Attach a security group allowing port 80 (HTTP) and port 22 (SSH).
  3. Connect via SSH and install Node.js.
  4. Upload the application code to the server.
  5. Start the application on port 80.
  6. Users access the app using the instance's public IP or a domain name pointed to it.

Summary

  • EC2 provides virtual servers (instances) in the cloud — start, stop, and resize at any time.
  • Instance types are chosen based on the workload — general purpose, compute, memory, storage, or GPU.
  • Pricing options include On-Demand, Reserved, Savings Plans, Spot, and Dedicated Hosts.
  • AMIs are templates for launching instances. Security Groups act as firewalls. EBS provides persistent storage.
  • User Data scripts automate instance configuration on first boot.

Leave a Comment