• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » AWS, Azure, Cloud » Adding A Swap File To An “EBS Only” Amazon EC2 Instance

Adding A Swap File To An “EBS Only” Amazon EC2 Instance

By Ehi Kioya Leave a Comment

I have previously written on the subject of adding a swap file to an Amazon EC2 instance. However, that article recommended and focused on adding the swap file to the instance storage (also known as ephemeral storage) that comes with certain Amazon EC2 instances.

The problem is that, in lots of use cases, your chosen Amazon EC2 instance type may not come with any instance storage – like the popular T2 instances which are EBS backed only.

This article discusses how you can still leverage the concept of swap files even on “EBS only” Amazon EC2 instances.

If you’re not familiar with swap files and virtual RAM at all, read my older post to get an idea of why we often need one: Swap File For Ubuntu On Amazon EC2 – Why And How?

What’s Cool About The Following Swap File Process?

One thing I really like about the process described below is that unlike the process in my previous article, the swap file we will add using the below process will persist even after the system is stopped and later started. This is a huge advantage over instance/ephemeral storage swap files which only manage to survive reboots but have to be recreated after a full stop-and-start machine cycle.

Of course, ephemeral storage has its own advantages as well (one of which is speed). But since the type of EC2 instances we’re working with here don’t have ephemeral storage at all, the speed advantage doesn’t apply.

In addition, unlike a few other swap file guides out there (that write some gigabytes of zeros to some portion of your root drive), my process below DOES NOT mess with your root drive at all. It uses its own small, dedicated EBS drive for swap.

Steps To Add A Swap File To An “EBS Only” Amazon EC2 Instance

Step 1: Decide on how much swap you will be adding. In this example, I will be adding a 3GB swap file to a t2.medium Amazon EC2 instance. For Ubuntu users, read here if you need help deciding on how much swap you should allocate.

Step 2: Run lsblk on your instance. Note the attached volume(s).

Step 3: Shut down the instance.
 
Step 4: Create a new EBS volume that you will use as your swap file (in my case, this would be a 3GB volume).
IMPORTANT: Make sure the volume is GP2 (SSD) to avoid paying extra for I/O.
Mount the new volume to /dev/sdf (this becomes /dev/xvdf).

Step 5: Start the instance.
 
Step 6: Run lsblk again on the instance to verify that the new volume is attached.
 
Step 7: Set up the swap area with this command:

sudo mkswap /dev/xvdf

Step 8: Turn on the swap with this command:

sudo swapon /dev/xvdf

Step 9: To make sure the swap persists across reboots as well as full stop-and-start cycles…
Backup the fstab file with this command:

sudo cp -iv /etc/fstab{,-$(date '+%y%m%d%H%M%S').bak}

Now add this line to the fstab file:

/dev/xvdf swap swap sw 0 0

Step 10: To confirm that the new swap file is in use, run the “swapon -s” command. To check the amount of swap space, run either the “top” or the “free” commands.

Step 11: To view the current (default) values to swapiness and cache pressure, run the following commands:

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

Step 12: Stop and then start the instance and use any of the commands from Step #10 to verify that swap is still on.

Ever had to add a swap file to an “EBS only” Amazon EC2 instance? Did this article help you do that? Please share your comments and contributions below.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: AWS, Azure, Cloud, Cloud Computing, Linux & Servers Tagged With: Amazon EBS, Amazon EC2, AWS, Cloud Computing, Linux

About Ehi Kioya

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

23,735
Followers
Follow
30,000
Connections
Connect
14,568
Page Fans
Like
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • How To Change A SharePoint List Or Library URL How To Change A SharePoint List Or Library URL
    All versions of the SharePoint user interface provide an option to change the title (or display name) of a list or library. Changing SharePoint library URL (or internal name), however, is not exactly very intuitive. We will discuss the process in this article.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • About
  • Contact

© 2022   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy