← Course Index

EC2 — Your First Server

~30 min · AWS

Ref
Primary Source
AWS EC2 Getting Started Guide

Official hands-on tutorial for launching and connecting to your first EC2 instance. Read →

What is EC2?

EC2 (Elastic Compute Cloud) is a virtual machine running in AWS's data centers. You choose the OS (Ubuntu, Amazon Linux), the size (CPU/RAM), and the region. You pay per hour of compute time.

Instance familyBest forExample
t3/t4gGeneral-purpose web apps (burstable)t3.micro (free tier), t3.small
m6i/m7gBalanced compute/memory APIsm6i.large, m7g.medium
c6i/c7gCPU-intensive workloadsc6i.large
r6iMemory-intensive (Redis, databases)r6i.large
💡 Start here

For a side project or small startup, t3.small (~$15/month) or t3.medium (~$30/month) is usually plenty. Scale up only when you need to.

Launching an EC2 Instance

  1. Go to EC2 → Instances → Launch Instance
  2. Choose AMI: Ubuntu 22.04 LTS (most tutorials target this)
  3. Choose instance type: t3.micro for free tier
  4. Create or select a key pair (download the .pem file — can't recover it later)
  5. Configure Security Group (firewall):
    • SSH (port 22): Your IP only — not 0.0.0.0/0
    • HTTP (port 80): 0.0.0.0/0
    • HTTPS (port 443): 0.0.0.0/0
  6. Attach an IAM role if your app needs AWS service access
  7. Launch
🚨 Security group gotcha

Never open SSH (port 22) to 0.0.0.0/0 (the internet). You'll get thousands of brute-force attempts per hour. Restrict to your IP or use a VPN.

Deploying Your App to EC2

# 1. SSH into the instance
ssh -i ~/.ssh/my-key.pem ubuntu@YOUR_EC2_IP

# 2. Update and install dependencies
sudo apt update && sudo apt upgrade -y
sudo apt install -y docker.io docker-compose-v2 nginx git

# 3. Clone your app
git clone https://github.com/yourusername/yourapp
cd yourapp

# 4. Build and start with Docker Compose
sudo docker compose up -d

# 5. Configure Nginx to proxy to Docker (port 3000)
# (See Lesson 09 for the full Nginx config)

# 6. Install Certbot and get SSL certificate
# (See Lesson 10)

# 7. Verify everything works
curl https://yourapp.com/health

Elastic IP — Stable IP Address

By default, EC2 instances get a new public IP on every restart. Allocate an Elastic IP (static) and attach it to your instance:

# Via AWS CLI
aws ec2 allocate-address --domain vpc
# Note the AllocationId

aws ec2 associate-address   --instance-id i-1234567890abcdef0   --allocation-id eipalloc-12345678

# Elastic IPs are free when attached to a running instance
# You're charged if you allocate one but don't use it
⚠️

After allocating an Elastic IP, update your DNS A record to point to the new IP. Then it never changes again even after reboots.

Check Your Understanding

1. You launch an EC2 instance and open SSH to 0.0.0.0/0 in the security group. What will happen within hours?
2. You stop and restart your EC2 instance. Your app breaks because the DNS A record is wrong. Why?
3. For a Node.js web app serving ~1000 req/min, which instance type is most cost-effective to start with?