EC2 — Virtual Machines
Launch, connect, and manage virtual machines — AMIs, instance types, security groups, and key pairs
Skills taught: aws.ec2, aws.ami, aws.instance-types, aws.security-groups, aws.key-pairs, aws.user-data
EC2 — rent a computer in the cloud
EC2 (Elastic Compute Cloud) is a virtual machine running in AWS's data center. You choose the operating system (Amazon Linux, Ubuntu, Windows), the hardware size (CPU, RAM, storage), and the region. Within minutes, you have a full Linux server you can SSH into, install software on, and run anything — exactly like a physical server, but billed by the hour.
Instance types and families
FORMAT: family.size
Examples: t3.micro, m7g.large, c7i.2xlarge, r6i.4xlarge
FAMILIES:
t3/t4g — General purpose, burstable → dev/test, low-traffic apps
m7g/m6i — General purpose, balanced → most production apps
c7i/c6g — Compute optimized (more CPU/RAM) → CPU-intensive workloads
r7i/r6g — Memory optimized (more RAM/CPU) → databases, caching
g4dn — GPU instances → ML inference, video
i3/i4i — Storage optimized (NVMe SSDs) → databases, big data
Suffix 'g' = Graviton (ARM) — 20-40% cheaper, same performance
SIZES (within a family, each size doubles resources):
nano → micro → small → medium → large → xlarge → 2xlarge → 4xlarge...
PRACTICAL SIZING (2026 pricing, us-east-1, on-demand):
t4g.micro → 2 vCPU, 1GB → $6/month (free tier: 750h/month)
t4g.small → 2 vCPU, 2GB → $12/month (small API, hobby project)
t4g.medium → 2 vCPU, 4GB → $24/month (Node.js app + workers)
m7g.large → 2 vCPU, 8GB → $56/month (production API)
m7g.xlarge → 4 vCPU, 16GB → $112/month (higher traffic)
Spot instances: up to 90% off on-demand, can be interrupted
Reserved instances: commit to 1-3 years, save 30-60%
Launching your first EC2 instance
# 1. Create a key pair (to SSH into the instance)
aws ec2 create-key-pair \
--key-name myapp-key \
--query 'KeyMaterial' \
--output text > myapp-key.pem
chmod 400 myapp-key.pem # restrict permissions
# 2. Create a security group (instance-level firewall)
aws ec2 create-security-group \
--group-name myapp-sg \
--description 'MyApp security group'
# Allow SSH from your IP only:
aws ec2 authorize-security-group-ingress \
--group-name myapp-sg \
--protocol tcp --port 22 \
--cidr $(curl -s ifconfig.me)/32
# Allow HTTP/HTTPS from anywhere:
aws ec2 authorize-security-group-ingress --group-name myapp-sg --protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name myapp-sg --protocol tcp --port 443 --cidr 0.0.0.0/0
# 3. Launch the instance
# AMI IDs change by region — get latest Ubuntu 24.04 in your region:
aws ssm get-parameters \
--names /aws/service/canonical/ubuntu/server/24.04/stable/current/amd64/hvm/ebs-gp2/ami-id \
--query 'Parameters[0].Value' --output text
aws ec2 run-instances \
--image-id ami-0xxxxxxxxxxxxxxxx \
--instance-type t4g.small \
--key-name myapp-key \
--security-groups myapp-sg \
--count 1 \
--user-data file://startup.sh # optional: runs on first boot
# 4. Get the public IP
aws ec2 describe-instances \
--filters Name=instance-state-name,Values=running \
--query 'Reservations[*].Instances[*].[InstanceId,PublicIpAddress]' \
--output table
# 5. SSH in
ssh -i myapp-key.pem ubuntu@54.x.x.x
User data — bootstrap on first boot
#!/bin/bash
# This script runs as root on the first boot of the instance
# Use it to install software and configure the server
set -e # exit on error
# Update packages
apt-get update -y
apt-get upgrade -y
# Install Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
# Install Nginx
apt-get install -y nginx
# Install Docker
curl -fsSL https://get.docker.com | sh
usermod -aG docker ubuntu
# Clone and start the app
git clone https://github.com/myorg/myapp.git /app
cd /app
npm ci --production
# Install PM2 to manage the Node.js process
npm install -g pm2
pm2 start server.js --name myapp
pm2 startup ubuntu -u ubuntu --hp /home/ubuntu # auto-start on reboot
pm2 save
echo 'Server setup complete'
Security groups — the instance firewall
INBOUND RULES (what traffic can reach the instance):
Type Protocol Port Source
SSH TCP 22 Your IP only (x.x.x.x/32) — NEVER 0.0.0.0/0
HTTP TCP 80 0.0.0.0/0 (anywhere)
HTTPS TCP 443 0.0.0.0/0 (anywhere)
Custom TCP 3000 sg-xxxxxxxxx (allow traffic from app LB only)
OUTBOUND RULES (what traffic can leave the instance):
By default: all outbound is allowed
Restrict: specific IPs/ports for locked-down environments
Security group rules are STATEFUL:
If you allow inbound on port 80, the response traffic is
automatically allowed out — you don't need to add an outbound rule
Never:
- Allow SSH (port 22) from 0.0.0.0/0 — bots scan for this constantly
- Use security groups as your only security layer
- Forget to revoke rules when they're no longer needed
Tip: Use AWS Systems Manager Session Manager instead of SSH. Session Manager gives you a shell in your EC2 instance from the AWS console or CLI — no key pairs, no open port 22, no SSH firewall rules. Access is controlled by IAM. It logs sessions to CloudWatch. It works even in private subnets with no internet gateway. It's strictly better than SSH for production access.
Frequently Asked Questions
EC2 vs Lambda vs ECS — which compute service should I use?
EC2: you manage the OS, best for long-running processes, VMs, or when you need full control. Lambda: for short-lived functions triggered by events, pay-per-invocation, zero servers to manage. ECS/Fargate: for containerized applications, managed container scheduling. Start with Lambda for event-driven code, Fargate for containerized apps, EC2 only when you need specific OS-level control.