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.
Thanks!
If anybody comes here looking for a pure bash solution.
Using jq to filter and parse the response of AWS CLI :
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
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:
try this script:
Sure. Use the aws cli:
The information you're looking for is in
LoadBalancerDescriptions.Instances
.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.
I'd like to add to Paul's excellent answer. This script will prompt for an EC2 instance ID