9

我正在尝试通过 boto 的 ec2.run_instances(..., user_data=USER_DATA) 将用户数据加载到 Ubuntu 12.04 LTS AMI(ami-a29943cb,但我尝试了其他一些无济于事)。同样,我在通过 AWS 控制台启动实例时手动提供用户数据也没有成功。/var/logs/syslog 中没有我尝试过的任何方法的结果或消息。

USER_DATA 类似于以下内容,作为字符串从文件中读取:

#!/usr/bin/env python

import boto

AWS_BOOTSTRAP_BUCKET  = ''
AWS_ACCESS_KEY_ID     = ''
AWS_SECRET_ACCESS_KEY = ''

# Pull processing script from S3.
print 'Bootstrapping started.....'
print 'Connecting to S3...'
s3     = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = s3.get_bucket(AWS_BOOTSTRAP_BUCKET)
print 'Downloading bootstrap file...'
key    = bucket.get_key('xxx')
key.get_contents_to_filename('xxx')

print 'Importing Bootstrap file...'
import xxx
xxx.process()

# Shut down the EC2 instance running this process.
print 'Shutting down this instance...'
import socket
desired_hostname = socket.gethostname()
ec2 = boto.connect_ec2(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
reservations = ec2.get_all_instances()
for reservation in reservations:
    for instance in reservation.instances:
        if desired_hostname in instance.private_dns_name:
            instance.terminate()

我还尝试将文件上传到公共 S3 存储桶并以这种方式加载它,再次无济于事:

#include
https://s3.amazonaws.com/bucket/file.py

有人在这方面有什么建议吗?我完全误解了 user-data/cloud-init 的目的,还是只是在我尝试使用的 AMI 中损坏了该技术?

4

2 回答 2

14

如果没有错误消息,很难知道发生了什么,但您可以查看以下几个地方:

  1. 该文件/var/log/cloud-init.log通常会包含在实例引导期间发生的任何错误(例如 boto 导入失败)。
  2. 该目录/var/lib/cloud/instance将包含下载到实例的原始脚本和用户数据
  3. 您可以通过右键单击实例来查看/编辑 AWS 控制台内的 USER_DATA,以查看 boto 是否正确填充它。

查看这些地方应该有助于提供清晰性。

我知道 Ubuntu 12.04 带有 boto 2.2.2:

root@vanilla-562c:/var/lib/cloud/instance# python
Python 2.7.3 (default, Apr 20 2012, 22:44:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto
>>> boto.__version__
'2.2.2'

..但我想知道它是否真的可以在运行时在您的 PYTHONPATH 中访问。

于 2012-05-24T22:40:28.373 回答
1

快速清单:

1 ) SSH 进入您的实例。
2 ) 继续运行cat并将/var/log/cloud-init-output.log其转储到一个不错的文本文件中。
3 ) 你会看到包含以下结构的多个块:

Dependencies Resolved

================================================================================
 Package                    Arch      Version               Repository     Size
================================================================================
Installing:


Transaction Summary
================================================================================
Install  X Package (+Y Dependent packages)

每个块都是根据user data您提供给 EC2 实例的内容创建的(所有脚本都存储在 下/var/lib/cloud/instance/scripts)。

4) 搜索关键字,如Failed,或 。ErrorWARNING/var/lib/cloud/instance/scripts/

例如,如果出现以下错误:

/var/lib/cloud/instance/scripts/part-001: line 10: vncpasswd: command not found
cp: cannot stat '/lib/systemd/system/vncserver@.service': No such file or directory
sed: can't read /etc/systemd/system/vncserver@.service: No such file or directory
Failed to execute operation: No such file or directory
Failed to start vncserver@:1.service: Unit not found.
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-epel

(*) 您还可以快速查看结尾/var/log/cloud-init-output.log或包含一般错误消息,这些消息将通知您在执行用户数据脚本/var/log/cloud-init.log时发生错误的事实。

/var/log/cloud-init-output.log上面错误中的结尾:

Is this ok [y/d/N]: Exiting on user command
Your transaction was saved, rerun it with:
 yum load-transaction /tmp/yum_save_tx.2020-08-31.15-14.VpTvV1.yumtx
Aug 31 15:14:00 cloud-init[3532]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
Aug 31 15:14:00 cloud-init[3532]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Aug 31 15:14:00 cloud-init[3532]: 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. 19.3-3.amzn2 finished at Mon, 31 Aug 2020 15:14:00 +0000. Datasource DataSourceEc2.  Up 87.44 seconds

和结束/var/log/cloud-init.log

Aug 31 15:14:00 cloud-init[3532]: util.py[DEBUG]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
    Traceback (most recent call last):
      File "/usr/lib/python2.7/site-packages/cloudinit/util.py", line 910, in runparts
        subp(prefix + [exe_path], capture=False, shell=True)
      File "/usr/lib/python2.7/site-packages/cloudinit/util.py", line 2105, in subp
        cmd=args)
    ProcessExecutionError: Unexpected error while running command.
    Command: ['/var/lib/cloud/instance/scripts/part-001']
    Exit code: 1
    Reason: -
    Stdout: -
    Stderr: -
    cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
于 2020-08-31T16:39:52.660 回答