首页 > 解决方案 > 在 ec2 和 boto3 中使用过滤器的混乱

问题描述

我正在尝试打印我的实例的公共 DNS 名称

所以我在这里写了一个代码

import sys
import boto3


instance_id = "i-03e7f6391a0f523ee"
action = 'ON'

ec2 = boto3.client('ec2')

if action == 'ON':
   response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)
else:
    response = ec2.stop_instances(InstanceIds=[instance_id], DryRun=False)
print(response)

resp=ec2.describe_network_interfaces();
print ("printing pub dns name")
print(resp['NetworkInterfaces'][0]['Association']['PublicDnsName'])



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(instance_id)
print (foo)

我得到的错误是在行

response = client.describe_instances(InstanceIds = [inst[0].instance_id])

在 get_name 中

    response = client.describe_instances(InstanceIds = [inst[0].instance_id])
AttributeError: 'str' object has no attribute 'instance_id'

我如何在这里使用过滤器?

标签: pythonpython-3.xamazon-web-servicesamazon-ec2boto3

解决方案


推荐阅读