为什么我的Ubuntu EC2实例的DataUser部分中的脚本没有运行?我正在使用Cloudformation

时间:2017-04-18 14:21:49

标签: amazon-ec2 ubuntu-16.04 amazon-cloudformation user-data

我正在使用CloudFormation创建运行Ubuntu的EC2实例。我已经在EC2实例的UserData属性中插入了安装pip的命令和安装cfn cloudformation帮助程序脚本的命令。也就是说,关键的云形态模板片段是:

"UserData" : {
      "Fn::Base64" : {
        "Fn::Join": ["", [
          "#!/bin/bash -xe","\n",
          "apt-get update","\n",
          "apt-get install -y python-pip","\n",
          "apt-get install -y python3","\n",
          "apt-get install -y heat-cfntools","\n"
          ]
        ]
      }
    }

不,我没有元数据部分。问题:为什么脚本没有运行?这是我在新创建的EC2实例上获得的输出:

  

ubuntu @ ip-10-0-0-121:〜$ curl -s http://169.254.169.254/latest/user-data

#/bin/bash -xe
apt-get update
apt-get install -y python-pip
apt-get install -y heat-cfntools

脚本从Cloudformation模板中的EC2资源列表的DataUser部分传输到实时EC2 isntance,但正如您在下面看到的,脚本未执行:

  

ubuntu @ ip-10-0-0-121:〜$ pip

The program 'pip' is currently not installed. You can install it by typing:
sudo apt install python-pip
  

ubuntu @ ip-10-0-0-121:〜$ vi /var/log/cloud-init.log

log show nothing that stands out.
  

ubuntu @ ip-10-0-0-121:〜$ cfn-init

The program 'cfn-init' is currently not installed. You can install it by typing:  
sudo apt install heat-cfntools

注意:当我不使用Cloudformation而是使用EC2控制台时,我在EC2的UserData部分运行相同的脚本没有任何问题。

编辑:我经常使用 aws cloudformation validate-template [NameOfTemplate] 命令。但是,此工具仅允许我验证模板是否完全符合JSON语法。该工具不会验证任何其他内容。如果模板被破坏,运行Cloudformation会导致回滚。 Cloudformation一直运行直到报告完成。

1 个答案:

答案 0 :(得分:0)

有什么东西从shebang(#!)中移除你的爆炸(!)。

在你的输出上你有:

ubuntu@ip-10-0-0-121 :~$ curl -s http://169.254.169.254/latest/user-data
#/bin/bash -xe
apt-get update
apt-get install -y python-pip
apt-get install -y heat-cfntools

所以cloudformation正在读取“#/ bin / bash”而不是“# / bin / bash”,并且不知道如何执行它并且什么都不做。

我不知道为什么会发生这种情况以及如何解决这个问题,但可能与您的上传过程有关的事情是删除

希望它对某人有所帮助,尽管这是一个老问题。