Is there a way to know if number of Load Balancer's backend changed?


I frequently run below script to get actual IP addresses of ec2 instances working as a Load balancer's backend.

TG_ELEMENTS=`aws elbv2 describe-target-health --target-group-arn $TG_ARN --query 'TargetHealthDescriptions[*].Target.Id' --profile $PROFILE`

for i in $TG_ELEMENTS ; do
    aws ec2 describe-instances --instance-id $i --query 'Reservations[].Instances[].PrivateIpAddress' --profile $PROFILE

Mostly returned TG_ELEMENTS list is the same (scalling is not so frequent). I was wondering if there is a way to do some small query to know if target group / load balancer's backend change, and if so do some extra queries to get these IP address list?

1 Answer

This doesn't exactly answer your question but you may want to consider using eventbridge events for triggering an action for scaling activity. See here for list of events. You can for example get an event when a new instance is launched and trigger a lambda which can get any additional information you need (eg ip address) as the instance id is emitted in the event. You can then trigger any other action you need.

Another common method to perform actions during scaling events is to use lifecycle hooks. With lifecycle hooks you can pause the scale out and scale in activities and perform a custom action

Both these methods can be used to let you know the ip addresses of any instances which are being added to your autoscale groups. The actions could be send an SNS notification via email or update a dynamodb table to maintain an accurate list of ip addresses

answered a year ago
  • Thank you for your comment, I'll try it. I'm also open to any other suggestions in that matter.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions