boto / boto

For the latest version of boto, see https://github.com/boto/boto3 -- Python interface to Amazon Web Services
http://docs.pythonboto.org/
Other
6.48k stars 2.26k forks source link

boto.ec2.connection.get_image throws exception when AMI ID is invalid. #2235

Open maitreya1975 opened 10 years ago

maitreya1975 commented 10 years ago

The documentation (http://docs.pythonboto.org/en/latest/ref/ec2.html#boto.ec2.connection.EC2Connection.get_image) states that the method get_image Returns:
The EC2 Image specified or None if the image is not found.

However, if the AMI ID is not found, it throws an exception instead of returning None.

Sample Verbose output when invoking get_image with an invalid AMI ID:

Traceback (most recent call last):
  File "getami.py", line 6, in <module>
    image = ec2.get_image('ami-c2492bf3')
  File "/usr/local/lib/python2.7/site-packages/boto/ec2/connection.py", line 260, in get_image
    return self.get_all_images(image_ids=[image_id], dry_run=dry_run)[0]
  File "/usr/local/lib/python2.7/site-packages/boto/ec2/connection.py", line 186, in get_all_images
    [('item', Image)], verb='POST')
  File "/usr/local/lib/python2.7/site-packages/boto/connection.py", line 1157, in get_list
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidAMIID.NotFound</Code><Message>The image id '[ami-c2492bf3]' does not exist</Message></Error></Errors><RequestID>3dd34687-b22b-4dc9-96f3-3a283b1ecd1a</RequestID></Response>
Constantin07 commented 8 years ago

I'm facing the same issue and it happens sporadically.

    img = ec2.get_image(image_id)
File "/usr/lib/python2.7/site-packages/boto/ec2/connection.py", line 263, in get_image
    return self.get_all_images(image_ids=[image_id], dry_run=dry_run)[0]
File "/usr/lib/python2.7/site-packages/boto/ec2/connection.py", line 189, in get_all_images
    [('item', Image)], verb='POST')
File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1186, in get_list
    raise self.ResponseError(response.status, response.reason, body)
    boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
    <?xml version="1.0" encoding="UTF-8"?>
    <Response><Errors><Error><Code>InvalidAMIID.NotFound</Code><Message>The image id '[ami-9fec6bec]' does not exist</Message></Error></Errors><RequestID>f62acd0a-31f1-46ff-8f81-c3bce3287e61</RequestID></Response>

Is there any PR to merge the fix?