Official hands-on tutorial for launching and connecting to your first EC2 instance. Read →
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 family | Best for | Example |
|---|---|---|
| t3/t4g | General-purpose web apps (burstable) | t3.micro (free tier), t3.small |
| m6i/m7g | Balanced compute/memory APIs | m6i.large, m7g.medium |
| c6i/c7g | CPU-intensive workloads | c6i.large |
| r6i | Memory-intensive (Redis, databases) | r6i.large |
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.
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.
# 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
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.