【问题标题】:user data is not running at launch aws ec2用户数据在启动 aws ec2 时未运行
【发布时间】:2019-07-03 13:39:13
【问题描述】:

我正在尝试启动一个 ec2 linux 实例 (linux 2 ami),并且在用户数据中执行此操作时,我正在尝试 安装node js,同时安装git。 然后我尝试克隆我的 github 存储库,然后启动节点 js 服务器,所有这些都在用户数据中完成。

我正在尝试在 cloudlog 初始化文件中到处检查,以找出为什么我的用户数据无法正常工作的一些错误。

这里是代码。

#!/bin/bash
sudo yum update -y
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash 
. ~/.nvm/nvm.sh
nvm install 4.4.5
sudo yum upgrade
sudo yum install git -y
git clone https://github.com/myname/one_user.git
cd one_user

dnsaddress=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname)
export dns_name=${dnsaddress}
npm install -y
node server.js

以下代码来自云初始化日志文件。

Cloud-init v. 18.2-72.amzn2.0.6 running 'init-local' at Sun, 10 Feb 2019 15:49:35 +0000. Up 4.93 seconds.
Cloud-init v. 18.2-72.amzn2.0.6 running 'init' at Sun, 10 Feb 2019 15:49:38 +0000. Up 7.42 seconds.
ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: | Device |  Up  |           Address           |      Mask     | Scope  |     Hw-Address    |
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: |  eth0  | True |          10.0.1.72          | 255.255.255.0 | global | 0e:1f:76:6a:3c:6c |
ci-info: |  eth0  | True | fe80::c1f:76ff:fe6a:3c6c/64 |       .       |  link  | 0e:1f:76:6a:3c:6c |
ci-info: |   lo   | True |          127.0.0.1          |   255.0.0.0   |  host  |         .         |
ci-info: |   lo   | True |           ::1/128           |       .       |  host  |         .         |
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: ++++++++++++++++++++++++++++++Route IPv4 info+++++++++++++++++++++++++++++++
ci-info: +-------+-----------------+----------+-----------------+-----------+-------+
ci-info: | Route |   Destination   | Gateway  |     Genmask     | Interface | Flags |
ci-info: +-------+-----------------+----------+-----------------+-----------+-------+
ci-info: |   0   |     0.0.0.0     | 10.0.1.1 |     0.0.0.0     |    eth0   |   UG  |
ci-info: |   1   |     10.0.1.0    | 0.0.0.0  |  255.255.255.0  |    eth0   |   U   |
ci-info: |   2   | 169.254.169.254 | 0.0.0.0  | 255.255.255.255 |    eth0   |   UH  |
ci-info: +-------+-----------------+----------+-----------------+-----------+-------+
ci-info: +++++++++++++++++++Route IPv6 info+++++++++++++++++++
ci-info: +-------+-------------+---------+-----------+-------+
ci-info: | Route | Destination | Gateway | Interface | Flags |
ci-info: +-------+-------------+---------+-----------+-------+
ci-info: |   9   |  fe80::/64  |    ::   |    eth0   |   U   |
ci-info: |   11  |    local    |    ::   |    eth0   |   U   |
ci-info: |   12  |   ff00::/8  |    ::   |    eth0   |   U   |
ci-info: +-------+-------------+---------+-----------+-------+
Cloud-init v. 18.2-72.amzn2.0.6 running 'modules:config' at Sun, 10 Feb 2019 15:49:39 +0000. Up 8.99 seconds.
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Existing lock /var/run/yum.pid: another copy is running as pid 3265.
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  31 M RSS (321 MB VSZ)
    Started: Sun Feb 10 15:49:38 2019 - 00:02 ago
    State  : Sleeping, pid: 3265
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  70 M RSS (361 MB VSZ)
    Started: Sun Feb 10 15:49:38 2019 - 00:04 ago
    State  : Running, pid: 3265
 --> 1:openssl-libs-1.0.2k-16.amzn2.0.1.x86_64 from installed removed (updateinfo)
 --> 1:openssl-1.0.2k-16.amzn2.0.1.x86_64 from installed removed (updateinfo)
 --> 1:openssl-libs-1.0.2k-16.amzn2.0.2.x86_64 from amzn2-core removed (updateinfo)
 --> 1:openssl-1.0.2k-16.amzn2.0.2.x86_64 from amzn2-core removed (updateinfo)
1 package(s) needed (+0 related) for security, out of 3 available
Resolving Dependencies
--> Running transaction check
---> Package kernel-tools.x86_64 0:4.14.88-88.76.amzn2 will be updated
---> Package kernel-tools.x86_64 0:4.14.94-89.73.amzn2 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch        Version                    Repository       Size
================================================================================
Updating:
 kernel-tools      x86_64      4.14.94-89.73.amzn2        amzn2-core      111 k

Transaction Summary
================================================================================
Upgrade  1 Package

Total download size: 111 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : kernel-tools-4.14.94-89.73.amzn2.x86_64                      1/2 
  Cleanup    : kernel-tools-4.14.88-88.76.amzn2.x86_64                      2/2 
  Verifying  : kernel-tools-4.14.94-89.73.amzn2.x86_64                      1/2 
  Verifying  : kernel-tools-4.14.88-88.76.amzn2.x86_64                      2/2 

Updated:
  kernel-tools.x86_64 0:4.14.94-89.73.amzn2                                     

Complete!
Cloud-init v. 18.2-72.amzn2.0.6 running 'modules:final' at Sun, 10 Feb 2019 15:49:47 +0000. Up 16.22 seconds.
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Existing lock /var/run/yum.pid: another copy is running as pid 3324.
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  54 M RSS (270 MB VSZ)
    Started: Sun Feb 10 15:49:45 2019 - 00:03 ago
    State  : Running, pid: 3324
Resolving Dependencies
--> Running transaction check
---> Package kernel.x86_64 0:4.14.94-89.73.amzn2 will be installed
---> Package openssl.x86_64 1:1.0.2k-16.amzn2.0.1 will be updated
---> Package openssl.x86_64 1:1.0.2k-16.amzn2.0.2 will be an update
---> Package openssl-libs.x86_64 1:1.0.2k-16.amzn2.0.1 will be updated
---> Package openssl-libs.x86_64 1:1.0.2k-16.amzn2.0.2 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch        Version                    Repository       Size
================================================================================
Installing:
 kernel            x86_64      4.14.94-89.73.amzn2        amzn2-core       19 M
Updating:
 openssl           x86_64      1:1.0.2k-16.amzn2.0.2      amzn2-core      496 k
 openssl-libs      x86_64      1:1.0.2k-16.amzn2.0.2      amzn2-core      1.2 M

Transaction Summary
================================================================================
Install  1 Package
Upgrade  2 Packages

Total download size: 21 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
--------------------------------------------------------------------------------
Total                                               33 MB/s |  21 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:openssl-libs-1.0.2k-16.amzn2.0.2.x86_64                    1/5 
  Updating   : 1:openssl-1.0.2k-16.amzn2.0.2.x86_64                         2/5 
  Installing : kernel-4.14.94-89.73.amzn2.x86_64                            3/5 
  Cleanup    : 1:openssl-1.0.2k-16.amzn2.0.1.x86_64                         4/5 
  Cleanup    : 1:openssl-libs-1.0.2k-16.amzn2.0.1.x86_64                    5/5 
  Verifying  : 1:openssl-libs-1.0.2k-16.amzn2.0.2.x86_64                    1/5 
  Verifying  : kernel-4.14.94-89.73.amzn2.x86_64                            2/5 
  Verifying  : 1:openssl-1.0.2k-16.amzn2.0.2.x86_64                         3/5 
  Verifying  : 1:openssl-libs-1.0.2k-16.amzn2.0.1.x86_64                    4/5 
  Verifying  : 1:openssl-1.0.2k-16.amzn2.0.1.x86_64                         5/5 

Installed:
  kernel.x86_64 0:4.14.94-89.73.amzn2                                           

Updated:
  openssl.x86_64 1:1.0.2k-16.amzn2.0.2                                          
  openssl-libs.x86_64 1:1.0.2k-16.amzn2.0.2                                     

Complete!
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10007  100 10007    0     0  10007      0  0:00:01 --:--:--  0:00:01 99079
=> Downloading nvm as script to '/.nvm'

=> Profile not found. Tried  (as defined in $PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
=> Create it (touch ) and run this script again
   OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
/var/lib/cloud/instance/scripts/part-001: line 4: /root/.nvm/nvm.sh: No such file or directory
/var/lib/cloud/instance/scripts/part-001: line 5: nvm: command not found
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Existing lock /var/run/yum.pid: another copy is running as pid 11772.
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  52 M RSS (268 MB VSZ)
    Started: Sun Feb 10 15:50:08 2019 - 00:02 ago
    State  : Running, pid: 11772
No packages marked for update
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:2.17.2-2.amzn2 will be installed
--> Processing Dependency: perl-Git = 2.17.2-2.amzn2 for package: git-2.17.2-2.amzn2.x86_64
--> Processing Dependency: git-core-doc = 2.17.2-2.amzn2 for package: git-2.17.2-2.amzn2.x86_64
--> Processing Dependency: git-core = 2.17.2-2.amzn2 for package: git-2.17.2-2.amzn2.x86_64
--> Processing Dependency: emacs-filesystem >= 25.3 for package: git-2.17.2-2.amzn2.x86_64
--> Processing Dependency: perl(Term::ReadKey) for package: git-2.17.2-2.amzn2.x86_64
--> Processing Dependency: perl(Git::I18N) for package: git-2.17.2-2.amzn2.x86_64
--> Processing Dependency: perl(Git) for package: git-2.17.2-2.amzn2.x86_64
--> Processing Dependency: libsecret-1.so.0()(64bit) for package: git-2.17.2-2.amzn2.x86_64
--> Running transaction check
---> Package emacs-filesystem.noarch 1:25.3-3.amzn2.0.1 will be installed
---> Package git-core.x86_64 0:2.17.2-2.amzn2 will be installed
---> Package git-core-doc.noarch 0:2.17.2-2.amzn2 will be installed
---> Package libsecret.x86_64 0:0.18.5-2.amzn2.0.2 will be installed
---> Package perl-Git.noarch 0:2.17.2-2.amzn2 will be installed
--> Processing Dependency: perl(Error) for package: perl-Git-2.17.2-2.amzn2.noarch
---> Package perl-TermReadKey.x86_64 0:2.30-20.amzn2.0.2 will be installed
--> Running transaction check
---> Package perl-Error.noarch 1:0.17020-2.amzn2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch       Version                   Repository      Size
================================================================================
Installing:
 git                  x86_64     2.17.2-2.amzn2            amzn2-core     217 k
Installing for dependencies:
 emacs-filesystem     noarch     1:25.3-3.amzn2.0.1        amzn2-core      64 k
 git-core             x86_64     2.17.2-2.amzn2            amzn2-core     4.0 M
 git-core-doc         noarch     2.17.2-2.amzn2            amzn2-core     2.3 M
 libsecret            x86_64     0.18.5-2.amzn2.0.2        amzn2-core     153 k
 perl-Error           noarch     1:0.17020-2.amzn2         amzn2-core      32 k
 perl-Git             noarch     2.17.2-2.amzn2            amzn2-core      70 k
 perl-TermReadKey     x86_64     2.30-20.amzn2.0.2         amzn2-core      31 k

Transaction Summary
================================================================================
Install  1 Package (+7 Dependent packages)

Total download size: 6.8 M
Installed size: 36 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                               18 MB/s | 6.8 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : git-core-2.17.2-2.amzn2.x86_64                               1/8 
  Installing : git-core-doc-2.17.2-2.amzn2.noarch                           2/8 
  Installing : libsecret-0.18.5-2.amzn2.0.2.x86_64                          3/8 
  Installing : 1:perl-Error-0.17020-2.amzn2.noarch                          4/8 
  Installing : perl-TermReadKey-2.30-20.amzn2.0.2.x86_64                    5/8 
  Installing : 1:emacs-filesystem-25.3-3.amzn2.0.1.noarch                   6/8 
  Installing : perl-Git-2.17.2-2.amzn2.noarch                               7/8 
  Installing : git-2.17.2-2.amzn2.x86_64                                    8/8 
  Verifying  : 1:emacs-filesystem-25.3-3.amzn2.0.1.noarch                   1/8 
  Verifying  : perl-TermReadKey-2.30-20.amzn2.0.2.x86_64                    2/8 
  Verifying  : 1:perl-Error-0.17020-2.amzn2.noarch                          3/8 
  Verifying  : libsecret-0.18.5-2.amzn2.0.2.x86_64                          4/8 
  Verifying  : git-core-2.17.2-2.amzn2.x86_64                               5/8 
  Verifying  : git-2.17.2-2.amzn2.x86_64                                    6/8 
  Verifying  : perl-Git-2.17.2-2.amzn2.noarch                               7/8 
  Verifying  : git-core-doc-2.17.2-2.amzn2.noarch                           8/8 

Installed:
  git.x86_64 0:2.17.2-2.amzn2                                                   

Dependency Installed:
  emacs-filesystem.noarch 1:25.3-3.amzn2.0.1                                    
  git-core.x86_64 0:2.17.2-2.amzn2                                              
  git-core-doc.noarch 0:2.17.2-2.amzn2                                          
  libsecret.x86_64 0:0.18.5-2.amzn2.0.2                                         
  perl-Error.noarch 1:0.17020-2.amzn2                                           
  perl-Git.noarch 0:2.17.2-2.amzn2                                              
  perl-TermReadKey.x86_64 0:2.30-20.amzn2.0.2                                   

Complete!
Cloning into 'zero2architect'...
/var/lib/cloud/instance/scripts/part-001: line 16: npm: command not found
/var/lib/cloud/instance/scripts/part-001: line 17: node: command not found
Feb 10 15:50:16 cloud-init[3314]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [127]
Feb 10 15:50:16 cloud-init[3314]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Feb 10 15:50:16 cloud-init[3314]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Cloud-init v. 18.2-72.amzn2.0.6 finished at Sun, 10 Feb 2019 15:50:16 +0000. Datasource DataSourceEc2.  Up 45.55 seconds

【问题讨论】:

  • 假设您使用的是 Amazon Linux 2,您是否检查了 /var/log/cloud-init-output.log (docs.aws.amazon.com/AWSEC2/latest/UserGuide/…) 中的信息?
  • 你为什么不在你的用户数据脚本中一步一步地回显一些进度信息,然后检查控制台输出呢?此外,用户数据中不需要 sudo,因为它已经以 root 身份运行。
  • @LechMigdal 是的,我做了,我真的没有看到任何错误,我应该在这里发布输出吗?
  • @jarmod 我怎样才能回显它?它会存放在哪里?
  • 只是回显到标准输出,例如回显“运行 yum update -y”。要查看,请参阅docs.aws.amazon.com/AWSEC2/latest/UserGuide/…

标签: linux amazon-web-services amazon-ec2


【解决方案1】:

您检查过您的 SG 和 NACL 配置吗?您需要在出站 SG 规则中允许端口 80 (HTTP) 和端口 443 (HTTPS) - 目标 0.0.0.0/0。您还需要在出站 NACL 的规则和入站规则的临时端口上允许相同的内容。确保您的实例位于公有子网中(具有到 IGW 的路由)并且它附加了公有/弹性 IPv4。

如果不满足其中任何一个条件,您的用户数据脚本将失败,因为它需要连接到 Internet(在您的情况下)。

如果您的实例位于私有子网中,请确保您正在运行 NAT 网关或 NAT 实例,并且您的路由表以及 SG 和 NACL 已正确配置。如果您正在使用它,还要确保在 NAT 实例上禁用源/目标检查。

更新

您的日志清楚地表明您没有安装npmnode,因此您还需要将安装说明传递给脚本。

npm install -y

不是安装npm 的正确方法。您可以按照以下步骤安装nodenpm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash

. ~/.nvm/nvm.sh

nvm install 4.4.5

这将安装节点版本 4.4.5。如果您想安装其他版本的节点,请更改任何受支持版本的编号。

【讨论】:

  • 它应该可以工作,因为当我 ssh 进入实例并从实例内部执行以下脚本时,它可以工作,所以我假设配置不会成为问题。
猜你喜欢
  • 2018-11-11
  • 2016-12-24
  • 1970-01-01
  • 2022-01-23
  • 2018-08-02
  • 2018-09-10
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多