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.
在我的弹性Beanstalk实例上,我如何运行npm更新-g npm ?对于每个实例来说,手动运行update命令是相当容易的,但是这不会通过扩展事件来完成,因为会自动添加更多的实例。
How can i get the latest version of NPM on Elastic Beanstalk instances, in a way that works through an auto-scaling event?
我怎样才能在弹性Beanstalk实例上获得最新版本的NPM,以一种通过自动缩放事件进行工作的方式?
3 个解决方案
#1
5
It turns out this one is tricky, took a bit of digging and experimentation.
这是一个棘手的问题,需要进行一些挖掘和实验。
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:
首先,简要介绍一下弹性豆茎的生命周期。在部署的每个实例上,AWS脚本执行了几个步骤。对于一个节点。JS服务器,有两种兴趣:
- Install Node.JS
- 安装node . js
- Run
npm install
- 运行npm安装
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.
安装节点。JS是我们可以介入并施展魔法的地方。大多数错误导致了想要做魔术或其他事情的愿望,而beanstalk实例则来自npm安装步骤。
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:
回到主题,用于在beanstalk实例上安装节点的脚本AWS是/opt/ ticbeanstalk/hook /appdeploy/pre/40install_node.sh。通常是这样的:
#!/bin/bash
set -xe
/opt/elasticbeanstalk/containerfiles/ebnode.py --action node-install
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?
该脚本安装了一组不同的节点版本,包括/opt/弹珠柄/节点安装,包括在beanstalk配置中选择的一个。如果运行npm update -g npm,并将其中一个节点版本放在该文件夹中,不是很好吗?
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:
事实证明,beanstalk提供了一种机制,可以在部署期间对每个实例交换文件。基本上,您可以在应用程序的. ebex紧张文件夹中配置YAML文件,有两种方法可以引用文件内容,在一行中,或者在s3存储桶中。我使用s3 bucket方法,给出一个节点。配置YAML是这样的:
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
Note the S3Access
property. We keep the bucket private, granting access to the aws-elasticbeanstalk-ec2-role
using IAM.
注意S3Access属性。我们使用IAM来保持bucket的私有,允许访问aws-弹性beanstalk-ec2。
Now all we need is a version of 40install_node.sh
running the npm update:
现在我们需要的是一个40install_node的版本。sh运行npm更新:
#!/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
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.
您也可以将任何定制的节点安装在这个文件中。记住要注意到节点的路径,在beanstalk配置中,它会随着节点版本的增加而变化。
#2
4
If you don't want to add a script on to S3, you can simply put the following in your .ebextensions
, assuming you're running node v12, for other versions the path node-v0.12.6-linux-x64
will be different.
如果您不想在S3中添加一个脚本,您可以简单地在. ebex紧张中添加以下内容,假设您运行的是节点v12,对于其他版本,路径node-v0.12.6-linux-x64将是不同的。
commands:
01-updatenpmv3:
command: PATH=$PATH:/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/ && npm update -g npm
cwd: /opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/
#3
1
This should not be necessary. You can specify in your configuration what version of Nodejs you want to be running, and thus would be paired with the appropriate npm version. If you did want to have an older version of nodejs with a newer version of npm, then this exercise would be warranted.
这是不必要的。您可以在配置中指定要运行的node . js的版本,因此将与适当的npm版本进行配对。如果您确实想要使用更新版本的npm的旧版本的nodejs,那么这个练习是有必要的。
In this case I would probably step on the npm install script in a file dropped into the .ebextensions folder (e.g. 00_default.config):
在这种情况下,我很可能会在一个文件中加入npm安装脚本,并将其放入. ebex文件夹(例如:00_default.config):
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/bin/bash
#==============================================================================
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the "License"). You may not use
# this file except in compliance with the License. A copy of the License is
# located at
#
# http://aws.amazon.com/asl/
#
# or in the "license" file accompanying this file. This file is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
# implied. See the License for the specific language governing permissions
# and limitations under the License.
#==============================================================================
set -xe
/opt/elasticbeanstalk/containerfiles/ebnode.py --action npm-install
# Update npm
cd /opt/elasticbeanstalk/node-install/CURRENTNODEVERSIONHERE/bin/ && /opt/elasticbeanstalk/node-install/CURRENTNODEVERSIONHERE/bin/npm update npm -g
Replace CURRENTNODEVERSIONHERE with the correct path/version for your setup.
用正确的路径/版本替换CURRENTNODEVERSIONHERE。
These are the available versions I see on my instances. You'll want to check your own resources to see what you're working with.
这些是我在实例中看到的可用版本。您将需要检查您自己的资源,以了解您正在处理的内容。
Location: /opt/elasticbeanstalk/node-install
地点:/ opt / elasticbeanstalk / node-install
- node-v0.10.46-linux-x64
- node-v0.10.46-linux-x64
- node-v0.12.15-linux-x64
- node-v0.12.15-linux-x64
- node-v0.8.28-linux-x64
- node-v0.8.28-linux-x64
- node-v4.4.6-linux-x64
- node-v4.4.6-linux-x64
- node-v5.12.0-linux-x64
- node-v5.12.0-linux-x64
- node-v6.2.2-linux-x64
- node-v6.2.2-linux-x64
#1
5
It turns out this one is tricky, took a bit of digging and experimentation.
这是一个棘手的问题,需要进行一些挖掘和实验。
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:
首先,简要介绍一下弹性豆茎的生命周期。在部署的每个实例上,AWS脚本执行了几个步骤。对于一个节点。JS服务器,有两种兴趣:
- Install Node.JS
- 安装node . js
- Run
npm install
- 运行npm安装
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.
安装节点。JS是我们可以介入并施展魔法的地方。大多数错误导致了想要做魔术或其他事情的愿望,而beanstalk实例则来自npm安装步骤。
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:
回到主题,用于在beanstalk实例上安装节点的脚本AWS是/opt/ ticbeanstalk/hook /appdeploy/pre/40install_node.sh。通常是这样的:
#!/bin/bash
set -xe
/opt/elasticbeanstalk/containerfiles/ebnode.py --action node-install
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?
该脚本安装了一组不同的节点版本,包括/opt/弹珠柄/节点安装,包括在beanstalk配置中选择的一个。如果运行npm update -g npm,并将其中一个节点版本放在该文件夹中,不是很好吗?
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:
事实证明,beanstalk提供了一种机制,可以在部署期间对每个实例交换文件。基本上,您可以在应用程序的. ebex紧张文件夹中配置YAML文件,有两种方法可以引用文件内容,在一行中,或者在s3存储桶中。我使用s3 bucket方法,给出一个节点。配置YAML是这样的:
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
Note the S3Access
property. We keep the bucket private, granting access to the aws-elasticbeanstalk-ec2-role
using IAM.
注意S3Access属性。我们使用IAM来保持bucket的私有,允许访问aws-弹性beanstalk-ec2。
Now all we need is a version of 40install_node.sh
running the npm update:
现在我们需要的是一个40install_node的版本。sh运行npm更新:
#!/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
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.
您也可以将任何定制的节点安装在这个文件中。记住要注意到节点的路径,在beanstalk配置中,它会随着节点版本的增加而变化。
#2
4
If you don't want to add a script on to S3, you can simply put the following in your .ebextensions
, assuming you're running node v12, for other versions the path node-v0.12.6-linux-x64
will be different.
如果您不想在S3中添加一个脚本,您可以简单地在. ebex紧张中添加以下内容,假设您运行的是节点v12,对于其他版本,路径node-v0.12.6-linux-x64将是不同的。
commands:
01-updatenpmv3:
command: PATH=$PATH:/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/ && npm update -g npm
cwd: /opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/
#3
1
This should not be necessary. You can specify in your configuration what version of Nodejs you want to be running, and thus would be paired with the appropriate npm version. If you did want to have an older version of nodejs with a newer version of npm, then this exercise would be warranted.
这是不必要的。您可以在配置中指定要运行的node . js的版本,因此将与适当的npm版本进行配对。如果您确实想要使用更新版本的npm的旧版本的nodejs,那么这个练习是有必要的。
In this case I would probably step on the npm install script in a file dropped into the .ebextensions folder (e.g. 00_default.config):
在这种情况下,我很可能会在一个文件中加入npm安装脚本,并将其放入. ebex文件夹(例如:00_default.config):
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/bin/bash
#==============================================================================
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the "License"). You may not use
# this file except in compliance with the License. A copy of the License is
# located at
#
# http://aws.amazon.com/asl/
#
# or in the "license" file accompanying this file. This file is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
# implied. See the License for the specific language governing permissions
# and limitations under the License.
#==============================================================================
set -xe
/opt/elasticbeanstalk/containerfiles/ebnode.py --action npm-install
# Update npm
cd /opt/elasticbeanstalk/node-install/CURRENTNODEVERSIONHERE/bin/ && /opt/elasticbeanstalk/node-install/CURRENTNODEVERSIONHERE/bin/npm update npm -g
Replace CURRENTNODEVERSIONHERE with the correct path/version for your setup.
用正确的路径/版本替换CURRENTNODEVERSIONHERE。
These are the available versions I see on my instances. You'll want to check your own resources to see what you're working with.
这些是我在实例中看到的可用版本。您将需要检查您自己的资源,以了解您正在处理的内容。
Location: /opt/elasticbeanstalk/node-install
地点:/ opt / elasticbeanstalk / node-install
- node-v0.10.46-linux-x64
- node-v0.10.46-linux-x64
- node-v0.12.15-linux-x64
- node-v0.12.15-linux-x64
- node-v0.8.28-linux-x64
- node-v0.8.28-linux-x64
- node-v4.4.6-linux-x64
- node-v4.4.6-linux-x64
- node-v5.12.0-linux-x64
- node-v5.12.0-linux-x64
- node-v6.2.2-linux-x64
- node-v6.2.2-linux-x64