2

So my application installs and deploys fine as long as I put it on an instance bigger than micro.

when I deploy on micro when composer tries to install I get a php error about not being able to allocate enough memory. so I did some googling and it seems that you can create a swap file/disk on boot/ creating of the instance here are two tutorials one two

these might be dated so first question is can this still be done?

Second question is how do I create a .ebextensions file to make this happen on deploy?

Here is my attempt but its not working unsurprisingly enough as I know very little about this.

commands:
command:dd if=/dev/zero of=/swapfile bs=1M count=3072
command:mkswap /swapfile
command:swapon /swapfile
Mark
  • 2,889
  • 4
  • 34
  • 74
  • 1
    Tangentially related: Make sure you are deploying your composer.lock file with your app. This will reduce the amount of memory needed by Composer. – Jeremy Lindblom Jan 30 '14 at 22:57

3 Answers3

10

Haven't attempted creating a swap, so I'm unable to answer your first question. But be advised that the .ebextensions file is a YAML-file. That means that spaces and syntax is extremely important for things to work properly...

The syntax of your example is a bit off. Should be something along the lines of (see here):

commands:
    000_dd:
        command: dd if=/dev/zero of=/swapfile bs=1M count=3072
    001_mkswap:
        command: mkswap /swapfile
    002_swapon:
        command: swapon /swapfile

Commands are executed in alphabetical order, so prefixing them with a number is a good idea.

Ensure that you're able to log on to your EC2 instance, and keep an eye on the /var/log/cfn-init.log file. This will tell you what happens. And what didn't work as intended... ;-)

Good luck!

joker
  • 736
  • 4
  • 11
4

Running Composer on a machine requires quite a bit of resources, especially RAM.

I have learned that it is counterproductive to use swap space on a EC2 micro instance because a) it is slow swap space and b) using it counts against IO which has also to be paid, allowing for an unexpected bill the next month.

The correct way to deploy anything that uses Composer is to install all dependencies prior to deploying the code onto the machine, then sync it in one go. That way you are independent from the limited resources of the instance because you can use some powerful deployment machine (I bet your dev machine can do it). You also prevent ending up with a broken site because some required library's hosting is down (ever experienced Github downtimes?), because you'd detect this on the deploying server, not the live machine. Additionally, Composer will cache anything downloaded, which also might exceed the limited resources on a micro instance - or at least use it for something non-productive.

Sven
  • 62,889
  • 9
  • 98
  • 100
  • Re "b) using it counts against IO which has also to be paid" - I can't seem to find where this is the case for using the Instance Store for swap space, any ideas? I was under the impression that Instance Store IO was free as it's "located on disks that are physically attached to the host computer" – a darren Feb 12 '16 at 13:07
  • I am in no way familiar with AWS and their billing details. – Sven Feb 12 '16 at 14:12
  • Considering that the answer is 2 years old, I suppose the source I used to base my answer on had it mentioned. Doing some googling now I find lots of references of "adding swap to EC2" that suggest there is a "right" way and a wrong way, and I'd say that I likely only read about the wrong way at that time. – Sven Feb 12 '16 at 16:41
  • I probably had found an answer like this: https://stackoverflow.com/a/22247782/1627406 – Sven Feb 12 '16 at 16:43
  • Ok: "Swap should take place on the Instance Storage (ephemeral) disk and not an EBS device. Swapping will cause a lot of IO and will increase cost on EBS. EBS is also slower than the Instance Store and the Instance Store comes free with the EC2 Instance." via https://stackoverflow.com/a/22247782/1627406 - makes sense, thanks for finding, Sven – a darren Feb 12 '16 at 16:56
3

You can create a predeploy hook to setup swap through ebextensions. Create an .ebextensions folder in your project root. In that folder, create a file 0001_setup_swap.config with the following contents:

commands:
  create-pre-dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/pre"
    ignoreErrors: true

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/0001_setup_swap.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash

      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

      /sbin/mkswap $SWAPFILE
      /sbin/swapon $SWAPFILE

      echo 10 | sudo tee /proc/sys/vm/swappiness
      echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

      /bin/chown root:root $SWAPFILE
      /bin/chmod 600 $SWAPFILE

For more information, refer to these: https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-12-04 , http://steinn.org/post/elasticbeanstalk-swap/

Tom Aranda
  • 4,698
  • 10
  • 29
  • 45
abr
  • 151
  • 2
  • 7