Deployment to Elastic Beanstalk fails with 'Cannot allocate memory' error

不羁的心 提交于 2019-12-04 06:29:00

I found that the answer from @osazemeu based on the blog article needed a tweak, because the container_commands execute too late (after the application was already set up), and thus it was never run.

I changed the container_command into a command (see this AWS documentation for detail on the difference between the two), and also created the file inline rather than having a separate .sh file since I didnt want to have to keep track of what directory that was being put in at deployment time. I called the file 01setup_swap.config and placed in the directory .ebextensions in the root of my project. This did the trick for me. The contents is as follows:

files:
  "/home/ec2-user/setup_swap.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash
      # based on http://steinn.org/post/elasticbeanstalk-swap/

      SWAPFILE=/var/swapfile
      SWAP_MEGABYTES=2048

      if [ -f $SWAPFILE ]; then
        echo "Swapfile $SWAPFILE found, assuming already setup"
        exit;
      fi

      /bin/dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAP_MEGABYTES
      /bin/chmod 600 $SWAPFILE
      /sbin/mkswap $SWAPFILE
      /sbin/swapon $SWAPFILE

commands:
  01setup_swap:
    command: "bash setup_swap.sh"
    cwd: "/home/ec2-user/"
osazemeu

I encountered similar issues a couple of days back and this was how I solved it. Only proceed to the next step if the present step fails.

Step 1: Increase the swap memory of your ElasticBean EC2 instance

yourapp/.ebextensions/setup_swap.sh
#!/bin/bash

SWAPFILE=/var/swapfile
SWAP_MEGABYTES=2048

if [ -f $SWAPFILE ]; then
        echo "Swapfile $SWAPFILE found, assuming already setup"
        exit;

/bin/dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAP_MEGABYTES
/bin/chmod 600 $SWAPFILE
/sbin/mkswap $SWAPFILE
/sbin/swapon $SWAPFILE

Step 1.1 In your yourapp/.ebextensions/01_setup_swap.config

container_commands:
    01setup_swap:
        command: "bash .ebextensions/swap/setup_swap.sh"

source: elasticbeanstalk-swap

Step 2: Increase the size of your ElasticBeanstalk instance, and try to deploy again.

Step 3: If all else fails, run this command in your rails app.

bundle exec bundle package

This compiles the gems and saves it to a local folder (vendor/cache) in your rails app. This folder is accessed during deployment, and there will be no for compilation.

Commit these files to your repo, and attempt to deploy. It should work without hitches.

Just ran in to this error. Here is what I learned and how I got passed this issue:

With the free tier of AWS elastic beanstalk, by default the EC2 instance type you get, as of the date of this answer, is t1.micro. t1.micro has a very small amount of memory. So little that the gem install process for this gem fails as it is evident by the post. t1.micro is now considered a previous generation instance type (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html). To get past this memory allocation failure, you must update the EC2 instance of your AWS elastic beanstalk app from t1.micro to t2.micro. This worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!