7
我使用ipython来了解Boto3并与EC2实例交互。下面是我使用创建一个实例代码:使用BOTO3检索EC2实例的公共dns
import boto3
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
new_instance = ec2.create_instances(
ImageId='ami-d05e75b8',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName=<name_of_my_key>,
SecurityGroups=['<security_group_name>'],
DryRun = False
)
这种精细启动EC2实例,我可以从AWS控制台的公共DNS名称,IP等信息。但是,当我试图让使用博托公共DNS,这样做:
new_instance[0].public_dns_name
返回空行情。然而,其他实例细节,如:
new_instance[0].instance_type
返回正确的信息。
任何想法?谢谢。
编辑:
所以,如果我做的:
def get_name(inst):
client = boto3.client('ec2')
response = client.describe_instances(InstanceIds = [inst[0].instance_id])
foo = response['Reservations'][0]['Instances'][0]['NetworkInterfaces'][0]['Association']['PublicDnsName']
return foo
foo = get_name(new_instance)
print foo
然后,它会返回公共DNS。但是为什么我需要这么做呢?