Skip to content

Instantly share code, notes, and snippets.

@obonyojimmy
Forked from nnnikolay/fix-npm.config
Created May 13, 2019 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obonyojimmy/af676764072f0a166c55c1c04d466e66 to your computer and use it in GitHub Desktop.
Save obonyojimmy/af676764072f0a166c55c1c04d466e66 to your computer and use it in GitHub Desktop.
Not the final AWS Elastic Beanstalk deployment for NodeJS. Why not the final? some parts are missing or not tested, like configuration changes. Currently it has been tested for application deployment only.
files:
"/opt/elasticbeanstalk/env.vars":
mode: "000775"
owner: root
group: users
content: |
# enable extra logs
set -xe
# Defines variables for use by the other scripts below.
EB_NODE_VERSION=$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)
# Exported to make sure Node binaries can be found by npm when we run it.
# And this lets us invoke npm more simply too.
export PATH=/opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-x64/bin:$PATH
# For parity with EB https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L147
# and because some npm packages require HOME. This will also get npm to
# read its configuration from the proper directory.
export HOME=$(/opt/elasticbeanstalk/bin/get-config container -k app_user_home)
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
# specify architecture
case $(arch) in
'i686') export ARCH=x86;;
'x86_64') export ARCH=x64;;
esac
# Export the user's environment variables for use with npm, both because
# EB does it https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L150
# as well as because we might need NPM_TOKEN.
#
# I wish this was a heredoc but I can't get the syntax right in YAML,
# EB gives an error "warning: here-document delimited by end-of-file (wanted `EOS`)"
/opt/elasticbeanstalk/bin/get-config --output YAML environment|perl -ne "/^\w/ or next; s/: /=/; print qq|\$_|" > /var/tmp/envvars
source /var/tmp/envvars
"/opt/elasticbeanstalk/hooks/appdeploy/pre/40install_node.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
/opt/elasticbeanstalk/containerfiles/ebnode.py --action node-install
. /opt/elasticbeanstalk/env.vars
#make sure node binaries can be found globally
ln -sf /opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-$ARCH/bin/node /usr/bin/node
ln -sf /opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-$ARCH/bin/npm /usr/bin/npm
ln -sf /opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-$ARCH/bin/node-gyp /usr/bin/node-gyp
echo "checking npm..."
if [ ! -f "/opt/elasticbeanstalk/node-install/npm_updated" ]; then
cd /opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-$ARCH/bin/ && /opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-$ARCH/bin/npm update npm -g
touch /opt/elasticbeanstalk/node-install/npm_updated
echo "YAY! Updated global NPM version to `npm -v`"
else
echo "Skipping NPM -g version update. already installed"
fi
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Only install modules, don't rebuild like Elastic Beanstalk does by default:
# https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663.
# New modules will be built when they are installed, and cached modules don't
# need to be rebuilt. When the Node version changes, the configdeploy script
# will rebuild.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script.
. /opt/elasticbeanstalk/env.vars
cd $EB_APP_STAGING_DIR
sudo -u ec2-user -H npm install --production
if [ -d /tmp/.npm ]; then
chown -R nodejs:nodejs /tmp/.npm
fi
"/opt/elasticbeanstalk/hooks/appdeploy/pre/55npm_cleanup.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Remove all npm tmp files leftover by npm shrinkwrap flow.
# https://github.com/npm/npm/issues/6855
rm -rf /tmp/npm-*
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Only rebuild modules, don't install like Elastic Beanstalk tries to do
# by default: https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663.
# package.json isn't changing on a config deploy, and all the existing
# modules should be cached.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script.
# But their default script actually doesn't work at all, since the app
# staging dir, where they try to run `npm install`, doesn't exist during
# config deploys, so ebnode.py just aborts:
# https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L140
. /opt/elasticbeanstalk/env.vars
cd $EB_APP_DEPLOY_DIR
sudo -u nodejs -H npm rebuild --production
if [ -d /tmp/.npm ]; then
chown -R nodejs:nodejs /tmp/.npm
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment