Boto3 Cloudtrail不返回资源的任何事件

2024-10-01 07:41:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过boto3从cloudtrail获取实例id的事件,如(runinstances、createtags和terminateinstances),下面是我的代码

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, 03 Apr 2020 03:53:57 GMT', 'content-length': '13', 'content-type': 'application/x-amz-json-1.1'}}}


Tags: 实例instanceidresponse事件contentboto3print
1条回答
网友
1楼 · 发布于 2024-10-01 07:41:32

似乎删除endtime对我有效,但不知道为什么

我现在可以得到想要的活动了

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)

相关问题 更多 >