2017-02-26 80 views
1

我的AWS Cloudformation模板中具有以下配置。aws cloudformation中的cfn-signal與launchConfiguration和AutoScalingGroup

該模板根據instance1創建一個EC2實例。我在我的LaunchConfiguration中使用對同一實例的引用來創建相同類型的實例。 我面臨的問題是通過在我的AutoScalingGroup模板中包含CreationPolicy元素。當ASG啓動一個實例並等待cfn-signal時,出現以下錯誤。

+ /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 

好像不知何故參考了已經存在INSTANCE1,而不是實例被創建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" ] } 
     ] 
     } 
    } 

我的啓動配置如下 -

"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" ] } ] 
     } 
    } 

這是我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" 
     } 
     } 
    } 

回答

2

的問題是,通過在LaunchConfiguration資源指定InstanceId屬性,它是重複使用用於啓動初始EC2實例的相同UserData,包括硬編碼對邏輯資源的引用,用t表示他cfn-signal命令。根據該文件,

當您使用一個實例來創建一個啓動配置,所有的屬性都來自以BlockDeviceMappingAssociatePublicIpAddress異常的情況下得到的。您可以通過在啓動配置中指定它們來覆蓋實例的任何屬性。

要讓cfn-signal信號正確的資源,你需要重寫UserDataLaunchConfiguration資源包含用戶數據的腳本,它引用啓動配置,而不是原來的EC2實例。不幸的是,這將需要複製用戶數據腳本或重寫腳本以動態找出與腳本當前正在運行的實例相關聯的邏輯資源,因此可以在原始EC2中使用相同的確切用戶數據實例和自動縮放組。

+0

感謝您的答覆。關於答案的其他問題 - 1.如果我覆蓋UserData,元數據是否仍然適用?我無法在LaunchConfiguration中找到指定的元數據。 2.關於你的第二條評論,是否有一個例子來動態地找出邏輯資源關聯? –

0

我同意wjordan,你遇到的問題的一部分是財產LaunchConfiguration。同樣看着你的代碼示例,我無法弄清楚哪裏引用了splitsweetInstance

但是我的CloudFormation AutoScalingGroup沒有這樣設置,我仍然得到了相同的錯誤CREATE_COMPLETE state and cannot be signaled

對我來說是在AutoScalingGroup設置DesiredCapacity(希望這可以幫助其他人也)溶液這裏找到: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-desiredcapacity

按照文檔:

CloudFormation並不標誌着汽車縮放組成功(通過將其狀態設置爲CREATE_COMPLETE)直至達到所需的容量。

這是我的CloudFormation模板的那部分是什麼樣子:

"WebServerGroup" : { 
    "Type" : "AWS::AutoScaling::AutoScalingGroup", 
    "Properties" : { 
    "VPCZoneIdentifier" : { "Ref" : "Subnets" }, 
    "LaunchConfigurationName" : { "Ref" : "LaunchConfig" }, 
    "MinSize" : "2", 
    "MaxSize" : "4", 
    "DesiredCapacity" : "2", 
    "TargetGroupARNs" : [ { "Ref" : "ALBTargetGroup" } ] 
    } 

下面的UserData部分的 「信號」 部分:

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