9

Can I configure Linux swap space for an AWS Elastic Beanstalk environment?

I don't see an option for it in the console. From looking at /proc/meminfo on my running instances in my environment MemAvailable is looking quite low despite there being quite high Inactive values. I suspect there are a few dormant background processes that it would do no harm to page out, and would free up a non-trivial portion of the limited physical memory on the t2.nano I'm using.

pauldoo
  • 18,087
  • 20
  • 94
  • 116

1 Answers1

16

I figured out how to do this using the .ebextensions config folder in my Tomcat web app.

Add a file .ebextensions/swap.config:

container_commands:
  add-swap-space:
    command: "/bin/bash .ebextensions/scripts/add-swap-space.sh"
    ignoreErrors: true

Add a file .ebextensions/scripts/add-swap-space.sh:

#!/bin/bash

set -o xtrace
set -e

if grep -E 'SwapTotal:\s+0+\s+kB' /proc/meminfo; then
    echo "Positively identified no swap space, creating some."
    dd if=/dev/zero of=/var/swapfile bs=1M count=512
    /sbin/mkswap /var/swapfile
    chmod 000 /var/swapfile
    /sbin/swapon /var/swapfile
else
    echo "Did not confirm zero swap space, doing nothing."
fi

More docs: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

After a while running this allowed 150MiB to be swapped out on my t2.nano instance which runs the Elastic Beanstalk Java Tomcat platform with default heap options. From what I can see there is no ongoing paging while the application runs. It looks like some dormant data has been pushed to swap and the page cache is significantly larger (up from 30MiB to 180MiB).

pauldoo
  • 18,087
  • 20
  • 94
  • 116