avoid rebuilding node_modules in elastic beanstalk

前端 未结 5 1299
礼貌的吻别
礼貌的吻别 2020-12-12 10:00

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

5条回答
  •  长情又很酷
    2020-12-12 10:48

    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

    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
    

提交回复
热议问题