【发布时间】:2015-09-24 10:36:39
【问题描述】:
如何在我的 Elastic Beanstalk 实例启动时运行 npm update -g npm?在每个实例中手动运行更新命令相当容易,但这不适用于扩展事件,因为会自动添加更多实例。
如何通过自动缩放事件在 Elastic Beanstalk 实例上获取最新版本的 NPM?
【问题讨论】:
标签: node.js amazon-web-services npm amazon-elastic-beanstalk
如何在我的 Elastic Beanstalk 实例启动时运行 npm update -g npm?在每个实例中手动运行更新命令相当容易,但这不适用于扩展事件,因为会自动添加更多实例。
如何通过自动缩放事件在 Elastic Beanstalk 实例上获取最新版本的 NPM?
【问题讨论】:
标签: node.js amazon-web-services npm amazon-elastic-beanstalk
事实证明,这个很棘手,需要一些挖掘和实验。
首先,简要介绍一下 Elastic Beanstalk 的生命周期。部署时在每个实例上运行的 AWS 脚本会采取几个步骤。对于 Node.JS 服务器,有两个有趣的地方:
npm install
安装 Node.JS 是我们可以介入并施展魔法的地方。大多数促使想要对 beanstalk 实例执行魔法或其他事情的错误来自 npm install 步骤。
回到主题,AWS 用于在 beanstalk 实例上安装节点的脚本是 /opt/elasticbeanstalk/hooks/appdeploy/pre/40install_node.sh。它通常看起来像这样:
#!/bin/bash
set -xe
/opt/elasticbeanstalk/containerfiles/ebnode.py --action node-install
此脚本将一堆不同的节点版本安装到/opt/elasticbeanstalk/node-install,包括在 beanstalk 配置中选择的那个。使用该文件夹中的一个节点版本运行 npm update -g npm 不是很好吗?
原来 beanstalk 提供了一种机制来在部署期间交换每个实例上的文件。基本上,您在应用程序的 .ebextensions 文件夹中配置 YAML 文件。有两种方法可以引用文件内容,即在行中或在 s3 存储桶中。我使用 s3 存储桶方法,给 node.config 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
注意S3Access 属性。我们将存储桶设为私有,使用 IAM 授予对 aws-elasticbeanstalk-ec2-role 的访问权限。
现在我们只需要一个运行 npm 更新的 40install_node.sh 版本:
#!/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 配置中节点版本的增加而改变。
【讨论】:
如果您不想在 S3 上添加脚本,您可以简单地将以下内容放在您的 .ebextensions 中,假设您正在运行节点 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/
【讨论】:
这应该不是必需的。您可以在配置中指定要运行的 Nodejs 版本,从而与适当的 npm 版本配对。如果您确实希望使用较旧版本的 nodejs 和较新版本的 npm,那么这个练习是有必要的。
在这种情况下,我可能会在放入 .ebextensions 文件夹(例如 00_default.config)的文件中执行 npm 安装脚本:
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
将 CURRENTNODEVERSIONHERE 替换为您的设置的正确路径/版本。
这些是我在我的实例上看到的可用版本。您需要检查自己的资源以了解您正在使用什么。
位置:/opt/elasticbeanstalk/node-install
【讨论】: