pythonamazon-web-servicesamazon-ec2boto3elastic-load-balancer

aws: boto3 get all instances of a load balancers


I can able to get the load balancers using below

import boto3
elb = boto3.client('elbv2')
lbs = elb.describe_load_balancers()

How to get the instances of the lbs.

Also How Can I fetch the load balancers which state is not active as describe_load_balanacers only give state active load balanceres.


Solution

  • Classic Load Balancer

    Use: client = boto3.client('elb')

    Then describe_load_balancers() results include a list of instances:

            'Instances': [
                {
                    'InstanceId': 'string'
                },
            ],
    

    Application Load Balancer

    Use: client = boto3.client('elbv2')

    Here is a sample response:

    {
        'TargetHealthDescriptions': [
            {
                'Target': {
                    'Id': 'i-0f76fade',
                    'Port': 80,
                },
    ...