避免在弹性 beantalk 中重建 node_modules [英] avoid rebuilding node_modules in elastic beanstalk

查看:28
本文介绍了避免在弹性 beantalk 中重建 node_modules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个相当简单的 node.js 应用程序,但由于 AWS Elastic Beanstalk 部署机制,即使在之后推出新版本(通过 git aws.push)也需要大约 5 分钟的时间单个文件提交.

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 个文件要推送),但随后 Elastic Beanstalk 从 S3 获取整个包,解压缩并运行 npm install,这会导致 node-gyp 编译一些模块.安装/构建完成后,Elastic Beanstalk 会擦除 /var/app/current 并将其替换为新的应用程序版本.

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 重建是没有必要的,在我的旧 Macbook Air 上重建需要 30 秒,在 ec2.micro 实例上需要 5 分钟以上,不好玩.

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:

  1. 调整 /opt/containerfiles/ebnode.py 并使用 node_modules 位置以避免在部署时将其删除和重建.
  2. 在 Elastic Beanstalk EC2 实例上设置一个 git repo 并基本上自己重写部署过程,因此/var/app/current 仅在必要时接收推送并运行 npm install(这使得 Elastic Beanstalk看起来像 OpsWorks..)
  1. tweak /opt/containerfiles/ebnode.py and play with node_modules location to avoid its removal and rebuilding upon deployment.
  2. 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..)

当 Amazon 更新其 Elastic Beanstalk 挂钩和架构时,这两种选择都缺乏优雅,并且容易出现问题.

Both options lack grace and are prone to issues when Amazon updates their Elastic Beanstalk hooks and architecture.

也许有人有更好的主意如何避免不断重建应用程序目录中已经存在的 node_modules?谢谢.

Maybe somebody has a better idea how to avoid constant rebuilding of node_modules that are already present in the app dir? Thank you.

推荐答案

感谢 Kirill,这真的很有帮助!

Thanks Kirill, it was really helpful !

我只是为那些只是寻找 npm install 的简单解决方案的人分享我的配置文件.这个文件需要放在项目的.ebextensions文件夹中,它比较轻,因为它不包含最新版本的node安装,可以直接使用.

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

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

这篇关于避免在弹性 beantalk 中重建 node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆