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

时间:2017-02-16 10:11:52

标签: json amazon-web-services amazon-cloudformation user-data

我创建了一个AWS云信息,它创建了一个启动配置和一个自动缩放组。在启动Config中的用户数据中,我配置了文件系统挂载目标,并安装了cloudwatch代理:

  

代码已编辑

  "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" },

运行模板后,资源成功创建。所以我连接到由autoscaling组通过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

这里有什么问题?

  

修改

$ 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

3 个答案:

答案 0 :(得分:2)

用户数据日志文件位于:

  • Linux cloud-init:save
  • Windows EC2Config:/var/log/cloud-init.log

检查日志文件中是否有任何内容。如果没有,那么从模板传递用户数据脚本就出了问题。 (为什么在Join中有初始的空引号?)

答案 1 :(得分:2)

cfn-init仅在Amazon Linux AMI上默认安装,因此如果您使用任何其他Image ID启动EC2实例,则需要确保在调用之前正确安装它。有关详细信息,请参阅我的previous answer问题“Installing packages using apt-get in CloudFormation file”。

答案 2 :(得分:1)

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

          "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"
          ]]}