Reputation: 652
How can I assign a new IP address (or Elastic IP) to an already existing AWS EC2 instance using boto library.
Upvotes: 0
Views: 1183
Reputation: 45223
Make sure you have set properly with ~/.boto
and connect to aws, have the boto module ready in python. If not, go through this first: Getting Started with Boto
For example, you need assign a new EIP 54.12.23.34
to the instance i-12345678
Make sure, EIP has been allocated(existed) and you can get its allocation_id
.
$ python
>>> import boto.ec2
>>> conn = boto.ec2.connect_to_region("us-west-2")
>>> conn.get_all_addresses()
>>> allocation=conn.get_all_addresses(filters={'public_ip': '54.12.23.34'})[0].allocation_id
u'eipalloc-b43b3qds'
then call associate_address and give instance_id
or private_ip_address
, then you should be fine to assign the EIP to Amazon EC2 instance using boto library
associate_address(instance_id=None, public_ip=None, allocation_id=None, network_interface_id=None, private_ip_address=None, allow_reassociation=False, dry_run=False)
So you will have the command as:
>>> conn.associate_address(instance_id='i-12345678', allocation_id=allocation)
Upvotes: 1