21

We are trying to get the state of a registered target (instance) in a target group. This can be done with the command aws elbv2 describe-target-health --target-group-arn ${TG} --targets Id=${ID},Port=${PORT}, reference. We are able to get the PORT via the aws ecs describe-tasks --cluster $CLUSTER --tasks $task command, reference. But how can the target's instance Id be retrieved via CLI?

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68

1 Answers1

50

This will give you the array of registered instance ID in a target group. When you have target ARN why you using target ID? so I am skipping target ID and using just target ARN.

aws elbv2 describe-target-health --target-group-arn ${TG}  --query 'TargetHealthDescriptions[*].Target.Id'
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • this command will list down both `"State": "unused"` and `"State": "healthy"` instances. What if I want to list down healthy instances only? – Alfred Dec 08 '21 at 11:27
  • 1
    @Alfred then you should parse the output for your use case. – purecharger May 23 '22 at 18:50
  • Alfred: you can print the state of the instances like this: aws elbv2 describe-target-health --target-group-arn "${TG}" --query 'TargetHealthDescriptions[*].[Target.Id,TargetHealth.State]' --output text – Peycho Dimitrov Feb 05 '23 at 07:09