9

I'd like to install packages on Elastic Beanstalk using Yarn as an alternative to NPM. I've tried all sorts of solutions I've found online, but they all appear to be outdated and no longer work. Here's what I have right now, as described in this gist.

files:
  '/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh' :
    mode: '000755'
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      set -euxo pipefail

      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)

      if node -v; then
        echo 'Node already installed.'
      else
        echo 'Installing node...'
        curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -
        yum -y install nodejs
      fi

      if yarn -v; then
        echo 'Yarn already installed.'
      else
        echo 'Installing yarn...'
        wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
        yum -y install yarn
      fi

  '/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh' :
    mode: '000755'
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      set -euxo pipefail

      yarn install --ignore-engines
tk421
  • 5,775
  • 6
  • 23
  • 34
Ben Botvinick
  • 2,837
  • 3
  • 16
  • 41

2 Answers2

6

The above answer works only on Amazon Linux (AMI) 1 version. If you are using AMI version 2 you can do the following:

  1. Create a .platform/hooks/prebuild/yarn.sh file with the following content:
#!/bin/bash

# need to install node first to be able to install yarn (as at prebuild no node is present yet)
sudo curl --silent --location https://rpm.nodesource.com/setup_14.x | sudo bash -
sudo yum -y install nodejs

# install yarn
sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
sudo yum -y install yarn

# install
cd /var/app/staging/

# debugging..
ls -lah

yarn install --prod

chown -R webapp:webapp node_modules/ || true # allow to fail
  1. Be sure to chmod +x this file (it needs to be executable)

https://gist.github.com/cooperka/0960c0652353923883db15b4b8fc8ba5#gistcomment-3390935

idrisjafer
  • 725
  • 1
  • 14
  • 29
  • I have a question which is not directly related indeed. / Is there any way to change npm install command of elasticbeanstalk, like appdeploy 50npm.sh? – jeongmin.cha Mar 23 '22 at 07:06
  • @jeongmin.cha Not sure about this. why do you want to modify this behavior? – idrisjafer Mar 23 '22 at 12:33
  • I want to make EB execute `npm install --production --ignore-scripts` because npm v8 has a bug which executes `prepare` even using arguments like `--production`. – jeongmin.cha Mar 23 '22 at 15:26
5

This is what I use to run Yarn on Beanstalk :

commands:
  01_install_node:
    command: |
      sudo curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
      sudo yum -y install nodejs
  02_install_yarn:
    test: '[ ! -f /usr/bin/yarn ] && echo "Yarn not found, installing..."'
    command: |
      sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
      sudo yum -y install yarn

container_commands:
  01_run_yarn:
    command: |
      yarn install
      yarn run encore production
titili
  • 207
  • 1
  • 6
  • Where do you add this? – Declan Nnadozie Apr 21 '22 at 20:55
  • 1
    I add this on `.ebextensions` files. Have a look to these pages to get further information : [AWS - ebextensions](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions.html) & [AWS - Linux Server Command](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-commands) – titili Apr 22 '22 at 22:30