Ubuntu AMI没有加载用户数据

时间:2012-05-24 19:08:56

标签: python ubuntu amazon-ec2 amazon-web-services user-data

我试图通过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

有没有人在这方面有任何建议?我是否完全误解了用户数据/ cloud-init的目的,还是我试图利用的AMI中仅仅打破了这项技术?

2 个答案:

答案 0 :(得分:12)

很难在没有错误消息的情况下知道发生了什么,但是有几个地方你可以看:

  1. 文件/var/log/cloud-init.log通常包含在实例引导过程中发生的任何错误(例如boto导入失败)。
  2. 目录/var/lib/cloud/instance将包含下载到实例的原始脚本和用户数据
  3. 您可以通过右键单击实例来查看/编辑AWS控制台内的USER_DATA,以查看boto是否正确填充了它。
  4. 在这些地方寻找应该有助于提供清晰度。

    我知道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运行时访问。

答案 1 :(得分:0)

快速清单:

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)

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

4)搜索FailedErrorWARNING/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)