Expected EC2 DescribeInstances Response when instance has been terminated

0

I am trying to programmatically determine whether an EC2 instance with a given instance id has been terminated. Every instanceId I am using belonged to an instance that I owned at one point, but may have been terminated at some point in history.

I am using the Java AWS SDK, version 2.22.10, though we try to keep up with the latest version.

This is the SDK method I am using: https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/ec2/Ec2Client.html#describeInstances(software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest)

and I am constructing the request like this:

DescribeInstancesRequest request = DescribeInstancesRequest.builder()
    .instanceIds(instanceId)
    .build();

I'm noticing something odd. If the instance has been terminated, there are at least two different ways the SDK/API will respond:

  1. It will return a successful response with an empty list of instances.
  2. It will throw a software.amazon.awssdk.services.ec2.model.Ec2Exception with a 400 status code and a message indicating that the instance does not exist.

My question is: What is the difference between these two responses? Under what conditions will describing a terminated instance cause it to be omitted from the response vs return a 400?

Ry
asked 3 months ago224 views
1 Answer
0

As you may know, an instance sticks around for a bit after being terminated (you can even view it in the console). Unless you have a log that stated it was terminated it can be hard to know for certain. For example the API may respond as if the instance never existed, or what if its just an outage preventing you from getting the correct information back.

Pardon my unsolicited suggestion. When you get a 400, you catch the 400 and check CloudTrail (via API) to see if there is a log. However there is a limit to how long cloud train keeps that information, so you could also write it to an S3 bucket and query there if the instance was terminated.

See this SO answer for hints too https://stackoverflow.com/a/35054667/419097

answered 3 months ago
  • Providing your application with a guaranteed record of terminated instances will help stability and performance.

  • an instance sticks around for a bit after being terminated

    Yes, but the instance state is included in the response, so I can determine whether it has been terminated.

    what if its just an outage preventing you from getting the correct information back

    The 400 response includes a message that the instance was not found, so it is clear whether it's not found vs an outage.

    I consider an instance terminated if 1) state is terminated, 2) instance not in response, or 3) 400 with not found message.

    My question is simply "what is the difference between case 2 and case 3?"

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