如何使用创建配置文件的 CloudFormation 模板创建 LaunchConfiguration?

How to create a LaunchConfiguration using CloudFormation template which creates a config file?

我想使用 CloudFormation 模板为我的 AWS 堆栈编写 LaunchConfiguration。 我是这样写的。

"LaunchConfiguration": {
  "Type": "AWS::AutoScaling::LaunchConfiguration",
  "Metadata" : {
      "AWS::CloudFormation::Init" : {
          "files": {
              "/etc/test.conf": {
                  "content": { "Fn::Join": [ "", [
                                                  "user: root\n",
                                                  "password: password\n"
                  ]]},
                  "mode": "000400",
                  "user": "root",
                  "group": "root"
              }
          }
      }
  },
  "Properties": {
    "ImageId": "ami-*****",
    "InstanceType": "*****",
    "KeyName": "*****",
    "IamInstanceProfile": "*****",
    "InstanceMonitoring": "****",
    "SecurityGroups": [
      {
        "Ref": "SecurityGroup"
      }
    ]
  }
},

文件未在创建的 EC2 实例中创建。谁能帮我解决这个问题?

将 "files" 放入 "upload" 部分作为

"Metadata" : {
      "AWS::CloudFormation::Init" : {
        "upload": {
          "files": {
              "/etc/test.conf": {
                  "content": { "Fn::Join": [ "", [
                                                  "user: root\n",
                                                  "password: password\n"
                  ]]},
                  "mode": "000400",
                  "user": "root",
                  "group": "root"
              }
          }
      }
    }
  },

您遗漏了几件事。首先,您需要从 LaunchConfiguration UserData 调用 cfn-init 脚本。 http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-helper-scripts-reference.html

"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
  "#!/bin/bash -ve\n",

  "# Run cfn-init\n",
  "/opt/aws/bin/cfn-init -v ",
  "         --stack ", { "Ref": "AWS::StackName" },
  "         --resource LaunchConfiguration ",
  "         --region ", { "Ref" : "AWS::Region" }, "\n",

  "# Signal success\n",
  "/opt/aws/bin/cfn-signal -e $? ",
  "         --stack ", { "Ref" : "AWS::StackName" },
  "         --resource AutoScalingGroup ",
  "         --region ", { "Ref" : "AWS::Region" }, "\n"
]]}}

此示例还使用 cfn-signal 发出成功信号,通知 Auto Scaling 组实例引导成功。要使用此功能,您还需要将 CreationPolicy 添加到您的 AutoScalingGroup 资源。 http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-creationpolicy.html

  "CreationPolicy" : {
    "ResourceSignal" : {
      "Timeout" : "PT10M",
      "Count"   : "1"
    }
  }

最后,您缺少元数据的默认 config 包装器。

  "Metadata" : {
    "AWS::CloudFormation::Init" : {
      "config" : {
        "files": {
          "/etc/test.conf" : {
            "content" : { "Fn::Join": [ "", [
              "user: root\n",
              "password: password\n"
            ]]},
            "mode" : "000400",
            "user" : "root",
            "group" : "root"
          }
        }
      }
    }
  }

你可以使用 config 以外的东西,但是你需要定义 configSets 属性。 http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html#aws-resource-init-configsets

"AWS::CloudFormation::Init" : {
  "configSets" : {
      "default" : [
        "db-config",
        "app-config"
      ]
  },
  "db-config": {
    "files": {
      ...
    }
  },
  "app-config": {
    ...
  }
}

有关更多信息,这是使用 CloudFormation 引导实例的详细概述。 https://s3.amazonaws.com/cloudformation-examples/BoostrappingApplicationsWithAWSCloudFormation.pdf