我正在尝试通过boto3从cloudtrail获取实例ID的事件(运行实例,创建标签和终止实例),下面是我的代码
import boto3
cloudtrail = boto3.client('cloudtrail')
instance_id = 'i-0002e660b987688'
starttime = '2020-04-01'
endtime = '2020-04-03'
try:
response = cloudtrail.lookup_events(
LookupAttributes=[
{
'AttributeKey': 'ResourceName',
'AttributeValue': instance_id
},
],
StartTime=starttime,
EndTime=endtime,
MaxResults=50
)
except Exception as e:
print(e)
raise(e)
print(response)
我没有收到任何事件,从AWS控制台中,我可以看到上述指定时间段内的所有那些事件。
{u'Events':[],'ResponseMetadata':{'RetryAttempts':0,'HTTPStatusCode':200,'RequestId':'658e4873-b0d1-453c','HTTPHeaders':{'x-amzn-requestid':'658e4873-b0d1-453','date':'Fri,2020年4月3日03:53:57 GMT','content-length':'13','content-type':'application / x-amz-json-1.1'}}}] >>
<< [
似乎删除结束时间对我有用,但不确定为什么。
我现在可以得到想要的事件。
try:
response = cloudtrail.lookup_events(
LookupAttributes=[
{
'AttributeKey': 'ResourceName',
'AttributeValue': instance_id
},
],
StartTime=starttime,
#EndTime=endtime,
MaxResults=50
)
except Exception as e:
print(e)
raise(e)
print(response)
try:
response = cloudtrail.lookup_events(
LookupAttributes=[
{
'AttributeKey': 'ResourceName',
'AttributeValue': instance_id
},
],
StartTime=starttime,
#EndTime=endtime,
MaxResults=50
)
except Exception as e:
print(e)
raise(e)
print(response)