如何在将引导EC2添加到ELB之前等待cfn信号

时间:2017-08-22 14:14:23

标签: amazon-web-services amazon-ec2 amazon-cloudformation amazon-elb cloud-init

我的问题是在将引导EC2添加到ELB之前实现等待cfn-signal(如果可能,使用cloudformation)。 现在,ELB开始使用实例而不等待它准备就绪,最糟糕的是,如果我只在“启动”结束时启动apache服务,它不会等待宽限期并在它之前删除实例完成初始化。

这是我的AutoScalingGroup conf:

"webServerASG": {
  "Type": "AWS::AutoScaling::AutoScalingGroup",

  "CreationPolicy": {
    "ResourceSignal": {
      "Timeout": "PT10M",
      "Count": { "Ref": "WebServerDesiredCapacity" }
    }
  },
  "UpdatePolicy" : {
    "AutoScalingReplacingUpdate" : {
      "WillReplace" : true
    },
    "AutoScalingRollingUpdate" : {
      "MaxBatchSize" : 1,
      "MinInstancesInService" : 1,
      "MinSuccessfulInstancesPercent" : 75,
      "SuspendProcesses" : [ "AlarmNotification", "ScheduledActions", "AZRebalance", "ReplaceUnhealthy", "HealthCheck", "Launch" ],
      "WaitOnResourceSignals": true,
      "PauseTime" : "PT10M"
    }
  },
  "Properties": {
    "AvailabilityZones": [...],
    "Cooldown": "60",
    "HealthCheckGracePeriod": "560",
    "HealthCheckType": "ELB",
    "MaxSize": { "Ref": "WebServerMaxCapacity" },
    "MinSize": "1",
    "DesiredCapacity": { "Ref": "WebServerDesiredCapacity" },
    "VPCZoneIdentifier": [...],
    "NotificationConfigurations": [...],
    "LaunchConfigurationName": { "Ref": "webServer01LC" },
    "LoadBalancerNames": [ { "Ref": "webServerLoadBalancerELB" } ],
    "MetricsCollection": [...],
    "TerminationPolicies": [
      "Default"
    ]
  }
},

使用LoadBalancer的这个conf:

"webServerLoadBalancerELB": {
  "Type": "AWS::ElasticLoadBalancing::LoadBalancer",
  "Properties": {
    "Subnets": [...],
    "HealthCheck": {
      "HealthyThreshold": "10",
      "Interval": "30",
      "Target": "HTTP:80/aws/healthcheck.php",
      "Timeout": "5",
      "UnhealthyThreshold": "2"
    },
    "ConnectionDrainingPolicy": {
      "Enabled": "true",
      "Timeout": "300"
    },
    "ConnectionSettings": {
      "IdleTimeout": "60"
    },
    "CrossZone": "true",
    "SecurityGroups": [...],
    "Listeners": [
      {
        "LoadBalancerPort": "80",
        "Protocol": "HTTP",
        "InstancePort": "80",
        "InstanceProtocol": "HTTP"
      }
    ]
  }
}

和LaunchConfiguration:

"webServer01LC": {
  "Type": "AWS::AutoScaling::LaunchConfiguration",
  "Properties": {
    "AssociatePublicIpAddress": false,
    "ImageId": {...},
    "InstanceType": "t2.small",
    "KeyName": {...},
    "IamInstanceProfile": "EC2WebServerRole",
    "SecurityGroups": [...],
    "BlockDeviceMappings": [...],
    "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
      "#!/bin/bash\n",

      "echo '\n### pip install cloud-init : ' | tee --append /var/log//userData.log \n",
      "pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz  2>> /var/log//userData.log\n",
      "ln -s /usr/local/init/ubuntu/cfn-hup /etc/init.d/cfn-hup  >> /var/log//userData.log>&1\n",


      "echo '\n### execute the AWS::CloudFormation::Init defined in the Metadata' | tee --append /var/log//userData.log \n",
      "cfn-init -v",
              " --region ", { "Ref" : "AWS::Region" },
              " --stack ", { "Ref" : "AWS::StackName" },
              " --resource lciTophubWebServer01LC ",
              " \n",

      "cfn-signal --exit-code $? --region ", { "Ref" : "AWS::Region" }, " --stack ", { "Ref" : "AWS::StackName" }, " --resource asgiTophubWebServerASG \n",
      "echo '\n### end of the script!' | tee --append /var/log//userData.log \n"
    ]]}}
  },
  "Metadata": {...}
}

1 个答案:

答案 0 :(得分:0)

我发现了我的问题: ELB状态检查在没有暂停的情况下运行,因此在宽限期内,测试正在运行。 一旦宽限期结束,自动缩放组立即查看当前的ELB状态检查。

在我的情况下,我有"HealthyThreshold": "10""Interval": "30"所以它需要ELB 10 * 30 = 300秒。改变主意(如果我不幸运的话,甚至是329.99),与只有HealthCheckGracePeriod的{​​{1}}相比太长了(560 - 329.99 = 230.01秒才能完成启动)

我在560处传递HealthyThreshold,从而缩短了“再次变得健康”的延迟: 560 - 99.99 = 460.01秒。离开了。