DEV Community

loading...

Elastic Beanstalk deployments with Lerna

alexbh profile image Alex ・2 min read

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/
Enter fullscreen mode Exit fullscreen mode

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:

.git

terraform/

node_modules/
**/node_modules

packages/frontend
packages/landing
packages/lambda-1
packages/lambda-1

**/dist
**/out
**/build
Enter fullscreen mode Exit fullscreen mode

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:

packages:
    yum:
        amazon-linux-extras: []

commands:
  01_postgres_activate:
      command: sudo amazon-linux-extras enable postgresql10
  02_postgres_install:
      command: sudo yum install -y postgresql-devel
  03_install_node:
    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
  04_install_yarn:
    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;
  05_dependencies_install:
    command: |
      cd /var/app/staging;
      sudo yarn;

container_commands:
  01_sudo_install:
    command: sudo cd /home/webapp && sudo yarn
Enter fullscreen mode Exit fullscreen mode

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 setup powers Portabella (https://portabella.io) and Block Hooks (https://block-hooks.com). I'll keep these files updated with any improvements as I find them.

Discussion (0)

pic
Editor guide