4

I am developing an application in Java and I am able to list the instances:

for (Reservation reservation : result.getReservations()) {
    for (Instance instance : reservation.getInstances()) {
        System.out.println("Instance id:"
                + instance.getInstanceId());
    }
}

How do I get the instance name?

4
  • Try Instance.getTags(). Commented Jan 22, 2017 at 8:04
  • Got it. Thanks.Instance.getTags() give list of tags and Tag.value is the instance name. Commented Jan 22, 2017 at 9:48
  • @shmosel: Can you please post as an answer for Viswanath to accept so that others can find your solution? Commented Jan 22, 2017 at 11:06
  • @DaveMaple I don't know enough to post a substantive answer. Feel free to post one yourself. Commented Jan 22, 2017 at 11:07

2 Answers 2

7

You can access tags associated with the instance:

for (Reservation reservation : result.getReservations()) {
    for (Instance instance : reservation.getInstances()) {
        System.out.println("Instance id:" + instance.getInstanceId());

        if (instance.getTags() != null) {
            for (Tag tag : instance.getTags()) {
                System.out.println(String.format(
                    "%s: %s", 
                    tag.getKey(), 
                    tag.getValue()
                ));
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

To get an instance name you need to use something like this:

        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                Tag tagName = instance.getTags().stream()
                        .filter(o -> o.getKey().equals("Name"))
                        .findFirst()
                        .orElse(new Tag("Name", "name not found"));

                System.out.println("Found instance with ID: " + instance.getInstanceId()
                        + ", NAME: " + tagName.getValue()
                        + ", TYPE: " + instance.getInstanceType()
                );
            }
        }

This way you extract Tag that has key:"Name" and value is what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.