如何解决Boto3脚本,用于查找仅具有最后值的未附加EBS卷更新字典
我一般不熟悉python和编码,并且正尝试编写一个boto3脚本,该脚本将使用cloudtrail事件来告知何时从实例取消附加EBS卷的时间戳。我可以使用它来正确地使用资源类型和资源名称更新字典,但是它使用的是timestamp字段中的最后一个值,而不是属于每个卷的相应时间戳。
现在输出类似于:
{'resourcetype': ['AWS::EC2::Volume'],'resourcename': ['vol-0f8xxxxxxxxxxx'],'timestamp': 'Tue Jul 28 13:47:33 2020'}
{'resourcetype': ['AWS::EC2::Volume'],'resourcename': ['vol-0a9xxxxxxxxxxx'],'timestamp': 'Tue Jul 28 13:47:33 2020'}
这是正确的,除了这两个卷应具有不同的时间戳记。显然,我搞砸了一些事情,即将两个卷的最后一个值添加到字典中。
这是脚本:
#!/usr/bin/env python3
import boto3
from botocore.exceptions import ClientError
import datetime
import json
import csv
import sys
ec2client = boto3.client('ec2')
cloudtrail = boto3.client('cloudtrail')
#volume_ids = []
try:
response = cloudtrail.lookup_events(LookupAttributes=[
{
'AttributeKey': 'EventName','AttributeValue': 'DetachVolume'
},],)
#print(response)
except ClientError as e:
print(e)
keys = {}
format = "%a %b %d %H:%M:%S %Y"
for r in response['Events']:
for i in r['Resources']:
resourcetype = (i['ResourceType'])
resourcename = (i['ResourceName'])
if resourcetype == 'AWS::EC2::Volume':
keys.update({'resourcetype': [i['ResourceType']]})
#print(resourcetype)
if resourcetype == 'AWS::EC2::Volume':
keys.update({'resourcename': [i['ResourceName']]})
#print(resourcename)
#print(keys)
for t in response['Events']:
time = (t['EventTime'])
#print(time)
format_time = ('{:%a %b %d %H:%M:%S %Y}'.format(time))
keys.update({'timestamp': format_time})
print(keys)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。