Why EC2 Ubuntu AMIs Lack Swap Space: Causes and Solutions for AWS Developers


2 views

When launching Ubuntu instances on EC2, many developers notice the absence of swap space - a puzzling discovery since most Linux distributions typically configure swap by default. This isn't an oversight on your part; Amazon's Ubuntu AMIs (Amazon Machine Images) intentionally ship without swap partitions or swap files.

There are several technical reasons behind this decision:

  • Performance Optimization: EBS-backed storage (where swap would reside) is significantly slower than instance memory
  • Cost Efficiency: Swap usage would increase EBS I/O operations, potentially raising costs
  • Instance Flexibility: AWS encourages vertical scaling (resizing instances) over relying on swap
  • Ephemeral Nature: Many EC2 use cases involve disposable instances where swap provides little benefit

Despite AWS's rationale, certain scenarios justify adding swap:

# Check current memory and swap
free -h
# Output example:
#               total        used        free      shared  buff/cache   available
# Mem:           3.7G        1.2G        2.1G         16M        456M        2.3G
# Swap:            0B          0B          0B

Here's how to safely add swap space:

# Create a 4GB swap file
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Make permanent by adding to /etc/fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Verify it's working
sudo swapon --show

If you do add swap, tune these kernel parameters in /etc/sysctl.conf:

vm.swappiness = 10
vm.vfs_cache_pressure = 50

This makes the system less aggressive about swapping, which is particularly important when using EBS-backed storage.

For memory-intensive workloads, consider:

  • Using instance types with more RAM
  • Implementing application-level memory management
  • Using EC2 Auto Scaling to handle load spikes
  • Exploring AWS MemoryDB or ElastiCache for specific use cases

After implementing swap, monitor its usage:

# Install and use htop for visual monitoring
sudo apt install htop
htop

# Or check swap usage directly
cat /proc/swaps
vmstat 1

When I first spun up Ubuntu instances on EC2, I noticed something peculiar - free -h showed zero swap space. This was surprising because most Linux systems allocate swap by default during installation. After testing multiple Ubuntu AMIs (16.04, 18.04, and 20.04), I confirmed this wasn't an isolated case.

Amazon's documentation reveals this is intentional. EC2 instances use EBS or instance store volumes rather than traditional hard drives. AWS engineers determined that:

  • SSD-backed storage performs differently than spinning disks
  • Modern instance types have substantial RAM allocations
  • Unpredictable swap behavior could affect neighboring instances in multi-tenant environments

While AWS recommends relying on instance RAM, certain workloads benefit from swap:

# Example: Memory-intensive applications like:
# - Large Java applications (JVM)
# - Machine learning workloads with spikes
# - Databases handling unpredictable loads

Here's how to create a 4GB swap file on Ubuntu:

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

To make it persistent, add this to /etc/fstab:

/swapfile none swap sw 0 0

Before implementing swap, consider these factors:

  • EBS-backed volumes have limited IOPS
  • Frequent swapping indicates you should upgrade your instance type
  • Swap usage metrics in CloudWatch can help monitor performance impact

Instead of traditional swap, you might:

# 1. Use EC2 Instance Store (if available)
# 2. Implement memory caching solutions like Redis
# 3. Optimize application memory usage
# 4. Configure EC2 Auto Scaling for bursty workloads