I created one ELB and attached a few instances to this ELB. So when I login into one of these attached instance, I would like to type a command or run a nodejs script that can return me the its ELB name. Is it possible? I know I can look up on AWS console but I'm looking for a way to look it up programmatically. If possible, I would like to see how it is done in either command or AWS Nodejs SDK.


  • Have you looked at the documentation for the API? Commented Apr 3, 2014 at 5:26
  • 1
    @DrewKhoury, yes I looked into metada service and doc but could not find anything related.
    – Nam Nguyen
    Commented Apr 3, 2014 at 5:27
  • Curious why you would want to do this. If it's simply to identify which application an instance is associated with tagging could be a better approach.
    – Tim
    Commented Jan 30, 2019 at 23:41

6 Answers 6


I'm not the greatest in JavaScript but I tested the code below and works. It basically uses the "describeLoadBalancers" API call to get a list of all your ELBs and then iterates through the result to look for your instance. If your instance is registered with a particular load balancer, it's name is output to the console:

// Require AWS SDK for Javascript
var AWS = require('aws-sdk');

// Set API Keys and Region
    "accessKeyId": "<your access key>", 
    "secretAccessKey": "<your secret key>",
    "region": "us-west-1" // specify your region

// Get All Load Balancers
function GetLoadBalancers(fn)
    var elb = new AWS.ELB();
    elb.describeLoadBalancers(null,function(err, data) {

// Loop through response to check if ELB contains myInstanceId
var myInstanceId = "<your instance id>";
      if(elb.Instances[0] != undefined){
        if (elb.Instances[0].InstanceId == myInstanceId){
  • Good but doesn't work with AWS.ELBv2. Field LoadBalancerDescriptions absent in JSON response of describeLoadBalancers in that case.
    – Atul
    Commented Aug 2, 2019 at 14:42

If anybody comes here looking for a pure bash solution.

Using jq to filter and parse the response of AWS CLI :

aws elb describe-load-balancers | jq -r '.LoadBalancerDescriptions[] | select(.Instances[].InstanceId == "<YOUR-INSTANCE-ID>") | .LoadBalancerName ' 

Also in aws-codedeploy-samples they define this function in common_functions.sh. I haven't tested it as I use ASG, but I suppose it will work

# Usage: get_elb_list <EC2 instance ID>
#   Finds all the ELBs that this instance is registered to. After execution, the variable
#   "INSTANCE_ELBS" will contain the list of load balancers for the given instance.
#   If the given instance ID isn't found registered to any ELBs, the function returns non-zero
get_elb_list() {
    local instance_id=$1

    local elb_list=""

    local all_balancers=$($AWS_CLI elb describe-load-balancers \
        --query LoadBalancerDescriptions[*].LoadBalancerName \
        --output text | sed -e $'s/\t/ /g')

    for elb in $all_balancers; do
        local instance_health
        instance_health=$(get_instance_health_elb $instance_id $elb)
        if [ $? == 0 ]; then
            elb_list="$elb_list $elb"

    if [ -z "$elb_list" ]; then
        return 1
        msg "Got load balancer list of: $elb_list"
        return 0

try this script:



aws elb describe-load-balancers --query \

Sure. Use the aws cli:

$ aws elb describe-load-balancers --load-balancer-name "your-elb-name"

The information you're looking for is in LoadBalancerDescriptions.Instances.

  • "your-elb-name" is the one that I don't have and also is the one that I want to get.
    – Nam Nguyen
    Commented Apr 3, 2014 at 5:26
  • 1
    Well then run the above command without the name flag to get all the ELBs and iterate through them. The information is all there. You just need to filter it out.
    – EEAA
    Commented Apr 3, 2014 at 6:45

An instance could be attached to any number of ELBs.

You could use the API to search all ELBs for your instance.

You might want to consider adding a TAG to your EC2 with information about which ELB it's attached to, so you can query it (the EC2) directly.

  • so there is no such command or file that can retrieve that info. Thanks Drew Khoury
    – Nam Nguyen
    Commented Apr 3, 2014 at 5:46
  • 1
    @NamNguyen - that's incorrect. All of the AWS API tools support tagging and querying via tags. Even the web console supports this. I think you'd be well-served by spending a couple hours reading AWS docs.
    – EEAA
    Commented Apr 3, 2014 at 6:54

I'd like to add to Paul's excellent answer. This script will prompt for an EC2 instance ID

echo "Enter EC2 instance name?" 

read instanceId

echo The instance name is: $instanceId

aws elb describe-load-balancers --query \

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .