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

时间:2015-05-11 12:44:26

标签: amazon-web-services autoscaling amazon-cloudformation

我想使用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实例中创建文件。任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:8)

你错过了几件事。首先,您需要从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

答案 1 :(得分:0)

将“文件”添加到“上传”部分

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