gpt4 book ai didi

python - 如何使用 boto 将公共(public) IP 自动分配给 EC2 实例

转载 作者:IT老高 更新时间:2023-10-28 20:50:33 26 4
gpt4 key购买 nike

我必须在给定的子网中使用 ec2.run_instances 启动一台新机器,但还要自动分配公共(public) ip(不是固定的弹性 ip)。

当人们通过请求实例(实例详细信息)从 Amazon 的 Web EC2 管理器启动新机器时,会出现一个名为分配公共(public) IP 的复选框来自动分配公共(public) IP。看到它在屏幕截图中突出显示:

Request Instance wizard

如何使用 boto 实现复选框功能?

最佳答案

有趣的是,似乎没有多少人遇到过这个问题。 对我来说,能够做到这一点非常重要。如果没有此功能,则无法从启动到非默认子网的实例访问互联网。

boto 文档没有提供任何帮助,最近修复了一个相关的错误,请参见:https://github.com/boto/boto/pull/1705 .

需要注意的是 subnet_id 和安全 groups 必须提供给网络接口(interface) NetworkInterfaceSpecification 而不是 run_instance .

import time
import boto
import boto.ec2.networkinterface

from settings.settings import AWS_ACCESS_GENERIC

ec2 = boto.connect_ec2(*AWS_ACCESS_GENERIC)

interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(subnet_id='subnet-11d02d71',
groups=['sg-0365c56d'],
associate_public_ip_address=True)
interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface)

reservation = ec2.run_instances(image_id='ami-a1074dc8',
instance_type='t1.micro',
#the following two arguments are provided in the network_interface
#instead at the global level !!
#'security_group_ids': ['sg-0365c56d'],
#'subnet_id': 'subnet-11d02d71',
network_interfaces=interfaces,
key_name='keyPairName')

instance = reservation.instances[0]
instance.update()
while instance.state == "pending":
print instance, instance.state
time.sleep(5)
instance.update()

instance.add_tag("Name", "some name")

print "done", instance

关于python - 如何使用 boto 将公共(public) IP 自动分配给 EC2 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19029588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com