Wil*_*ill 12 python dns amazon-web-services boto3 aws-ec2
我正在使用ipython来了解Boto3并与EC2实例进行交互.这是我用来创建实例的代码:
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和其他信息.但是,当我尝试使用Boto获取公共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.但对我来说,为什么我需要做所有这些都没有意义.
Jor*_*ips 19
Instance您获得的对象仅使用create_instances调用的响应属性进行补充.由于DNS名称在实例达到运行状态[1]之前不可用,因此不会立即显示.我想你创建实例和调用describe实例之间的时间足够长,微实例可以启动.
import boto3
ec2 = boto3.resource('ec2')
instances = ec2.create_instances(
    ImageId='ami-f0091d91',
    MinCount=1,
    MaxCount=1,
    InstanceType='t2.micro',
    KeyName='<KEY-NAME>',
    SecurityGroups=['<GROUP-NAME>'])
instance = instances[0]
# Wait for the instance to enter the running state
instance.wait_until_running()
# Reload the instance attributes
instance.load()
print(instance.public_dns_name)
| 归档时间: | 
 | 
| 查看次数: | 6472 次 | 
| 最近记录: |