簡體   English   中英

如何使用 boto3 查找來自所有區域的所有 CloudTrail 事件?

[英]How can I look up all CloudTrail events from all regions using boto3?

我有一個關於如何使用 boto3 從所有區域獲取所有 CloudTrail 事件的快速問題。

當我運行以下腳本時,它只列出來自 CloudTrail 主區域(這意味着創建 CloudTrail 的區域)的實例。


response = trail.lookup_events(
    LookupAttributes = [
        {
            'AttributeKey': 'EventName',
            'AttributeValue': 'RunInstances'
        }
    ],
    StartTime = datetime(2021,8,21),
    EndTime = datetime(2021,8,24),
)

有什么方法可以獲取所有區域的所有 CloudTrail 事件?

提前感謝您的幫助!

僅供參考,我的 CloudTrail 的多區域選項已啟用。

使用 boto3 配置更改區域將起作用。

我使用下面的regions_work_with_service function 作為獲取所有區域的片段。 然后循環使用區域來做某事。

下面, client = boto3.client("cloudtrail", config=my_config)設置一個區域,客戶端運行lookup_events並打印事件(如果有)。

import boto3
from botocore.config import Config


def regions_work_with_service(service):
    regions = []
    client = boto3.client(service)
    response = client.describe_regions()

    for item in response["Regions"]:
        regions.append(item["RegionName"])

    return regions


regions = regions_work_with_service("ec2")

for region in regions:

    print(f"region: {region}")

    my_config = Config(region_name=region)
    client = boto3.client("cloudtrail", config=my_config)

    response = client.lookup_events(
        LookupAttributes=[
            {"AttributeKey": "EventName", "AttributeValue": "RunInstances"}
        ]
    )

    if response["Events"]:
        for i in range(len(response["Events"])):
            event = json.loads(response["Events"][i]["CloudTrailEvent"])
            print(event)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM