[英]Python BOTO3 script is not returning name inside the tag
我需要提取 AWS EC2 的名称、实例 ID、状态并将其导出到 csv。 通过使用下面的代码,我得到了实例 ID 和状态。 名称在标签内,我的标签中有多个键值,如下所示:
"标签": [ { "Value": "ggggg", "Key": "bbbb" }, { "Value": "rrrrrr", "Key": "eeeee" }, { "Value": "uyyyutu", "Key": "hhhhhh" }, { "Value": "xxxxxxxx", "Key": "NAME" }, { "Value": "GREEN", "Key": "STATE" }, { "Value": "xxxxx", "Key": "yyyyy" } ]
如何从特定的 KEY=NAME 获取名称。 在使用以下代码时,我获取了标签中的所有值,但并未给出特定值。
import boto3
import csv
client = boto3.client('ec2')
response = client.describe_instances(
Filters=[
{
'Name':'tag:STATE','Values':['GREEN']
}
]
)
detail=[]
for Reservations in response["Reservations"]:
for Instances in Reservations["Instances"]:
detail.append({
'ID':Instances['InstanceId'],
'Status':Instances['State']['Name'],
'Name':Instances['Tags']['Key'=='Name']['Value']
})
header=['ID','Status','Name']
with open('EC2_Detail.csv','w') as file:
writer=csv.DictWriter(file, fieldnames=header)
writer.writeheader()
writer.writerows(detail)
尝试:
[x for x in Instances['Tags'] if x['Key'] == 'NAME'][0]['Value']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.