[英]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.