自定义AWS ElasticBeanstalk NodeJS安装(使用纱线)

时间:2017-01-15 02:31:54

标签: node.js amazon-web-services npm amazon-elastic-beanstalk yarnpkg

是否可以使用yarn package manager而不是NPM配置EBS来安装我的NodeJS应用程序?

6 个答案:

答案 0 :(得分:24)

我已经找到了办法,但这有点儿骇客。

  1. 创建.ebextensions/yarn.config文件。 (名称不一定是'纱线'。)
  2. 将此内容放入文件中:

    files:
    # Runs right before `npm install` in '.../50npm.sh'
    "/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
        mode: "000775"
        owner: root
        group: users
        content: |
            #!/bin/bash
    
            app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
    
            # install node
            curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -;
    
            # install yarn
            curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo;
            yum -y install yarn;
    
            # install node_modules with yarn
            cd "${app}";
            yarn --production;
    
  3. 这个ebextension创建了一个文件,可以完成3件事:

    1. 安装节点。
    2. 安装纱线。
    3. 使用yarn安装node_modules。
    4. 为了使Elastic Beanstalk在运行yarn install之前运行npm install,该文件将在/opt/elasticbeanstalk/hooks/appdeploy/pre下创建。这会将文件转换为预部署挂钩,这意味着Elastic Beanstalk将在部署的第一阶段运行它。默认情况下,此目录中有另一个名为50npm.sh的文件,该文件运行npm install。由于Elastic Beanstalk按字母顺序运行此目录中的文件,49yarn.sh(我们的文件)将在50npm.sh(默认文件)之前运行,导致yarn installnpm install之前运行。< / p>

      一个潜在的问题是,在部署阶段的此时,Elastic Beanstalk UI(Configuration&gt; Software Configuration下)中设置的环境变量不可用。如果你有一个用于安装私有npm模块的npm auth令牌,这是一个大问题。

      另一个潜在的问题是,这会手动安装节点,因此&#34;节点版本&#34;您在Elastic Beanstalk UI中指定(在Configuration&gt; Software Configuration下)对您的应用程序使用的节点版本没有影响;你需要在这个ebextension中指定它。 Elastic Beanstalk 50npm.sh都安装节点并运行npm install。由于我们必须在该文件运行之前运行yarn install,因此我们还必须手动安装节点。然后,当Elastic Beanstalk进入安装节点时,它会检测到该节点已经安装但是没有验证它是否是正确的版本,因此它会跳过节点安装。

      作为参考,纱线安装说明来自此处:https://yarnpkg.com/docs/install#linux-tab

答案 1 :(得分:7)

我按照https://yarnpkg.com/lang/en/docs/install/

上的说明执行了此操作
commands:
  01_install_yarn:
    command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - && sudo yum install yarn -y"

答案 2 :(得分:3)

不得不重新审视此问题,因为即使我们将其设置为EB UI中的节点12,我们也无法弄清为什么停留在节点8上。似乎如果安装全局节点,它将覆盖版本设置。代替安装全局节点,它使用Elastic Beanstalk节点安装并将其添加到路径。您必须在纱线安装脚本的开头再次添加PATH,但这似乎是使用纱线的侵入性最低的方法。

content: |
  #!/usr/bin/env bash
  set -euxo pipefail

  EB_NODE_VERSION=$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)
  echo "EB node version: $(EB_NODE_VERSION)"

  # Make sure Node binaries can be found (required to run npm).
  # And this lets us invoke npm more simply too.
  export PATH=/opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-x64/bin:$PATH

  if yarn -v; then
    echo 'Yarn already installed.'
  else
    echo 'Installing yarn...'
    npm install yarn -g
  fi

答案 3 :(得分:2)

通过这种方式,您仍然可以通过Elastic Beanstalks仪表板控制节点版本。

感谢这个问题!没有它就无法解决这个问题:D

 "/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
        mode:    "000755"
        owner:   root
        group:   users
        content: |
           #!/usr/bin/env bash
           #
           # Prevent installing or rebuilding like Elastic Beanstalk tries to do by
           # default.
           #
           # Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script
           # (https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663).
    "/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh":
        mode:    "000755"
        owner:   root
        group:   users
        content: |
           #!/usr/bin/env bash
           #
           # Prevent installing or rebuilding like Elastic Beanstalk tries to do by
           # default.
           #
           # Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script.
           # But their default script actually doesn't work at all, since the app
           # staging dir, where they try to run `npm install`, doesn't exist during
           # config deploys, so ebnode.py just aborts:
           # https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L140
    "/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
        mode:    "000775"
        owner:   root
        group:   users
        content: |
           tmp="$(mktemp || bail)";
           app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
           version="$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)";
           echo $version
           major="$(cut -d'.' -f1 <<<${version})"
           yum -y install python26 python26-libs
           wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo;

           wget "https://rpm.nodesource.com/pub_${major}.x/el/7/x86_64/nodejs-${version}-1nodesource.x86_64.rpm" -O "${tmp}";
           rpm -i --nosignature --force "${tmp}";
           rm -f "${tmp}";

           yum -y install yarn;

           cd "${app}";
           yarn --production;

答案 4 :(得分:0)

EB默认支持npm,因此我建议通过 .ebextensions 中的部署脚本安装 yarn 。应该这样做。

答案 5 :(得分:0)

由于get-config不再存在于新的Amazon Linux 2平台中,我们不得不想出另一种干净的方法来做到这一点,并提出了以下建议:

container_commands:
  01_npm_install_yarn:
    command: "npm install -g yarn"
  10_yarn_install:
    command: 'PATH="$PATH:$(dirname $(readlink $(which node)))" yarn install'

您可能希望将PATH=逻辑放入脚本中,并在每个yarn命令之前调用它,以使扩展区中的command:指令清晰。

此外,请注意,如果您使用yum软件包管理器安装yarn,则会完全破坏Beanstalk提供的NodeJS版本管理(因为其背后的黑魔法在/bin和{ {1}}。

相关问题