Elastic Beanstalk deployments with Lerna


AWS’s Elastic Beanstalk is a great platform for quickly spinning up apps in a reproducible manner. It’s definitely not without its quirks and if you have infrastructure already in place for rolling Docker image deployments, I’m not advocating that you switch.

I’ve used it for a bunch of projects because I’m already tied into the AWS ecosystem (RDS and S3 primarily) and just want to get products out the door, not worry about task definitions (ECS), scaling underlying EC2 instances (ECS) or exorbitant costs (looking at you Fargate).

I also primarily use monorepos for my projects, Lerna being my tool of choice to manage. Over the last few months I’ve tweaked my Elastic Beanstalk setup to perfectly work with how I do things, so there may be changes on your end needed.

Almost without fail my projects have:

  • a frontend (think app.domain.com)
  • a landing page (domain.com)
  • a backend (backend.domain.com)
  • potentially a few cron lambda’s.

In Lerna, that looks a bit like this:

- packages/
  - common/
  - frontend/
  - landing/
  - backend/
  - lambda-1/
  - lambda-2/

There’s a few tips to getting frictionless deployments going with Elastic Beanstalk so here they are.

Leverage .ebignore

This is critical for small deployment bundles. I want to run t2.small or if possible t2.micro instances and doing a lerna bootstrap on a small machine with heavyweight dependencies like React for the 1000+ other modules installed for typical frontend deployments will cripple those machines.

Here’s what my .ebignore file looks like in a typical setup:






Install Lerna and Yarn

Elastic Beanstalk doesn’t come with Yarn already installed, so you have to do it yourself. Create a file .ebextensions/yarn.config in the root of your repository with this:

        amazon-linux-extras: []

      command: sudo amazon-linux-extras enable postgresql10
      command: sudo yum install -y postgresql-devel
    test: '[ ! -f /usr/bin/node ] && echo "node not installed"'
    command: |
      echo "Installing node"
      sudo curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -
      sudo yum -y install nodejs
    test: '[ ! -f /usr/bin/yarn ] && echo "Yarn not found, installing..."'
    command: |
      echo "Installing yarn"
      sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo;
      sudo yum -y install yarn;
    command: |
      cd /var/app/staging;
      sudo yarn;

    command: sudo cd /home/webapp && sudo yarn

This could probably be optimised but it just works and that’s what is important for us. If you’re not using Postgres feel free to remove the amazon-linux-extras stuff.

This is the setup that powers Portabella and Block Hooks so it’s battle tested and reliable. I’ll keep these files updated with any improvements as I find them.