简体   繁体   中英

Find EC2 Instances belonging to specific Target Group with Boto3

I have a couple of Elastic Load Balancers. I wish to dynamically find the public IP addresses associated with the EC2 Instances which belong to the ELB's Target Group. I used to be able to do it with the previous version of ELB, because the Instance ID's would be listed with each ELB. Now, it seems, they are not. Any clues would be great!

As answered on How to query AWS to get ELB names and attached instances to that using python boto3 modules? :

The Application Load Balancer has multiple Target Groups . Ports on instances are registered to a Target Group.

The only command that seems to list instances in a Target Group is describe_target_health() , which returns the instance and port (because one instance can serve multiple targets):

{
    'TargetHealthDescriptions': [
        {
            'Target': {
                'Id': 'i-0f76fade',
                'Port': 80,
            },
            'TargetHealth': {
                'Description': 'Given target group is not configured to receive traffic from ELB',
                'Reason': 'Target.NotInUse',
                'State': 'unused',
            },
        },
        {
            'HealthCheckPort': '80',
            'Target': {
                'Id': 'i-0f76fade',
                'Port': 80,
            },
            'TargetHealth': {
                'State': 'healthy',
            },
        },
    ],
    'ResponseMetadata': {
        '...': '...',
    },
}

对于仍在寻找解决方案的人,我已经为其编写了一个python脚本,并且代码可以在github上找到

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM