我正在使用 boto3 创建一个 ec2 实例,我想打印该新实例的 IP 地址。
ec2 = boto3.resource('ec2')
# create the instance
new_instance = ec2.create_instances(
ImageId='###',
MinCount = 1,
MaxCount = 1,
InstanceType = 't2.nano',
KeyName = "key",
SecurityGroupIds = ["###"]
)
... wait until running ...
ip = new_instance[0].ipv4 # something like this
有没有办法在运行后做这样的事情?
最佳答案
ec2.create_instances返回 ec2.Instance objects 的列表. ec2.Instance 对象有一个名为 private_ip_address
的属性。您可以使用它来获取私有(private) IP 地址。
旁注(基于代码示例中的注释)您还可以使用 wait_until_running服务员让您的代码暂停,直到实例运行。
# create the instance
new_instance = ec2.create_instances(...)
# use a waiter on the instance to wait until running
new_instance[0].wait_until_running()
ip = new_instance[0].private_ip_address
public_ip = new_instance[0].public_ip_address
关于python - 使用 boto3 获取新创建的亚马逊 ec2 实例的公共(public) IPv4 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64664813/