Compute

EC2 — Virtual Servers

Launch and manage virtual servers in the cloud with Amazon EC2.

Amazon EC2

Elastic Compute Cloud (EC2) provides resizable virtual machines in the cloud. You choose the CPU, memory, storage, and networking capacity.

Instance Types

EC2 instances are organized into families:

  • General purpose (t3, m6): Balanced CPU/memory
  • Compute optimized (c6): High CPU
  • Memory optimized (r6): High memory
  • Storage optimized (i3): High I/O
  • GPU instances (g4, p4): ML, graphics

Key Components

  • AMI (Amazon Machine Image): OS + software template
  • Security Group: Virtual firewall (which ports to allow)
  • Key Pair: SSH authentication
  • Elastic IP: Static public IP address
  • IAM Role: Permissions for the EC2 instance to access AWS services

Example

bash
# Launch an EC2 instance (via CLI)
aws ec2 run-instances   --image-id ami-0c55b159cbfafe1f0   --instance-type t3.micro   --key-name my-key-pair   --security-group-ids sg-12345678   --subnet-id subnet-12345678   --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyServer}]'

# Connect via SSH
ssh -i my-key-pair.pem ec2-user@ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com

# Describe instances
aws ec2 describe-instances   --filters "Name=instance-state-name,Values=running"

# Start/Stop/Terminate
aws ec2 start-instances --instance-ids i-1234567890abcdef0
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

# Allocate and associate Elastic IP
aws ec2 allocate-address --domain vpc
aws ec2 associate-address   --instance-id i-1234567890abcdef0   --allocation-id eipalloc-12345678

# Create security group
aws ec2 create-security-group   --group-name MyWebSG   --description "Web server security group"

# Add inbound rules
aws ec2 authorize-security-group-ingress   --group-id sg-12345678   --protocol tcp   --port 80   --cidr 0.0.0.0/0
Try it yourself — BASH