问题描述
我一般不熟悉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 (将#修改为@)