Method for finding instances attached to an ELB

I am using Amazon AWS ELB command line tools. Is there a way to know instances attached to a specific elastic load balancer (ELB)?

+10
amazon-web-services amazon-ec2 amazon-elb
source share
11 answers

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

I installed the new cli AWI tools:

$ pip install awscli 

Then executed:

 $ 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 stored in ~/.aws/config .

Then I can find instances related to loadbalancer as follows:

 $ 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 chose when you created it.

Old answer below!

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

I would check these two queries:

Does the cli tool probably have something like this?

NTN!

+9
source share

Assuming you are aws-cli and jq , you can use the following command to get the associated ec2 instance identifiers:

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

This will return the ec2 identifiers associated with this ELB.

Side note. I recommend that you configure aws cli profiles , so you don’t have to bother with environment variables and area parameters (as much).

+7
source share

Because I like the answers that can be used with a minimal amount of searching / replacing and copying folders

Prerequisites: aws-cli configured

 pip install awscli aws configure 

Customize: your name is ELB

 $ELB_NAME = "Your-elb-name" 

Copy-n-paste in the 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 

Lists public IP addresses. You can also simply query in parentheses for ID in $(...) to just get instance IDs

Want something else?

Feel free to take a look at the structure.

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

and modify the request accordingly!

+5
source share

If someone came here from a search, why the elb-describe-lbs command does not return anything when they have an ELB and it works, I realized that I need to add EC2_REGION=eu-west-1 to my environment variables (or use the command elb-describe-lbs --region )

+2
source share

If you want to see all your ELBs and attached instances, use JMESPath as follows:

 aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[*].InstanceId}[*]. {ELB:ID,InstanceId:InstanceId[*]}" --output=json 

Result

 [ { "ELB": "my_name", "InstanceId": [ "i-0cc72" ] }, { "ELB": "my_name2", "InstanceId": [ "i-02ff5f", "i-09e467" ] } ] 

If you know the name of ELB and want to see what the application is, use JMESPath as follows:

 aws elb describe-load-balancers --load-balancer-name "my_name" --query "LoadBalancerDescriptions[].{ID:LoadBalancerName,InstanceId:Instances[].InstanceId}[].{ELB:ID,InstanceId:InstanceId[]}" --output=json 

Result:

 [ { "ELB": "my_name", "InstanceId": [ "i-02ff5f72", "i-09e46743" ] } ] 
+1
source share

replace INSTANCEID with the actual instance identifier

aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[?InstanceId=='INSTANCEID'].InstanceId}[*].{ID:ID,InstanceId:InstanceId[0]}" --output=text | grep INSTANCEID | awk '{print $1}'

0
source share

In node.js, you can do this using aws-sdk .

 var AWS = require('aws-sdk') var options = { accessKeyId: 'accessKeyId', secretAccessKey: 'secretAccessKey', region: 'region' } var elb = new AWS.ELB(options) elb.describeLoadBalancers({LoadBalancerNames: ['elbName']}, function(err, data) { if (err) { console.log('err: ', err) } else { console.log('data: ', data.LoadBalancerDescriptions) } }) 

data.LoadBalancerDescriptions is an array, and each element in the array is an object with an Instances property that has an instance identifier.

0
source share

You can iterate over all loadable balancer instance ids as follows:

 while read -r lb ; do echo -e "\n\n start lb: $lb " ; \ echo run cmd on lb: $lb ; echo " stop lb: $lb" ; \ done < <(aws elb describe-load-balancers --query \ 'LoadBalancerDescriptions[].Instances[].InstanceId' \ --profile dev|perl -nle 's/\s+/\n/g;print') 

You can scroll the names of your load balancer as follows:

  # how-to loop trough all your load balancer names while read -r lb ; do \ echo -e "\n\n start lb: $lb " ; \ echo run cmd on lb: $lb ; \ echo " stop lb: $lb" ; \ done < <(aws elb describe-load-balancers --query \ 'LoadBalancerDescriptions[].LoadBalancerName' \ --profile rnd|perl -nle 's/\s+/\n/g;print') 

Provided you configured your aws cli: src: http://docs.aws.amazon.com/cli/latest/topic/config-vars.html cat <"EOF"> ~ / .aws / config

 [profile dev] output = text region = us-east-1 [profile dev] output = text region = us-east-1 [default] output = text region = Global EOF 

And set up your security credentials:

  # in aws admin console : # Services => iam => users => <<your_username>> => Security Credentials => Access Keys # configure the aws cli cat << "EOF" > ~/.aws/credentials [dev] aws_access_key_id = <<your_aws_access_key_id_in_the_dev_environment>> aws_secret_access_key = <<your_aws_secret_access_key_in_dev_env>> [dev] aws_access_key_id = <<your_aws_access_key_id_in_the_dev_environment>> aws_secret_access_key = <<your_aws_secret_access_key_in_dev_env>> [default] aws_access_key_id = <<your_aws_access_key_id_in_the_dev_environment>> aws_secret_access_key = <<your_aws_secret_access_key_in_dev_env>> EOF 
0
source share

aws elb description-load-balrsrs - load balancer name "LB_NAME" | grep "InstanceId" | awk '{print $ 2}' | sed 's / \ "// g'

0
source share

First do elb-describe-lbs to get a list of load balancers and their names.

Then do elb-describe-instance-health <LB_NAME> to get a list of instances behind this load balancer. LB_NAME is the value of the second column in the output of elb-describe-lbs .

-one
source share

You can use AWS command line tools with several bash pipelines:

 elb-describe-instance-health loadbalancer_name --region eu-west-1 | awk '{ print $2 }' | xargs ec2-describe-instances --region eu-west-1 | grep ^INSTANCE | awk '{ print $4 }' 

This will give you a public DNS name for each instance attached to the ELB, you can change the awk columns accordingly to get other information.

-2
source share

All Articles