Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I configure Linux swap space on AWS Elastic Beanstalk?

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.

like image 784
pauldoo Avatar asked Mar 20 '16 20:03

pauldoo


People also ask

Does the instance have swap memory Linux?

Amazon Linux automatically enables and uses this swap space, but your AMI may require some additional steps to recognize and use this swap space. To see if your instance is using swap space, you can use the swapon -s command. The above instance has a 900 MiB swap volume attached and enabled.

When should you not use Elastic Beanstalk?

Elastic Beanstalk isn't great if you need a lot of environment variables. The simple reason is that Elastic Beanstalk has a hard limit of 4KB to store all key-value pairs. The environment had accumulated 74 environment variables — a few of them had exceedingly verbose names.


1 Answers

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).

like image 107
pauldoo Avatar answered Oct 07 '22 07:10

pauldoo