如何在Elastic Beanstalk上运行`npm update -g npm`? [英] How do I run `npm update -g npm` on Elastic Beanstalk?

查看:76
本文介绍了如何在Elastic Beanstalk上运行`npm update -g npm`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Elastic Beanstalk实例启动时,如何在它们上运行npm update -g npm?封装到每个实例中以手动运行update命令是相当容易的,但这不会通过扩展事件起作用,因为会自动添加更多实例.

How can I run npm update -g npm on my Elastic Beanstalk instances as they spin up? It's fairly easy to shell into each instance to run the update command manually, but this won't work through a scaling event, as more instances are added automatically.

如何通过自动缩放事件来在Elastic Beanstalk实例上获取最新版本的NPM?

How can i get the latest version of NPM on Elastic Beanstalk instances, in a way that works through an auto-scaling event?

推荐答案

事实证明,这很棘手,需要进行一些挖掘和实验.

It turns out this one is tricky, took a bit of digging and experimentation.

首先,简要介绍一下Elastic Beanstalk的生命周期.部署时,在每个实例上运行的AWS脚本需要执行几个步骤.对于Node.JS服务器,有两个有趣的地方:

First, a quick bit about Elastic Beanstalk's lifecycle. There are several steps taken by AWS scripts running on each instance on deploy. For a Node.JS server, there are two of interest:

  • 安装Node.JS
  • 运行npm install

安装Node.JS是我们可以介入并做一些魔术的地方.大多数提示想要对beantalk实例执行魔术或其他操作的错误来自npm install步骤.

Installing Node.JS is where we can step in and do some magic. Most errors prompting the desire to do magic, or other things, to a beanstalk instance come from the npm install step.

回到主题,用于在beantalk实例上安装节点的AWS脚本为/opt/elasticbeanstalk/hooks/appdeploy/pre/40install_node.sh.通常看起来像这样:

Getting back on topic, the script AWS used to install node on beanstalk instances is /opt/elasticbeanstalk/hooks/appdeploy/pre/40install_node.sh. It usually looks like this:

#!/bin/bash

set -xe

/opt/elasticbeanstalk/containerfiles/ebnode.py --action node-install

此脚本将许多不同的节点版本安装到/opt/elasticbeanstalk/node-install,包括在beantalk配置中选择的版本.在其中一个节点版本位于该文件夹中的情况下运行npm update -g npm会不会很好?

This script installs a bunch of different node versions to /opt/elasticbeanstalk/node-install, including the one selected in the beanstalk configuration. Wouldn't it be nice to run npm update -g npm with one of the node versions sitting in that folder?

事实证明,beanstalk提供了一种在部署期间交换每个实例上的文件的机制.基本上,您可以在应用程序的.ebextensions文件夹中配置YAML文件.有两种方法可以在行中或在s3存储桶中引用文件内容.我使用s3存储桶方法,使node.config YAML看起来像这样:

It turns out beanstalk supplies a mechanism to swap out files on each instance during deploy. Basically you configure YAML files in a .ebextensions folder in your app. There are two ways to reference the file contents, in line, or in an s3 bucket. I use the s3 bucket approach, giving a node.config YAML looking like this:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/40install_node.sh" :
    mode: "000775"
    owner: root
    group: users
    source: https://s3.amazonaws.com/bucketname/40install_node.sh
    authentication: S3Access
Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Access:
          type: S3
          roleName: aws-elasticbeanstalk-ec2-role
          buckets: bucketname

请注意S3Access属性.我们将存储桶保持私有状态,并使用IAM授予对aws-elasticbeanstalk-ec2-role的访问权限.

Note the S3Access property. We keep the bucket private, granting access to the aws-elasticbeanstalk-ec2-role using IAM.

现在我们需要的是运行npm更新的40install_node.sh版本:

Now all we need is a version of 40install_node.sh running the npm update:

#!/bin/bash

set -xe

/opt/elasticbeanstalk/containerfiles/ebnode.py --action node-install

# Update npm
cd /opt/elasticbeanstalk/node-install/node-v0.12.2-linux-x64/bin/ && /opt/elasticbeanstalk/node-install/node-v0.12.2-linux-x64/bin/npm update npm -g

您也可以将节点安装的任何自定义设置都放在此文件中.只要记住注意节点的路径,它就会随着beantalk配置中节点版本的增加而改变.

You can put any customization of your node install in this file as well. Just remember to keep an eye on the path to node, it will change as node versions go up in the beanstalk configuration.

这篇关于如何在Elastic Beanstalk上运行`npm update -g npm`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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