Python ec2和boto3中过滤器的使用混乱
我正在尝试打印实例的公共DNS名称 所以我在这里写了一个代码Python ec2和boto3中过滤器的使用混乱,python,python-3.x,amazon-web-services,amazon-ec2,boto3,Python,Python 3.x,Amazon Web Services,Amazon 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 =
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])
以你的名义
response = client.describe_instances(InstanceIds = [inst[0].instance_id])
AttributeError: 'str' object has no attribute 'instance_id'
如何在这里使用筛选器?您将
实例id
作为inst
传递,它是一个字符串,因此inst[0]
仍然是一个长度为1的字符串。我无法理解您是否可以详细说明。首先您有实例id=“I-03e7f6391a0f523ee”
,然后是获取名称(实例id)
。好的,这就是问题所在。response=client.description\u instances(instanceId=[instance\u id])
解决了问题。谢谢,顺便问一下,我想知道inst[0]包含哪些内容,我是python新手,还在学习