Skip to content

Instantly share code, notes, and snippets.

@etiennea
Created March 29, 2014 19:58
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save etiennea/9861792 to your computer and use it in GitHub Desktop.
Save etiennea/9861792 to your computer and use it in GitHub Desktop.
AWS Elastic Beanstalk node.js configuration for quick deployments
packages:
yum:
git: []
gcc: []
make: []
openssl-devel: []
ImageMagick: []
option_settings:
- option_name: NODE_ENV
value: production
- option_name: DB_NAME
value: voice
- option_name: NODE_ENV
value: production
- namespace: aws:elasticbeanstalk:container:nodejs
option_name: NodeVersion
value: 0.10.26
files:
"/opt/elasticbeanstalk/env.vars" :
mode: "000775"
owner: root
group: users
source: https://s3.amazonaws.com/myapp/env.vars
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh" :
mode: "000775"
owner: root
group: users
source: https://s3.amazonaws.com/myapp/50npm.sh
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh" :
mode: "000775"
owner: root
group: users
source: https://s3.amazonaws.com/myapp/50npm.sh
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh" :
mode: "000666"
owner: root
group: users
content: |
#no need to run npm install during configdeploy
"/opt/elasticbeanstalk/hooks/appdeploy/pre/40install_node.sh" :
mode: "000775"
owner: root
group: users
source: https://s3.amazonaws.com/myapp/40install_node.sh
#!/bin/bash
#source env variables including node version
. /opt/elasticbeanstalk/env.vars
function error_exit
{
eventHelper.py --msg "$1" --severity ERROR
exit $2
}
#UNCOMMENT to update npm, otherwise will be updated on instance init or rebuild
#rm -f /opt/elasticbeanstalk/node-install/npm_updated
#download and extract desired node.js version
OUT=$( [ ! -d "/opt/elasticbeanstalk/node-install" ] && mkdir /opt/elasticbeanstalk/node-install ; cd /opt/elasticbeanstalk/node-install/ && wget -nc http://nodejs.org/dist/v$NODE_VER/node-v$NODE_VER-linux-$ARCH.tar.gz && tar --skip-old-files -xzpf node-v$NODE_VER-linux-$ARCH.tar.gz) || error_exit "Failed to UPDATE node version. $OUT" $?.
echo $OUT
#make sure node binaries can be found globally
if [ ! -L /usr/bin/node ]; then
ln -s /opt/elasticbeanstalk/node-install/node-v$NODE_VER-linux-$ARCH/bin/node /usr/bin/node
fi
if [ ! -L /usr/bin/npm ]; then
ln -s /opt/elasticbeanstalk/node-install/node-v$NODE_VER-linux-$ARCH/bin/npm /usr/bin/npm
fi
if [ ! -f "/opt/elasticbeanstalk/node-install/npm_updated" ]; then
/opt/elasticbeanstalk/node-install/node-v$NODE_VER-linux-$ARCH/bin/ && /opt/elasticbeanstalk/node-install/node-v$NODE_VER-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. To update, please uncomment 40install_node.sh:12"
fi
#!/bin/bash
. /opt/elasticbeanstalk/env.vars
function error_exit
{
eventHelper.py --msg "$1" --severity ERROR
exit $2
}
#install not-installed yet app node_modules
if [ ! -d "/var/node_modules" ]; then
mkdir /var/node_modules ;
fi
if [ -d /tmp/deployment/application ]; then
ln -s /var/node_modules /tmp/deployment/application/
fi
OUT=$([ -d "/tmp/deployment/application" ] && cd /tmp/deployment/application && /opt/elasticbeanstalk/node-install/node-v$NODE_VER-linux-$ARCH/bin/npm install --production 2>&1) || error_exit "Failed to run npm install. $OUT" $?
echo $OUT
export HOME=/root
export NPM_CONFIG_LOGLEVEL=error
export NPM_CONFIG_PRODUCTION=true
export NODE_VER=0.10.26
export ARCH=x64
export PATH="$PATH:/opt/elasticbeanstalk/node-install/node-v$NODE_VER-linux-$ARCH/bin/:/root/.npm"
export NODE_PATH=/usr/bin/node
@JehandadK
Copy link

Why is there repetition in first file.

  • option_name: NODE_ENV
    value: production
    • option_name: DB_NAME
      value: voice
    • option_name: NODE_ENV
      value: production

@stanleycyang
Copy link

This script breaks on the 2nd and subsequent pushes.

{"status":"FAILURE","api_version":"1.0","results":[{"status":"FAILURE","msg":"(TRUNCATED)...\"/opt/elasticbeanstalk/containerfiles/ebnode.py\", line 166, in npm_install\n    raise e\nsubprocess.CalledProcessError: Command '['/opt/elasticbeanstalk/node-install/node-v4.2.1-linux-x64/bin/npm', '--production', 'install']' returned non-zero exit status 1. \nHook /opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh failed. For more detail, check /var/log/eb-activity.log using console or EB CLI","returncode":1,"events":[{"msg":"Failed to run npm install. Snapshot logs for more details.","severity":"ERROR","timestamp":1450153318414}]}],"truncated":"true"}

How would I get around this?

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