aws cloudformation 中的 cfn-signal 具有 launchConfiguration 和 AutoScalingGroup

Jyo*_*a K 2 amazon-web-services aws-cloudformation

我的 AWS Cloudformation 模板中有以下配置。

该模板创建一个基于instance1. 我在 LaunchConfiguration 中使用对同一实例的引用来创建相同类型的实例。我面临的问题是在我的 AutoScalingGroup 模板中包含 CreationPolicy 元素。当 ASG 启动实例并等待 cfn 信号时,我收到以下错误。

+ /opt/aws/bin/cfn-signal -e 0 --stack ss07 --resource Instance1 --region us-west-2
ValidationError: Resource Instance1 is in CREATE_COMPLETE state and cannot be signaled
Run Code Online (Sandbox Code Playgroud)

似乎以某种方式引用了已经存在的实例1,而不是LaunchConfig 创建的实例。我看到了嵌入 LaunchConfig 的示例,但我想将实例详细信息保留在一处而不是两处。

"instance1": {
      "Type": "AWS::EC2::Instance",
      "Metadata": {
        "AWS::CloudFormation::Init": {
          "configSets": {
            "install": ["yum_packages","install_cfn"]
          },

          "yum_packages": {
              "packages" : {
                  "yum" : {
                    "awslogs"        : [],
                    "ruby"        : [],
                    "wget"        : [],
                    "httpd"       : []
                  }
            }
          },
          "install_cfn": {
            "files": {
              "/etc/cfn/cfn-hup.conf": {
                "content": {
                  "Fn::Join": [
                    "",
                    [
                      "[main]\n",
                      "stack=",
                      {
                        "Ref": "AWS::StackId"
                      },
                      "\n",
                      "region=",
                      {
                        "Ref": "AWS::Region"
                      },
                      "\n"
                    ]
                  ]
                },
                "mode": "000400",
                "owner": "root",
                "group": "root"
              },
              "/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
                "content": {
                  "Fn::Join": [
                    "",
                    [
                      "[cfn-auto-reloader-hook]\n",
                      "triggers=post.update\n",
                      "path=Resources.WebServer.Metadata.AWS::CloudFormation::Init\n",
                      "action=/opt/aws/bin/cfn-init -v ",
                      "         --stack ",
                      {
                        "Ref": "AWS::StackName"
                      },
                      "         --resource splitsweetInstance ",
                      "         --configsets install ",
                      "         --region ",
                      {
                        "Ref": "AWS::Region"
                      },
                      "\n"
                    ]
                  ]
                },
                "mode": "000400",
                "owner": "root",
                "group": "root"
              }             
            },
            "services": {
              "sysvinit": {
                "cfn-hup": { "enabled": "true",  "ensureRunning": "true",  "files": [
                    "/etc/cfn/cfn-hup.conf",
                    "/etc/cfn/hooks.d/cfn-auto-reloader.conf"
                                ]
                            }
              }
            }
          }
        }
      },
      "CreationPolicy": {
        "ResourceSignal": {
          "Timeout": "PT10M"
        }
      },
      "Properties": {
        "ImageId": {
                "Fn::FindInMap": [
                    "AWSRegionArch2AMI", {
                        "Ref": "AWS::Region"
                    }, {
                        "Fn::FindInMap": [
                            "AWSInstanceType2Arch", {
                                "Ref": "instanceType1"
                            },
                            "Arch"
                        ]
                    }
                ]
            },
        "InstanceType": {"Ref": "instanceType1"},
        "KeyName": {"Ref": "KeyName"},
        "Monitoring": "false",
        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
             "#!/bin/bash -xe\n",
             "yum install -y aws-cfn-bootstrap\n",

             "# Install the files and packages from the metadata\n",
             "/opt/aws/bin/cfn-init -v ",
             "         --stack ", { "Ref" : "AWS::StackName" },
             "         --resource splitsweetInstance ",
             "         --configsets install ",
             "         --region ", { "Ref" : "AWS::Region" }, "\n",

             "wget ", { "Fn::FindInMap": [  "Region2CodeDeployAgent", { "Ref": "AWS::Region"},  "url"] }, "\n",
             "chmod +x ./install\n",
             "./install auto\n",

             "# Signal the status from cfn-init\n",
             "/opt/aws/bin/cfn-signal -e $? ",
             "         --stack ", { "Ref" : "AWS::StackName" },
             "         --resource splitsweetInstance ",
             "         --region ", { "Ref" : "AWS::Region" }, "\n"

        ]]}},
        "Tags": [
          {
            "Key": "Name",
            "Value": "inst1"
          }
        ],
        "SecurityGroupIds": [
            { "Fn::GetAtt" : [ "instance1Sg", "GroupId" ] }
        ]
      }
    }
Run Code Online (Sandbox Code Playgroud)

我的启动配置如下 -

"LaunchConfig1": {
       "Type" : "AWS::AutoScaling::LaunchConfiguration",
       "Properties" : {
          "ImageId": {
            "Fn::FindInMap": [
                "AWSRegionArch2AMI", {
                    "Ref": "AWS::Region"
                }, {
                    "Fn::FindInMap": [
                        "AWSInstanceType2Arch", {
                            "Ref": "instanceType1"
                        },
                        "Arch"
                    ]
                }
            ]
          },
          "InstanceId" : { "Ref":"instance1"},
          "InstanceMonitoring" : "false",
          "InstanceType" : { "Ref": "instanceType1"},
          "KeyName" : { "Ref" : "KeyName" },
          "SecurityGroups" : [ { "Fn::GetAtt" : [ "instance1Sg", "GroupId" ] } ]
       }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的 AutoScalingGroup 模板 -

    "AutoScalingGroup1":    {
       "Type" : "AWS::AutoScaling::AutoScalingGroup",
       "Properties" : {
          "AvailabilityZones" :  {  "Fn::GetAZs": { "Ref": "AWS::Region" }   },
          "Cooldown" : "60",
          ....
          "LaunchConfigurationName" : {"Ref":"LaunchConfig1"},
          "MaxSize" : "3",
          "MinSize" : "1",
          "TargetGroupARNs" : [ {"Ref":"TargetGroup1"} ],
          "VPCZoneIdentifier" : [   { "Ref": "subnetCache1" },   { "Ref": "subnetCache2" },  { "Ref": "subnetCache3" }  ]
       },
       "CreationPolicy" : {
        "ResourceSignal" : {
          "Timeout" : "PT6M",
          "Count"   : "1"
         }
       }
    }   
Run Code Online (Sandbox Code Playgroud)

wjo*_*dan 5

问题在于,通过在资源InstanceId中指定属性LaunchConfiguration,它会重用用于启动初始 EC2 实例的相同 UserData,包括对命令指示的逻辑资源的硬编码引用cfn-signal。根据文档,

当您使用实例创建启动配置时,所有属性均从该实例派生,但 和BlockDeviceMapping除外AssociatePublicIpAddress。您可以通过在启动配置中指定来覆盖实例中的任何属性。

要发出cfn-signal正确的资源信号,您需要覆盖资源UserData中的LaunchConfiguration,以包含引用启动配置而不是原始 EC2 实例的用户数据脚本。不幸的是,这需要复制用户数据脚本,或者重写脚本以动态找出与脚本当前运行的实例关联的逻辑资源,因此可以在原始 EC2 中使用完全相同的用户数据实例和弹性伸缩组。