Skip to content

Instantly share code, notes, and snippets.

@sealocal
Last active May 14, 2021 12:16
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sealocal/0cd468ba4f12cdada436aebe534b40da to your computer and use it in GitHub Desktop.
Save sealocal/0cd468ba4f12cdada436aebe534b40da to your computer and use it in GitHub Desktop.
Install Yarn and NodeJS on AWS Elastic Beanstalk EC2 Instance with Amazon Linux Ruby Platform, prior to precompiling assets for a Rails app
files:
# If this file is edited, it must be removed from EC2 instance prior to deploy.
"/opt/elasticbeanstalk/hooks/appdeploy/pre/09_yarn_install.sh" :
mode: "000775"
owner: root
group: users
content: |
#!/usr/bin/env bash
set -xe
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
echo "I am: `whoami`"
echo "App user is $EB_APP_USER"
# If yarn is not detected, install it.
if which yarn; then
echo "Skipping installation of yarn -- yarn already installed."
echo "yarn --version: `yarn --version`"
else
echo "which yarn: `which yarn`"
echo "Yarn is not installed and accessible."
echo "Installing yarn..."
# Consider that the EC2 instance is managed by AWS Elastic Beanstalk.
# Changes made via SSH WILL BE LOST if the instance is replaced by auto-scaling.
# QUESTION: Will this script be run on new instances that are created by auto-scaling?
# QUESTION: Should installation be moved to a rake task?
# Download the yarn repo
sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
# Confirm that it downloaded
file /etc/yum.repos.d/yarn.repo
# If node is not detected, install it.
if [ `node --version` == 'v6.10.0' ]; then
echo "Skipping installation of node -- node already installed."
echo "node --version: `node --version`"
else
echo "Installing Node v6.10.0 ..."
# Download the Node v6 setup script
curl --location https://rpm.nodesource.com/setup_6.x > /home/ec2-user/node_install.sh
# Confirm that it downloaded
file /home/ec2-user/node_install.sh
# Run the Node v6 setup script
sudo bash /home/ec2-user/node_install.sh
# Install nodejs
sudo yum install -y nodejs
node --version
echo "... and finished installing Node v6.10.0"
fi
# install yarn
sudo yum install -y yarn
yarn --version
echo "... and finished installing yarn."
fi
echo "Change directory to $EB_APP_STAGING_DIR"
cd $EB_APP_STAGING_DIR
# yarn install
echo "Running yarn install."
./bin/yarn install
@andyexeter
Copy link

andyexeter commented Dec 13, 2017

This script gives me the following error:

/opt/elasticbeanstalk/hooks/appdeploy/pre/09_yarn_install.sh: line 59: ./bin/yarn: No such file or directory.

Managed to fix it by replacing ./bin/yarn with just yarn

@gssbzn
Copy link

gssbzn commented Mar 26, 2018

I took inspiration from this and the native bundle install from AWS and made this version https://gist.github.com/gssbzn/54f1f8036dfd29a89629078152972dff
I also updated it to use the latest LTS version of nodejs

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