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.release_address() fails to release public Elastic IPs. #2727

Open cjbern opened 10 years ago

cjbern commented 10 years ago

python 3.4.0 The method fails with a 400 Bad Request error, and seems to want an allocation id, despite the fact that the EC2 public elastic IP was never assigned to a VPC allocation directly.

>>> import boto, boto.ec2
>>> boto.__version__
'2.34.0'
>>> conn=boto.ec2.connect_to_region('us-west-1')
>>> eip=conn.allocate_address()
>>> eip
Address:54.215.129.85
>>> conn.release_address(public_ip='54.215.129.85')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/byeh/aws/boto/boto/ec2/connection.py", line 2077, in release_address
    return self.get_status('ReleaseAddress', params, verb='POST')
  File "/home/byeh/aws/boto/boto/connection.py", line 1224, in get_status
    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>InvalidParameterValue</Code><Message>You must specify an allocation id when releasing a VPC elastic IP address</Message></Error></Errors><RequestID>dab8aa51-52b4-48a5-9717-bada23ab56f6</RequestID></Response>

Omitting the keyword doesn't work either:

>>> conn.release_address('54.215.129.85')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/byeh/aws/boto/boto/ec2/connection.py", line 2077, in release_address
    return self.get_status('ReleaseAddress', params, verb='POST')
  File "/home/byeh/aws/boto/boto/connection.py", line 1224, in get_status
    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>InvalidParameterValue</Code><Message>You must specify an allocation id when releasing a VPC elastic IP address</Message></Error></Errors><RequestID>8f665b18-b7a2-468c-b2e1-a78e53023417</RequestID></Response>

On the other hand, boto.ec2.address.release() does release a public Elastic IP properly:

>>> eip.release()
True
>>>
srri-zz commented 9 years ago

So this is happening because the function release_address() of the connection class requires an allocation ID if the elastic IP is a VPC EIP. the reason eip.release() works is because that method gets the allocation id from itself, it is stored in the instance of the address object (eip) and attempts to use the allocation id if available. See https://github.com/boto/boto/blob/develop/boto/ec2/connection.py#L2069-L2075 versus https://github.com/boto/boto/blob/develop/boto/ec2/address.py#L80-L88

You would need to do this instead in order to get around and provide the release_address() function the information it needs:

>>> import boto, boto.ec2
>>> conn = boto.ec2.connect_to_region('us-west-1')
>>> eip = conn.allocate_address()
>>> eip
Address:54.183.213.100
>>> conn.release_address(allocation_id=eip.allocation_id)
True
>>> boto.__version__
'2.34.0'
cjbern commented 9 years ago

So I've done a little bit more reading, and discovered the difference between EC2-Classic and EC2-VPC. AIUI, the public_ip keywords only apply to EC2-Classic, which is only available if you have an AWS account dating from before the introduction of EC2-VPC. Newer accounts do not have access to EC2-Classic, only EC2-VPC, so basically can only use the allocation_id keywords for the Elastic IP related methods.

So I guess I'm saying that the Boto documentation should be made to more accurately reflect what AWS currently calls objects, to avoid confusing newer users. Basically change: 'EC2 Elastic IPs' and 'VPC Elastic IPs' To: 'EC2-Classic Elastic IPs' and 'EC2-VPC Elastic IPs'.

So I withdraw stating that this is a code bug, but do state that it is currently a documentation bug.