通过 cloudformation 启动实例后,userData 未被执行

userData are not getting executed after launching the instance by cloudformation

我创建了一个 AWS cloudformation,它创建了一个启动配置和一个自动缩放组。在launch Config中的user Data中我配置了文件系统挂载目标,我安装了cloudwatch agent:

Code EDITED

  "LaunchConfig":{
    "Type":"AWS::AutoScaling::LaunchConfiguration",
    "Metadata" : {
      "AWS::CloudFormation::Init" : {
        "config" : {
          "files" : {
            "/etc/cwlogs.cfg": {
              "content": { "Fn::Join" : ["", [
                "[general]",
                "state_file = /var/awslogs/state/agent-state",
                "[/var/log/syslog]",
                "file = /tmp/",
                "log_group_name = ecs-dataloader",
                "log_stream_name = ECS-loader",
                "datetime_format = %b %d %H:%M:%S"
                ]]},
                "mode": "000755",
                "owner": "root",
                "group": "root"
            },
            "/etc/ecs/ecs.config": {
              "content": { "Fn::Join" : ["", [
                "ECS_CLUSTER=", { "Ref" : "ClusterName" }
              ]]},
              "mode": "000755",
              "owner": "root",
              "group": "root"
            }
          },
          "commands": {
            "Update": {
              "command": "yum -y update"
            },
            "InstallNfs":{
              "command": "yum -y install nfs-utils"
            },
            "CreatFolder": {
              "command": "mkdir -p /efs-mount-point/"
            },
            "EditPerms": {
              "command": "chown ec2-user:ec2-user /efs-mount-point/"
            },
            "MountPoint": {
              "command": { "Fn::Join" : ["", [
                "AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)\n",
                "echo LABEL=cloudimg-rootfs / ext4 defaults,discard    0 0\n",
                "$AZ.",{ "Fn::FindInMap" : [ "FileSystemMap", {"Ref" : "EnvParam"}, "FileSystemID"] },
                ".efs.",{ "Ref" : "AWS::Region" },".amazonaws.com:/  /efs-script-import-tmp nfs4 nfsvers=4.1 0 0 >> /etc/fstab"
              ]]}
            },
            "Mount": {
              "command": "mount -a -t nfs4"
            },
            "CloudWatchAgent": {
              "command": { "Fn::Join" : ["", [
                "curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O\n",
                "python ./awslogs-agent-setup.py --region ",{"Ref" : "AWS::Region"},"\n",
                "chmod +x ./awslogs-agent-setup.py ./awslogs-agent-setup.py -n -r",
                {"Ref" : "AWS::Region"}," -c /etc/cwlogs.cfg"
              ]]}
            }
          },
          "services" : {
            "sysvinit" : {
              "awslogs"  : { "enabled" : "true", "ensureRunning" : "true" }
            }
          }
        }
      }
    },
    "Properties":{
      "ImageId":{ "Fn::FindInMap":[ "AWSRegionToAMI", { "Ref":"AWS::Region" }, "AMIID" ] },
      "SecurityGroups":[ { "Ref":"EcsSecurityGroup" } ],
      "InstanceType": {"Ref":"InstanceType" },
      "IamInstanceProfile":{ "Ref":"EC2InstanceProfile" },
      "KeyName":{ "Fn::FindInMap" : [ "KeyPairMapping", {"Ref" : "EnvParam"}, "Key"] },
      "UserData":{  "Fn::Base64" : {
        "Fn::Join" : ["", [
          "#!/bin/bash -xe\n",
          "/opt/aws/bin/cfn-init -v ",
          "         --stack ", { "Ref": "AWS::StackName" },
          "         --resource LaunchConfig",
          "         --region ", { "Ref" : "AWS::Region" },"\n"
        ]]}
      }
    }
  }

图片详情:"eu-west-1": { "AMIID":"ami-ba346ec9" },

运行创建模板后,资源创建成功。因此,我通过 SSH 连接到由自动缩放组创建的实例,以查看 userData 是否正确 运行 并设置。 不幸的是,经过检查,这是我在 /etc/fstab 文件中找到的:

   $ cat /etc/fstab
   LABEL=cloudimg-rootfs    /    ext4   defaults,discard    0 0


   $ cat /etc/ecs/ecs.config
   cat: /etc/ecs/ecs.config: No such file or directory

实例未连接到 file system,我尝试在 cloudformation::init /etc/cwlogs.cfg 中创建的文件也不存在(它是 cloudwatch agent 配置文件) 。谁能告诉我用户数据有什么问题没有被执行?

我试图检查日志文件但是:

$ cat /var/log/cfn-init.log 
cat: /var/log/cfn-init.log: No such file or directory

这里有什么问题?

EDIT

$ cat /var/log/cloud-init-ouput.log
...

Cloud-init v. 0.7.6 running 'modules:final' at Fri, 17 Feb 2017 11:43:42 +0000. Up 44.66 seconds.
+ yum install -y aws-cfn-bootstrap/opt/aws/bin/cfn-init -v --stack Mystack --resource LaunchConfig --region eu-west-1
Loading "priorities" plugin
Loading "update-motd" plugin
Config time: 0.009
Command line error: no such option: --stack
Feb 17 11:43:43 cloud-init[2814]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
Feb 17 11:43:43 cloud-init[2814]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Feb 17 11:43:43 cloud-init[2814]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Cloud-init v. 0.7.6 finished at Fri, 17 Feb 2017 11:43:43 +0000. Datasource DataSourceEc2.  Up 45.18 seconds

用户数据日志文件位于:

  • Linux cloud-init: /var/log/cloud-init.log
  • Windows EC2Config:C:\cfn\log\cloud-init.log

检查日志文件中是否有任何内容。如果不是,那么从模板传递用户数据脚本有问题。 (为什么你的Join开头有空引号?)

cfn-init 仅默认安装在 Amazon Linux AMI 上,因此如果您使用任何其他图像 ID 启动 EC2 实例,您需要确保在调用它之前已正确安装.查看我的 to the question, "" 了解更多信息。

以下是我解决问题的方法:我在调用 meta-data 之前更新了用户数据中的 cloud-init,我没有在元数据中安装 cloudwatch 代理,而是在用户数据中安装了 cloudwatch 代理。

          "UserData":{  "Fn::Base64" : {
            "Fn::Join" : ["", [
              "#!/bin/bash -xe\n",
              "yum -y install aws-cfn-bootstrap\n",
              "/opt/aws/bin/cfn-init -v",
              "         --stack ", { "Ref": "AWS::StackName" },
              "         --resource LaunchConfig",
              "         --region ", { "Ref" : "AWS::Region" },"\n",
              "# Get the CloudWatch Logs agent\n",
              "wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py\n",
              "# Install the CloudWatch Logs agent\n",
              "python ./awslogs-agent-setup.py -n -r ", { "Ref" : "AWS::Region" }, " -c /etc/cwlogs.cfg || error_exit 'Failed to run CloudWatch Logs agent setup'\n",
              "service awslogs start"
          ]]}