[Hack] Update NodeJS on AWS Elastic Beanstalk

ElasticBeanstalk_Benefit_Productivity

For a project needs, I have to use NodeJS 6.x on AWS Elastic Beanstalk. The last LTS version was 4.x and I want to use some features in the 6.x.
I’ve found a trick to use the last version of NodeJS : You just have to install nodeJS like on any Linux system. Then, just replace the binary used by AWS by our installation 🙂
Please create first .ebextensions/01-node.config

freeze the version

Firstly, we have to lock the version used by AWS to prevent an update which will change the symlink.

  1. option_settings:
  2.     - namespace: aws:elasticbeanstalk:container:nodejs
  3.       option_name: NodeVersion
  4.       value: "4.4.3"
option_settings:
    - namespace: aws:elasticbeanstalk:container:nodejs
      option_name: NodeVersion
      value: "4.4.3"

Install nodeJS

We will install nodeJS, and some compilation tools to permit installation of C++ modules with NPM.
if /usr/bin/node exist, just ignore the command.

  1. commands:
  2.     100-node-script-installer:
  3.         command: "curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -"
  4.         test: "[ ! -x /usr/bin/node ]"
  5.     200-install-nodeJS:
  6.         command: "yum -y install nodejs gcc-c++ make"
  7.         test: "[ ! -x /usr/bin/node ]"
commands:
    100-node-script-installer:
        command: "curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -"
        test: "[ ! -x /usr/bin/node ]"
    200-install-nodeJS:
        command: "yum -y install nodejs gcc-c++ make"
        test: "[ ! -x /usr/bin/node ]"

Replace binaries AWS EBS

Just rename old binaries to keep a backup. Use this backup to test if the binaries was already changed.

  1. commands:
  2.     300-replace-node:
  3.         command: "mv node node.old && ln -s /usr/bin/node node"
  4.         cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
  5.         test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/node.old ]"
  6.     350-replace-npm:
  7.         command: "mv npm npm.old && ln -s /usr/bin/npm npm"
  8.         cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
  9.         test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/npm.old ]"
commands:
    300-replace-node:
        command: "mv node node.old && ln -s /usr/bin/node node"
        cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
        test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/node.old ]"
    350-replace-npm:
        command: "mv npm npm.old && ln -s /usr/bin/npm npm"
        cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
        test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/npm.old ]"

Full script

This script should work if you place this in the right directory. Please see the AWS documentation for more informations.

  1. option_settings:
  2.     - namespace: aws:elasticbeanstalk:container:nodejs
  3.       option_name: NodeVersion
  4.       value: "4.4.3"
  5. commands:
  6.     100-node-script-installer:
  7.         command: "curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -"
  8.         test: "[ ! -x /usr/bin/node ]"
  9.     200-install-nodeJS:
  10.         command: "yum -y install nodejs gcc-c++ make"
  11.         test: "[ ! -x /usr/bin/node ]"
  12.     300-replace-node:
  13.         command: "mv node node.old && ln -s /usr/bin/node node"
  14.         cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
  15.         test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/node.old ]"
  16.     350-replace-npm:
  17.         command: "mv npm npm.old && ln -s /usr/bin/npm npm"
  18.         cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
  19.         test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/npm.old ]"
option_settings:
    - namespace: aws:elasticbeanstalk:container:nodejs
      option_name: NodeVersion
      value: "4.4.3"
commands:
    100-node-script-installer:
        command: "curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -"
        test: "[ ! -x /usr/bin/node ]"
    200-install-nodeJS:
        command: "yum -y install nodejs gcc-c++ make"
        test: "[ ! -x /usr/bin/node ]"
    300-replace-node:
        command: "mv node node.old && ln -s /usr/bin/node node"
        cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
        test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/node.old ]"
    350-replace-npm:
        command: "mv npm npm.old && ln -s /usr/bin/npm npm"
        cwd: "/opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin"
        test: "[ ! -x /opt/elasticbeanstalk/node-install/node-v4.4.3-linux-x64/bin/npm.old ]"