如何确定刚刚在Amazon ELB上注册的EC2实例的状态背后的“原因”?

时间:2013-09-13 16:44:47

标签: amazon-elb

我正在开发一个部署脚本(更具体地说,一个Ansible模块),它使用Amazon ELB注册EC2实例。该脚本使用Boto库。

以下是脚本的相关部分:

def register(self, wait):
    """Register the instance for all ELBs and wait for the ELB
    to report the instance in-service"""
    for lb in self.lbs:
        lb.register_instances([self.instance_id])
        if wait:
            self._await_elb_instance_state(lb, 'InService')

def _await_elb_instance_state(self, lb, awaited_state):
    """Wait for an ELB to change state
    lb: load balancer
    awaited_state : state to poll for (string)"""

    while True:
        state = lb.get_instance_health([self.instance_id])[0].state
        if state == awaited_state:
            break
        else:
            time.sleep(1)

(顺便说一句,上面的代码来自Ansible的ec2_elb module。)

因此,当实例首次注册时,它是'OutOfService'。这里的脚本“等待”实例在通过健康检查等后达到“InService”状态。

所以这就是问题所在:上面的过程过于简单化了(这就是我为了自己的目的而尝试自定义模块的原因)。我遇到的主要问题是,如果负载均衡器未配置为服务实例所在的可用区域,则实例将保持停止服务。基本上上面的脚本就会挂起。

我想做什么(这就是为什么我自定义这个内置模块)是找到一种方法来确定ELB是否只是等待实例通过健康检查或者是否有其他原因(如未注册的可用区域)导致其停止服务。

Boto库(通过Amazon ELB API)确实提供了比状态更多的细节:它有一个“reason”属性described in the Boto docs(以及Amazon ELB API docs)如下:

  

reason_code(str) - 提供有关原因的信息   OutOfService实例。具体来说,它表明原因是否存在   Elastic Load Balancing或LoadBalancer背后的实例。

关于我可以在那里找到的reason_code属性的文档很少,所以我不确定a)我可以期待甚至在这里可能的返回值,以及b)它们实际上意味着什么我的上述问题。

我认为我想要做的事情是可行的,因为亚马逊能够显示实例停止服务的原因的详细原因是管理控制台 - 据我了解他们是在那里养他们的API。

那么我如何/在哪里可以找到实例状态背后更详细的原因?

1 个答案:

答案 0 :(得分:1)

啊,这是InstanceState的描述字段:

  

description(str) - 实例的描述。

我猜这太模糊了,我的大脑忽略了它。

看起来state的可能性是两个字符串值:

  • 'ELB'
  • '实例'

这只是来自于使用API​​;它不是确定的或任何东西。