Method of finding instances attached to ELB

Because I love answers that can be used with a minimum of search/replace and copy paste

Prerequisites : aws-cli configured

pip install awscli
aws configure

Configure : your ELB name

$ELB_NAME = "Your-elb-name"

Copy-n-Paste in terminal

for ID in $(aws elb describe-load-balancers --load-balancer-name $ELB_NAME \
    --query LoadBalancerDescriptions[].Instances[].InstanceId \
    --output=text);
do
    aws ec2 describe-instances --instance-ids $ID \
        --query Reservations[].Instances[].PublicIpAddress \
        --output text
done

Will output a list of Public IPs. You could also just execute the query inside the parenthesis of the for ID in $(...) to just get the instance IDs

Want something different ?

Feel free to have a look at the structure of

aws elb describe-load-balancers --load-balancer-name $ELB_NAME
aws ec2 describe-instances --instance-ids $INSTANCE_ID

and change the query accordingly!


Assuming you have aws-cli and jq installed, you can use the following command to get associated ec2 instance ids:

aws elb describe-load-balancers --load-balancer-name my-elb \
  | jq -r '.LoadBalancerDescriptions[].Instances[].InstanceId'

This will return the ec2 ids associated with that ELB.

Side note: I recommend you setup aws cli profiles so you don't have to fiddle with environment variables and region params (as much).


2013/12/18: To update this and since the links are dead!

I installed the new AWS cli tools:

$ pip install awscli

Then ran:

$ aws configure                                                                                                                                                
AWS Access Key ID [None]: my-key
AWS Secret Access Key [None]: my-secret
Default region name [None]: us-east-1
Default output format [None]:

This data is saved into ~/.aws/config.

Then I can find instances connected to a loadbalancer like so:

$ aws elb describe-load-balancers --load-balancer-name "my-name"
{
    "LoadBalancerDescriptions": [
        {
            "Subnets": [],
            "CanonicalHostedZoneNameID": "ID",
            "CanonicalHostedZoneName": "my-name-foo.us-east-1.elb.amazonaws.com",
            "ListenerDescriptions": [
                {
                    "Listener": {
                        "InstancePort": 80,
                        "LoadBalancerPort": 80,
                        "Protocol": "HTTP",
                        "InstanceProtocol": "HTTP"
                    },
                    "PolicyNames": []
                },
                {
                    "Listener": {
                        "InstancePort": 80,
                        "SSLCertificateId": "arn:aws:iam::x:server-certificate/x-ssl-prod",
                        "LoadBalancerPort": 443,
                        "Protocol": "HTTPS",
                        "InstanceProtocol": "HTTP"
                    },
                    "PolicyNames": [
                        "AWSConsole-SSLNegotiationPolicy-api-production"
                    ]
                }
            ],
            "HealthCheck": {
                "HealthyThreshold": 10,
                "Interval": 30,
                "Target": "HTTP:80/healthy.php",
                "Timeout": 5,
                "UnhealthyThreshold": 2
            },
            "BackendServerDescriptions": [],
            "Instances": [
                {
                    "InstanceId": "i-FIRST-INSTANCEID"
                },
                {
                    "InstanceId": "i-SECOND-INSTANCEID"
                }
            ],
            "DNSName": "my-name-foo.us-east-1.elb.amazonaws.com",
            "SecurityGroups": [],
            "Policies": {
                "LBCookieStickinessPolicies": [],
                "AppCookieStickinessPolicies": [],
                "OtherPolicies": [
                    "AWSConsole-SSLNegotiationPolicy-my-name"
                ]
            },
            "LoadBalancerName": "my-name",
            "CreatedTime": "2013-08-05T16:55:22.630Z",
            "AvailabilityZones": [
                "us-east-1d"
            ],
            "Scheme": "internet-facing",
            "SourceSecurityGroup": {
                "OwnerAlias": "amazon-elb",
                "GroupName": "amazon-elb-sg"
            }
        }
    ]
}

The data is in LoadBalancerDescriptions.Instances.

My loadbalancer is called my-name — this is the name you selected when you created it.

Old answer below!

I'm not familiar with the cli tool, but I used the API.

I'd check these two requests:

  • DescribeLoadBalancers
  • DescribeInstanceHealth

The cli tool probably has something to resemble these?

HTH!