[英]How to change the IP address of Amazon EC2 instance using boto library
如何使用boto库为现有的AWS EC2实例分配新的IP地址(或弹性IP)。
Make sure you have set properly with ~/.boto
and connect to aws, have the boto module ready in python. 确保已使用
~/.boto
正确设置并连接到aws,并在python中准备好了boto模块。 If not, go through this first: Getting Started with Boto 如果没有,请先完成以下步骤: Boto入门
For example, you need assign a new EIP 54.12.23.34
to the instance i-12345678
例如,您需要为实例
i-12345678
分配新的EIP 54.12.23.34
Make sure, EIP has been allocated(existed) and you can get its allocation_id
. 确保已分配(存在)EIP,并且可以获取其
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
或private_ip_address
,那么您可以使用boto库将EIP分配给Amazon EC2实例
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)
associate_address(instance_id = None,public_ip = None,distribution_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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.