Skip to content

Instantly share code, notes, and snippets.

@steinnes
Last active October 30, 2019 17:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save steinnes/1f8a1b44fed4b136005f to your computer and use it in GitHub Desktop.
Save steinnes/1f8a1b44fed4b136005f to your computer and use it in GitHub Desktop.
EB config file which executes setup_swap.sh
container_commands:
01setup_swap:
command: "bash .ebextensions/setup_swap.sh"
@samzilverberg
Copy link

samzilverberg commented Jul 29, 2016

ty your article at http://steinn.org/post/elasticbeanstalk-swap/ was helpful.

some thoughts:
container_commands run after your source bundle has been zipped and before your container has been built (for docker for example), and before the actual deploy takes place.

i found it more useful to setup the swap before everything takes place.
im my case the build process takes extra memory that my app doesnt need and that my instance type on aws (small) doesnt have.

so i setup the swap logic to run as part of the Elastic Beanstalk hooks flow:

setup_swap.config

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/00setup_swap.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash

      SWAPFILE=/var/swapfile
      SWAP_MEGABYTES=1024

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

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

you might find it useful as well, enjoy and thanks for the article again!

@lucasdavila
Copy link

lucasdavila commented Nov 5, 2016

@samzilverberg thank you to the comment, it helped me a lot, also thanks to steinnes :)

As a suggestion, you can also setup a "post hook" to turn of the swap and remove the swap file, after the deploy:

.ebextensions/setup_swap.config

commands:
  create_pre_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/pre"
    ignoreErrors: true
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/00_setup_swap.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash

      SWAPFILE=/var/swapfile
      SWAP_MEGABYTES=1024

      if [ -f $SWAPFILE ]; then
        echo "Swapfile $SWAPFILE found, skipping"
        exit 0;
      fi

      /bin/dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAP_MEGABYTES
      /bin/chmod 600 $SWAPFILE
      /sbin/mkswap $SWAPFILE
      /sbin/swapon $SWAPFILE
      exit 0;
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_remove_swap.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash

      SWAPFILE=/var/swapfile

      /sbin/swapoff $SWAPFILE
      rm $SWAPFILE
      exit 0;

@steinnes
Copy link
Author

Wow, I just noticed these comments on my gist, I'm flattered! Perhaps I should enable comments on my blog :-)

@samzilverberg: The reason I chose container_commands was to be able to include the swap setup script as a separate file, but your implementation of inlining inside the config yaml works fine as well, and I guess some will feel that it's more elegant (also it does not suffer from a mistake I made in cleaning up the path). I am curious how you learned to write files into /opt/elasticbeanstalk/hooks/appdeploy/pre/, is this something the AWS docs recommend or something you figured out by reverse engineering Elastic Beanstalk internals? :-)

@lucasdavila: Out of curiosity, why would you disable the swap after the deployment? To me the only upside would be to save some disk space, but offset that against performing extra swap-creation work with every deployment (thus making them slower). If you're worried about the kernel using the swap space and the node performance deteriorating due to unnecessary swapping, that should be avoidable by setting vm.swappiness = 0 (see: https://en.wikipedia.org/wiki/Swappiness)

I'm (perhaps sadly) not going to update the blog post because I've switched to kubernetes for my app deployments, and thus can't easily test your methods, but I did update the gist to remove the erroneous /swap/.. path to setup_swap.sh.

@rokumatsumoto
Copy link

@steinnes

I'm (perhaps sadly) not going to update the blog post because I've switched to kubernetes for my app deployments, and thus can't easily test your methods

I tested @lucasdavila 's config and it worked.

Please update your blog post.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment