Skip to content

Instantly share code, notes, and snippets.

@cooperka
Created June 18, 2018 23:50
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cooperka/0960c0652353923883db15b4b8fc8ba5 to your computer and use it in GitHub Desktop.
Save cooperka/0960c0652353923883db15b4b8fc8ba5 to your computer and use it in GitHub Desktop.
AWS Elastic Beanstalk - Replace npm with yarn
# .ebextensions/01_install_yarn.config
files:
'/opt/elasticbeanstalk/hooks/appdeploy/pre/49install_yarn.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_8.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
@dadooda
Copy link

dadooda commented Apr 16, 2020

Hey guys,

It could have worked 2 years ago, today it doesn't.

Today's EB scripts don't run npm directly, instead they invoke it from the guts of containerfiles/ebnode.py, heavily tuned to match details of crazy environment set by Amazon. Thus, such a simple patch relying on system-wide yarn fails bluntly...

Cheers!
Alex

@PaulMEdwards
Copy link

This looks to be a newer solution:
https://stackoverflow.com/a/56776872/1217760

@nikaspran
Copy link

I was successfully able to use the approach described here. TL;DR:

  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_12.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
  2. Be sure to chmod +x this file (it needs to be executable)

@hmtri1011
Copy link

Hi @nikaspran, I follow this article but got permission error for this file .platform/hooks/prebuild/yarn.sh. How and where I should do chmod +x to this file?

@cyzanfar
Copy link

make sure you add the correct version:

sudo curl --silent --location https://rpm.nodesource.com/setup_{version_number}.x | sudo bash -

@cyzanfar
Copy link

@hmtri1011

for permission, git update-index --chmod=+x path/to/file

@puneetpandey
Copy link

2 things I would like to add:

  1. after git update-index command, you only have to git commit
  2. touching /path/to/file is always resetting the permission for me from 100755 => 100644. So I have to make sure I run update-index command again

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