Using boto to assign elastic ip to instance

1,867 views
Skip to first unread message

Deependra

unread,
Dec 6, 2010, 11:47:54 AM12/6/10
to boto-users
Hi all,

Just started using boto. Had a small doubt.

I have couple of Windows instances on EC2 which I would like to start/
stop via a python script. After the instance has started I would like
to associate a specific elastic ip to each one of them. If I have 3
instance suppose each with a elastic ip:

i-xxxxxx 51.x.x.x
i-yyyyyy 51.x.x.y
i-zzzzzz 51.x.x.z

How do I make sure that these instances get the same IP when they
start back?

I heard about something call Tagging a instance in EC2 not sure if
that is possible via boto.

Regards
Deependra

Matt Billenstein

unread,
Dec 6, 2010, 3:15:13 PM12/6/10
to boto-...@googlegroups.com
See the docs on the connection object:

http://boto.s3.amazonaws.com/ref/ec2.html#module-boto.ec2.connection


I would basically do:

conn = boto.ec2.connect_to_region(
region_name = 'REGION_NAME',
aws_access_key_id = 'ACCESS_KEY',
aws_secret_access_key = 'SECRET_ACCESS_KEY',
)

ip_to_inst_map = {
'51.x.x.x' : 'i-xxxxxx',
'51.x.x.y' : 'i-yyyyyy',
'51.x.x.z' : 'i-zzzzzz',
}

for a in conn.get_all_addresses():
inst_id = ip_to_inst_map.get(a.public_ip)
if inst_id and a.instance_id != inst_id:
success = a.associate(inst_id)
if not success:
print "Failed to associate ip %s to inst %s" % (a.public_ip,
inst_id)


Although it appears you can just call:

conn.associate_address(instance_id, public_ip)

and if the address is already associated with that instance I'd expect
it to do nothing...

m

> --
> You received this message because you are subscribed to the Google Groups "boto-users" group.
> To post to this group, send email to boto-...@googlegroups.com.
> To unsubscribe from this group, send email to boto-users+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/boto-users?hl=en.
>

--
Matt Billenstein
ma...@vazor.com
http://www.vazor.com/

Reply all
Reply to author
Forward
0 new messages