2

每当beantalk更改为警告状态时,我正在尝试使用事件桥重新启动我降级的ec2实例,在目的地可以选择调用lambda函数或使用api重新启动实例,我的疑问是如何获取id实例仅降级(考虑到我的环境有 2 个实例)。

4

1 回答 1

1

当 Elastic Beanstalk 为健康状态更改调度事件时,它看起来像:

{ 
   "version":"0",
   "id":"1234a678-1b23-c123-12fd3f456e78",
   "detail-type":"Health status change",
   "source":"aws.elasticbeanstalk",
   "account":"111122223333",
   "time":"2020-11-03T00:34:48Z",
   "region":"us-east-1",
   "resources":[
      "arn:was:elasticbeanstalk:us-east-1:111122223333:environment/myApplication/myEnvironment"
   ],
   "detail":{
      "Status":"Environment health changed",
      "EventDate":1604363687870,
      "ApplicationName":"myApplication",
      "Message":"Environment health has transitioned from Pending to Ok. Initialization completed 1 second ago and took 2 minutes.",
      "EnvironmentName":"myEnvironment",
      "Severity":"INFO"
   }
}

在您的 AWS Lambda 中,您可以使用任何AWS Elastic Beanstalk命令,使用 AWS 开发工具包作为您选择的语言,

使用适用于 Elastic Beanstalk 的 AWS JavaScript 开发工具包,您可以通过以下方式重新启动您的环境restartAppServer

 var params = {
  EnvironmentName: event.detail.EnvironmentName // based on the sample above
 };
 elasticbeanstalk.restartAppServer(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
 });

在上面的示例中,我们将触发环境中所有实例的重启。

要针对特定​​实例,您可以使用describe-instances-healthdescribeInstancesHealth ,在 AWS JavaScript SDK 中重命名为:

var params = {
  AttributeNames: ["HealthStatus"],
  EnvironmentName: event.detail.EnvironmentName,
};
elasticbeanstalk.describeInstancesHealth(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

根据其响应,您可以过滤不正常的实例并通过调用EC2 APIrebootInstances使用实例 ID 触发重启:

 var params = {
  InstanceIds: [
     "i-1234567890abcdef5"
  ]
 };
 ec2.rebootInstances(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
 });
于 2021-03-30T01:12:07.673 回答