【问题标题】:AWS::CloudFormation::Init not executing commands in metadataAWS::CloudFormation::Init 不执行元数据中的命令
【发布时间】:2016-08-24 04:28:07
【问题描述】:

这是我的模板,元数据中有一个测试命令。我无法弄清楚为什么该命令没有被执行。它应该将字符串保存到 /tmp 中的文件中。 该机器是安装了 cloud-init 的 Ubuntu 16.04。在 UserData 中,我安装了帮助脚本并执行 cfn-init。

感谢您的帮助。

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Metadata": {
    "AWS::CloudFormation::Designer": {
      "2af5b799-f6bf-7f20-a6eb-943274f18373": {
        "size": {
          "width": 60,
          "height": 60
        },
        "position": {
          "x": 326,
          "y": 118
        },
        "z": 0,
        "embeds": []
      }
    }
  },
  "Resources": {
    "EC2I3WADD": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-c60b90d1",
        "KeyName": "CF-KEY",
        "InstanceType": "t2.micro",
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "",
              [
                "#!/bin/bash -xe\n",
                "apt-get -y install python-setuptools\n",
                "mkdir aws-cfn-bootstrap-latest\n",
                "curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1\n",
                "easy_install aws-cfn-bootstrap-latest\n",
                "/usr/local/bin/cfn-init --stack ",
                {
                  "Ref": "AWS::StackName"
                },
                " --resource WebServer",
                " --region ",
                {
                  "Ref": "AWS::Region"
                },
                "\n"
              ]
            ]
          }
        }
      },
      "Metadata": {
        "AWS::CloudFormation::Designer": {
          "id": "2af5b799-f6bf-7f20-a6eb-943274f18373"
        },
        "AWS::CloudFormation::Init": {
          "config": {
            "commands": {
              "test": {
                "command": "echo \"$MAGIC\" > /tmp/test.txt",
                "env": {
                  "MAGIC": "I come from the environment!"
                }
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

  • /var/log/cfn-init.log 中的任何内容?
  • 发布/var/log/cfn-init.log的内容

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

此问题可能是由于将不正确的 --resource 参数传递给 UserData 中的 cfn-init 命令所致。此参数应与包含元数据的资源的逻辑资源名称匹配,在您的情况下为 EC2I3WADD

"/usr/local/bin/cfn-init --stack ", { "Ref": "AWS::StackName" }, " --resource EC2I3WADD", " --region ", { "Ref": "AWS::Region" }

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2020-07-10
    • 2020-06-12
    • 2019-06-13
    • 2018-04-26
    • 2017-11-12
    • 1970-01-01
    相关资源
    最近更新 更多