Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method '[]' for nil:NilClass (NoMethodError) when getting state of previously terminated instance #1449

Closed
DenverJ opened this issue Mar 8, 2017 · 8 comments

Comments

@DenverJ
Copy link

DenverJ commented Mar 8, 2017

Using this example code

ec2 = Aws::EC2::Resource.new(region: 'us-east-1')
instance = ec2.instance('i-abcdefgh123456789')
is_terminated = instance.state.name == "terminated"

I get an error like
E, [2017-03-08T16:02:01.299512 #31176] ERROR -- : undefined method `[]' for nil:NilClass (NoMethodError) C:/Dev/SDK/Ruby200/lib/ruby/gems/2.0.0/gems/aws-sdk-resources-2.6.3/lib/aws-sdk-resources/resource.rb:223:in `block in add_data_attribute'

When instance id 'i-abcdefgh123456789' is a previously terminated instance (but no longer visible via the API as being in a 'terminated' state).

It looks like the AWS API returns a different response for a valid instance id that was previously terminated vs an instance id that never existed in the account. See example API responses below.

Previously terminated instance...

<?xml version="1.0" encoding="UTF-8"?>
<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2016-04-01/">
    <requestId>7b9e5362-1cec-4489-8c8d-890cf7d3d211</requestId>
    <reservationSet/>
</DescribeInstancesResponse>

Nonsense instance id that never existed in the account...

<?xml version="1.0" encoding="UTF-8"?>\n<Response>
    <Errors>
        <Error>
            <Code>InvalidInstanceID.NotFound</Code>
            <Message>The instance ID 'i-0c2ae750b43b49634' does not exist</Message>
        </Error>
    </Errors>
    <RequestID>b3d2dafc-5b89-45fb-b3a6-9ca2740bb0ee</RequestID>
</Response>
@cjyclaire
Copy link
Contributor

Taking a look, trying to reproduce ...

@cjyclaire
Copy link
Contributor

@DenverJ I couldn't reproduce this error with latest SDK, I could see it responses correctly with either freshly terminated instances or an invalid id.

It looks like the AWS API returns a different response for a valid instance id that was previously terminated vs an instance id that never existed in the account

This is an expected behavior for recent terminated instance.

So it would be great if you could provide more information, such as, is this causing errors everytime? or this only happens with terminated instances?

Also, our #getting help section has been updated recently as well, feel free to get involved in channels : ). I'd be happy to dig deeper in to issue if you feel it might be a potential bug with our SDK.

@DenverJ
Copy link
Author

DenverJ commented Mar 9, 2017

Yes, I would like to report this as a bug running version 2.8.3

I can reproduce this every time (the key being on an instance that has been terminated but AFTER it disappears from the console and the API).

As I was trying to illustrate above, a describeInstances API call filtering on a recently terminated instance returns an object in the array just like filtering for a running instance would. After some time (~1hr) the describeInstances response returns an EMPTY array.

So to recreate the issue...

  • create an instance
  • terminate the instance
  • wait at least 1hr (or until it disappears from the console)
  • run the example code above

@cjyclaire
Copy link
Contributor

@DenverJ Thanks for getting back! I'll try that again since the last time when I'm trying to reproduce, it's kind of freshly terminated : )

@awood45
Copy link
Member

awood45 commented Mar 9, 2017

Here's a repro assuming the service returns as described:

ec2.stub_responses(describe_instances: [{reservations: []}])
r = Aws::EC2::Resource.new(region: "us-east-1", client: ec2)
r.instance("i-123456789").state.name

That returns the junk error.

@awood45
Copy link
Member

awood45 commented Mar 9, 2017

I can see that the EC2 resource definition seems to take for granted that reservations cannot be empty. How we can wrap this with a more user friendly exception in a general way is an interesting question.

@chef1729
Copy link

chef1729 commented May 2, 2017

I have the same issue what was done to fix it ?

@cjyclaire
Copy link
Contributor

You can always use client directly with API calls to workaround the issue.

Tracking it as a resource model limitation at backlog, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants