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
질문됨 3달 전241회 조회
1개 답변
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

답변함 3달 전
  • 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?"

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠