Skip to content

Instantly share code, notes, and snippets.

@bwbaugh
Last active September 27, 2019 06:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwbaugh/a771871949786e45738c2ab65e8256ac to your computer and use it in GitHub Desktop.
Save bwbaugh/a771871949786e45738c2ab65e8256ac to your computer and use it in GitHub Desktop.
EC2 swap file instructions. Gathered from several online articles / blogs / stackexchange.

Can check if you're using swap with sudo swapon --show and free -h.

EBS volume

Useful for t2.micro and other instances with no access to instance store. Use this if you prefer not to mess with the root volume.

  1. Create the EBS volume e.g., 1 GB.   Be sure to avoid the Previous Generation EBS Magnetic volume as it charges for I/O.
  2. Run lsblk before on the instance.
  3. Attach the volume to an instance. Attaching to /dev/sdf will appear as /dev/xvdf on the instance.
  4. Run lsblk again on the instance to verify it's attached.
  5. Set up the swap area: sudo mkswap /dev/xvdf
  6. Turn on the swap: sudo swapon /dev/xvdf

To make this persist across reboots:

  1. Backup the fstab file: sudo cp -iv /etc/fstab{,-$(date '+%y%m%d%H%M%S').bak}
  2. Add: /dev/xvdf none swap sw 0 0

Sources:

Tweak swap settings

https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04

View current values:

cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure

Temporarily change the values:

sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50

Persist the values across reboots by appending to /etc/sysctl.conf:

vm.swappiness=10
vm.vfs_cache_pressure=50
@hseritt
Copy link

hseritt commented Dec 17, 2016

Good stuff! This helped me out for an AWS install. Thanks.

@buckneri
Copy link

what about your bill? seems like if you wanted to pay $300 a month for IOPs overages.. you would just upgrade your instance to one that supports instance store?

@bwbaugh
Copy link
Author

bwbaugh commented Jul 16, 2017

@buckneri SSD based EBS volumes don't charge by the IOPS. ☺
Looking at the pricing page, it looks like most of the Current Generation volumes include I/O. The "EBS Magnetic" volume now listed under Previous Generation was the one that charges by I/O. I've edited the gist to note this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment