DevDocsDev Docs
EC2

AWS EC2

Elastic Compute Cloud - Virtual servers in the cloud

Amazon Elastic Compute Cloud (EC2) provides scalable computing capacity in the AWS cloud.

Instance Selection

Choose the right instance type for your workload. You can change instance types later, but some changes require stopping the instance.

Key Concepts

Instance Types

EC2 offers various instance types optimized for different use cases:

FamilyUse CaseExamples
tBurstable workloadst3.micro, t3.medium
mBalanced compute/memorym5.large, m6i.xlarge

Best for: Web servers, development environments, small databases

FamilyUse CaseExamples
cCompute optimizedc5.large, c6i.xlarge
rMemory optimizedr5.large, r6i.xlarge

Best for: Batch processing, gaming servers, in-memory caching

FamilyUse CaseExamples
p/gGPU instancesp3.2xlarge, g4dn.xlarge
i/dStorage optimizedi3.large, d2.xlarge

Best for: Machine learning, video encoding, data warehousing

AMIs (Amazon Machine Images)

Pre-configured templates containing the OS and applications:

  • Amazon Linux 2023: AWS-optimized Linux distribution
  • Ubuntu: Popular open-source Linux
  • Windows Server: Microsoft Windows OS
  • Custom AMIs: Your own preconfigured images

Amazon Linux 2023 is recommended for most workloads as it's optimized for AWS and includes long-term support.

Security Groups

Virtual firewalls controlling inbound/outbound traffic:

security-group-rules.json
{
  "IpPermissions": [
    {
      "IpProtocol": "tcp",
      "FromPort": 22,
      "ToPort": 22,
      "IpRanges": [{"CidrIp": "10.0.0.0/8"}]
    },
    {
      "IpProtocol": "tcp",
      "FromPort": 443,
      "ToPort": 443,
      "IpRanges": [{"CidrIp": "0.0.0.0/0"}]
    }
  ]
}

Security Best Practice

Never allow SSH (port 22) from 0.0.0.0/0. Use Systems Manager Session Manager or restrict to specific IPs.

Key Pairs

SSH key pairs for secure access:

Create Key Pair
aws ec2 create-key-pair --key-name my-key --query 'KeyMaterial' --output text > my-key.pem
chmod 400 my-key.pem

Instance Lifecycle

StateDescription
pendingInstance is launching
runningInstance is running and ready
stoppingInstance is stopping
stoppedInstance is stopped (no compute charges)
terminatedInstance is permanently deleted

Stopped instances don't incur compute charges but you still pay for attached EBS volumes.

Pricing Models

Pay by the second with no commitments.

Best for:

  • Unpredictable workloads
  • Short-term projects
  • Development and testing

Up to 72% savings with 1 or 3-year commitment.

Options:

  • Standard Reserved: Fixed instance type
  • Convertible Reserved: Can change instance types
  • Scheduled Reserved: Specific time windows

Up to 90% savings for fault-tolerant workloads.

Spot instances can be interrupted with 2 minutes notice. Use for fault-tolerant, stateless workloads.

Flexible pricing with commitment to usage ($/hour).

Types:

  • Compute Savings Plans: Any region, family, OS
  • EC2 Instance Savings Plans: Specific region/family

Storage Options

EBS (Elastic Block Store)

Persistent block storage volumes:

TypeUse CaseIOPSThroughput
gp3General purpose SSD3,000-16,000125-1,000 MB/s
io2High performanceUp to 64,0001,000 MB/s
st1Throughput HDD500500 MB/s
sc1Cold HDD250250 MB/s

gp3 is recommended for most workloads. You can provision IOPS and throughput independently.

Instance Store

Temporary storage attached to the host:

  • High I/O performance (NVMe SSD)
  • Data lost when instance stops
  • Good for caches, buffers, and temporary data

Best Practices

Security

  1. Use security groups as the first line of defense
  2. Keep instances in private subnets when possible
  3. Use Systems Manager Session Manager instead of SSH
  4. Enable detailed monitoring for production workloads
  5. Always use IAM roles instead of storing credentials

Cost Optimization

  1. Right-size instances based on actual usage
  2. Use Reserved Instances for steady-state workloads
  3. Use Spot Instances for fault-tolerant workloads
  4. Stop instances when not in use
  5. Use AWS Compute Optimizer for recommendations

High Availability

  1. Distribute instances across multiple AZs
  2. Use Auto Scaling for automatic scaling
  3. Use Elastic Load Balancing for traffic distribution
  4. Implement health checks and auto-recovery

User Data

Run scripts on instance launch:

user-data.sh
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello World" > /var/www/html/index.html

User data scripts run as root. Logs are available at /var/log/cloud-init-output.log.

Next Steps

On this page