我试图使用boto3找到目标组中不健康的实例,但没有可以提供的功能。
您可以使用代码过滤目标组的不健康实例。我使用 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)
执行应该为您提供以下信息: