使用 Boto3 查找 AWS 中目标组中不健康的实例

问题描述 投票:0回答:1

我试图使用boto3找到目标组中不健康的实例,但没有可以提供的功能。

amazon-web-services boto3
1个回答
0
投票

您可以使用代码过滤目标组的不健康实例。我使用 boto3 的 LB 客户端在 Python 上通过此代码完成了此操作。

import boto3
# Initialize a boto3 client for Elastic Load Balancing
elb_client = boto3.client('elbv2')
# Specify your target group ARN
target_group_arn = 'arn:aws:elasticloadbalancing:us-east-2:208371820303:targetgroup/myapp/123456'
# Describe the target health
response = elb_client.describe_target_health(TargetGroupArn=target_group_arn)
#print('Response from elb client: ',response)

unhealthy_instances = []
healthy_instances = []
other_state_instances = []

#Evaluate the health state of each instance in the response by iterating
for instance in response['TargetHealthDescriptions']:
    if (instance['TargetHealth']['State'] == 'unhealthy'):
        unhealthy_instances.append(instance['Target']['Id'])
    elif ((instance['TargetHealth']['State'] == 'healthy')):
        healthy_instances.append(instance['Target']['Id'])
    else:
        other_state_instances.append(instance['Target']['Id'])

print("-------------------------------------------")
print("Unhealthy Instances: ", unhealthy_instances)
print("Healthy Instances: ", healthy_instances)

执行应该为您提供以下信息:

Console output of python script shows healthy and unhealthy isntances.

Instances in my target group on AWS console

© www.soinside.com 2019 - 2024. All rights reserved.