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

how to wait for cfn-signal before adding a booting EC2 to the ELB

我的问题是关于在将启动 EC2 添加到 ELB 之前等待 cfn-signal(如果可能,使用 cloudformation)。 现在,ELB 开始使用实例而不等待它准备好,最糟糕的是,如果我只在 "boot" 结束时启动 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 的配置:

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

对于启动配置:

"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": {...}
}

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

在我的例子中,我有 "HealthyThreshold": "10""Interval": "30",所以 ELB 需要 10*30=300 秒。改变主意(如果我不幸运的话,甚至是 329.99)而且与 HealthCheckGracePeriod 相比它太长了 560 (560 - 329.99 = 230.01 秒。离开完成开始) .

我通过在 2 传递 HealthyThreshold 来缩短这个 "become healthy again" 延迟: 560 - 99.99 = 460.01 秒。剩下开始了。