避免弹性魔豆重建node_modules [英] avoid rebuilding node_modules in elastic beanstalk
问题描述
我们有一个相当简单的Node.js应用程序,但由于AWS弹性魔豆部署机制,大约需要5分钟,转出一个新版本(通过混帐aws.push
)甚至在一个单独的文件提交。
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.
即。在致力于(和上传)速度快(只有1个文件推),但随后弹性魔豆取整包从S3,解压缩并运行 NPM安装
,这将导致节点-gyp编译一些模块。在安装/建设完成后,弹性魔豆湿巾的/ var /应用/电流
,并与新的应用程序的版本替换它。
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.
不用说,恒node_modules重建是没有必要的,而且重建的需要30秒在我的旧的Macbook Air,需要> 5分钟,在ec2.micro实例,不好玩。
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:
- 的调整
/opt/containerfiles/ebnode.py
和node_modules位置开始播放,以避免其拆除,并在部署时重建。 - 设置了一个混帐回购协议上的弹性魔豆EC2实例,基本上重新写部署过程自己,所以在/ var /应用/电流接收推并运行
NPM安装
只有当必要(这使得弹性魔豆的样子OpsWorks ..)
- 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.
也许有人有一个更好的主意如何避免node_modules这已经是美元的应用程序目录p $ psent不断重建?谢谢你。
Maybe somebody has a better idea how to avoid constant rebuilding of node_modules that are already present in the app dir? Thank you.
推荐答案
感谢基里尔,这是非常有帮助!
Thanks Kirill, it was really helpful !
我只是分享我的配置文件的人谁只看简单的解决方案到 NPM安装
。该文件需要放置在项目的 .ebextensions
文件夹,它是轻,因为它不包括最新版本的节点安装,并准备使用。
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.
这也动态地检查安装的节点版本,所以没有必要为它包含在env.vars文件。
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
.ebextensions/00_deploy_npm.config
files:
"/opt/elasticbeanstalk/env.vars" :
mode: "000775"
owner: root
group: users
content: |
export NPM_CONFIG_LOGLEVEL=error
export NODE_PATH=`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh" :
mode: "000775"
owner: root
group: users
content: |
#!/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 && $NODE_PATH/npm install 2>&1) || error_exit "Failed to run npm install. $OUT" $?
echo $OUT
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh" :
mode: "000666"
owner: root
group: users
content: |
#no need to run npm install during configdeploy
这篇关于避免弹性魔豆重建node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!