One thing that you may find when running Elastic Beanstalk on small instances is that you can run out of memory very easily.

This can be particularly troublesome when deploying a real world application to Elastic Beanstalk that depends on gems with native extensions. Building these native extensions can quickly exhaust physical memory and by default ec2 instances do not have any virtual memory (swap) configured.

ERROR: Unsuccessful command execution on instance id(s) 'i-0077ee0ac45e9207a'. Aborting the operation.
ERROR: Failed to deploy application.                                
ERROR: Failed to deploy application.

Examining the eb activity logs with eb logs reveals the underlying problem:

Cannot allocate memory - /opt/rubies/ruby-2.3.1/bin/ruby -r ./siteconf20170603-17803-rttiu8.rb extconf.rb 2>&1

The machine has run out of physical memory.

To fix this we can add swap to our Elastic Beanstalk instances using the following file:

# .ebextensions/01_setup_swap.config
        test: test ! -e /var/swapfile
        command: |
            /bin/dd if=/dev/zero of=/var/swapfile bs=1M count=2048
            /bin/chmod 600 /var/swapfile
            /sbin/mkswap /var/swapfile
            /sbin/swapon /var/swapfile

Add this file to git: git add . && git commit -m "adds swap". Now deploy to Elastic Beanstalk and your memory issues should be fixed.

Why not subscribe to my mailing list - I'll send out regular emails with new content (don't worry I won't spam you!)

* indicates required
Blog Logo

Chris Greening



Chris Greening

Blogging about random stuff

Back to Overview