Sanjyoti Patil
Sanjyoti Patil

Reputation: 1

How to get Target group of running EC2 instances using AWS API's

I am trying to write a lambda where I have requirement to get all running instances from the target group. Once I have instance ID we need to get the meta-data about that instance and set those values into the lambda. In order to start I have looked at AWS SDK documentation https://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_DescribeTargetGroups.html https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/examples-ec2-instances.html
But I am not sure if this a correct way to start with my problem. How can I achieve this?

Upvotes: 0

Views: 2368

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269470

Here's some examples for Python, but there are matching commands in every AWS SDK for other languages.

Based on advice from how to get list of registered targets in AWS target group via CLI, you can obtain a list of instances registered to a Target Group with describe_target_health(). It will return a list of targets, including the Instance ID:

{
    'TargetHealthDescriptions': [
        {
            'Target': {
                'Id': 'i-0f76fade',
                'Port': 80,
            },
            ...
        }
    ]
}

You could then call describe_instances() to obtain information about each instance. To make things quicker, you can pass multiple Instance IDs to the describe_instances() call to obtain information about all instances from a single API call.

Upvotes: 1

Related Questions