如何使用 boto 库更改 Amazon EC2 实例的 IP 地址
How to change the IP address of Amazon EC2 instance using boto library
如何使用 boto 库将新的 IP 地址(或弹性 IP)分配给现有的 AWS EC2 实例。
确保您已正确设置 ~/.boto
并连接到 aws,在 python 中准备好 boto 模块。如果没有,请先完成此操作:Getting Started with Boto
例如,您需要为实例i-12345678
分配一个新的EIP 54.12.23.34
确保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'
然后调用associate_address并给出instance_id
或private_ip_address
,然后你应该可以使用boto库
将EIP分配给Amazon EC2实例
associate_address 用法:
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)
因此您将得到如下命令:
>>> conn.associate_address(instance_id='i-12345678', allocation_id=allocation)
如何使用 boto 库将新的 IP 地址(或弹性 IP)分配给现有的 AWS EC2 实例。
确保您已正确设置 ~/.boto
并连接到 aws,在 python 中准备好 boto 模块。如果没有,请先完成此操作:Getting Started with Boto
例如,您需要为实例i-12345678
54.12.23.34
确保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'
然后调用associate_address并给出instance_id
或private_ip_address
,然后你应该可以使用boto库
associate_address 用法:
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)
因此您将得到如下命令:
>>> conn.associate_address(instance_id='i-12345678', allocation_id=allocation)