We have a fairly simple node.js app, but due to AWS Elastic Beanstalk deployment mechanism, it takes about 5 minutes to roll-out a new version (via git aws.push
) even after a single file commit.
I.e. the commit itself (and upload) is fast (only 1 file to push), but then Elastic Beanstalk fetches whole package from S3, unzips it and runs npm install
, which causes node-gyp to compile some modules. Upon installation/building completion, Elastic Beanstalk wipes /var/app/current
and replaces it with the new app version.
Needless to say, constant node_modules rebuilding is not necessary, and rebuilding that takes 30 seconds on my old Macbook Air, takes >5 mins on a ec2.micro instance, not fun.
I see two approaches here:
- tweak
/opt/containerfiles/ebnode.py
and play with node_modules location to avoid its removal and rebuilding upon deployment. - set up a git repo on Elastic Beanstalk EC2 instance and basically re-write deployment procedure ourselves, so /var/app/current receives pushes and runs
npm install
only when necessary (which makes Elastic Beanstalk to look like OpsWorks..)
Both options lack grace and are prone to issues when Amazon updates their Elastic Beanstalk hooks and architecture.
Maybe somebody has a better idea how to avoid constant rebuilding of node_modules that are already present in the app dir? Thank you.
I had 10+ minute builds when I would deploy. The solution was much simpler than others have came up with... Just check node_modules into git! See http://www.futurealoof.com/posts/nodemodules-in-git.html for the reasoning
I've found a quick solution to this. I looked through the build scripts that Amazon are using and they only run
npm install
if package.json is present. So after your initial deploy you can change it to_package.json
andnpm install
won't run anymore! It's not the best solution but it's a quick fix if you need one!There's npm package that's overwriting default EB behaviour for
npm install
command by truncating following files:https://www.npmjs.com/package/eb-disable-npm
Might be better than just copying script from SO, since this package is maintained and probably will be updated when EB behaviour will change.
25/01/13 NOTE: updated scripts to run npm -g version upgrade (only once, on initial instance roll out or rebuild) and to avoid NPM operations during EB configuration change (when app dir is not present, to avoid error and to speed up configuration updates).
Okay, Elastic Beanstalk behaves dodgy with recent node.js builds (including presumably supported v.0.10.10), so I decided to go ahead and tweak EB to do the following:
Basically, I use env.config to replace deploy&config hooks with customized ones (see below). Also, in a default EB container setup some env variables are missing (
$HOME
for example) andnode-gyp
sometimes fails during rebuild because of it (took me 2 hours of googling and reinstalling libxmljs to resolve this).Below are the files to be included along with your build. You can inject them via env.config as inline code or via
source: URL
(as in this example)env.vars
(desired node version & arch are included here and in env.config, see below)40install_node.sh
(fetch and ungzip desired node.js version, make global symlinks, update global npm version)50npm.sh
(creates /var/node_modules, symlinks it to app dir and runs npm install. You can install any module globally from here, they will land in /root/.npm)env.config
(note node version here too, and to be safe, put desired node version in env config in AWS console as well. I'm not certain which of these settings will take precedence.)There you have it: on t1.micro instance deployment now takes 20-30 secs instead of 10-15 minutes! If you deploy 10 times a day, this tweak will save you 3 (three) weeks in a year. Hope it helps and special thanks to AWS EB staff for my lost weekend :)
Thanks Kirill, it was really helpful !
I'm just sharing my config file for people who just look the simple solution to the
npm install
. This file needs to be placed in the.ebextensions
folder of the project, it is lighter since it doesn't include last version of node installation, and ready to use.It also dynamically checks the node version installed, so no need for it to be included in the env.vars file.
.ebextensions/00_deploy_npm.config