【问题标题】:How to create a LaunchConfiguration using CloudFormation template which creates a config file?如何使用创建配置文件的 CloudFormation 模板创建 LaunchConfiguration?
【发布时间】:2015-07-21 23:26:58
【问题描述】:

我想使用 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 实例中创建。谁能帮我解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    把“文件”放到“上传”部分

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

    【讨论】:

    • 感谢您的回复@Evgeniy Kuzmin。但这对我不起作用。还有什么我应该在模板中添加的吗?
    • 你在 /var/log/cfn-init.log 看到了什么?
    • 我的实例中没有这样的日志文件
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 2016-06-13
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2020-12-28
      • 2023-01-12
      • 2020-06-23
      相关资源
      最近更新 更多