Created
March 14, 2023 14:42
-
-
Save hetul99/fe4f1efdf1302dc13754b81bb6f49efd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Prompt the user to enter the instance ID to check for | |
read -p "Enter the instance ID to check for: " INSTANCE_ID | |
# Get a list of all load balancer ARNs in the current AWS account | |
LB_ARNS=$(aws elbv2 describe-load-balancers --query "LoadBalancers[].LoadBalancerArn" --output text) | |
# Loop through each load balancer ARN and check if the instance is registered with any target groups | |
for LB_ARN in $LB_ARNS | |
do | |
# Get a list of all target groups associated with the current load balancer | |
TG_ARNS=$(aws elbv2 describe-target-groups --load-balancer-arn $LB_ARN --query "TargetGroups[].TargetGroupArn" --output text) | |
# Loop through each target group ARN and check if the instance is registered with it | |
for TG_ARN in $TG_ARNS | |
do | |
# Get a list of all registered targets in the current target group | |
TARGETS=$(aws elbv2 describe-target-health --target-group-arn $TG_ARN --query "TargetHealthDescriptions[?Target.Id=='$INSTANCE_ID'].{TargetHealth:TargetHealth.State,TargetGroupArn:TargetGroupArn,LoadBalancerArn:LoadBalancerArn}" --output json) | |
# If the list of registered targets is not empty, print the load balancer and target group information | |
if [ "$TARGETS" != "[]" ] | |
then | |
echo "Instance $INSTANCE_ID is registered with the following load balancer and target group:" | |
echo "Load balancer ARN: $LB_ARN" | |
echo "Target group ARN: $TG_ARN" | |
echo "Target health: $(echo $TARGETS | jq -r '.[0].TargetHealth')" | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment