Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 469
MFB Avatar asked Mar 20 '17 04:03

MFB


2 Answers

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': {
        '...': '...',
    },
}
like image 175
John Rotenstein Avatar answered Oct 19 '22 07:10

John Rotenstein


For those who are still looking for the solution, I have made a python script for the same, and the code is available on github.

like image 2
live_alone Avatar answered Oct 19 '22 09:10

live_alone