【问题标题】:How can I look up all CloudTrail events from all regions using boto3?如何使用 boto3 查找来自所有区域的所有 CloudTrail 事件?
【发布时间】:2021-08-23 07:01:36
【问题描述】:

我有一个关于如何使用 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 启用了 MultiRegion 选项。

【问题讨论】:

    标签: amazon-web-services boto3 amazon-cloudtrail


    【解决方案1】:

    使用 boto3 配置更改区域将起作用。

    我使用下面的regions_work_with_service 函数作为获取所有区域的sn-p。然后循环与区域做某事。

    下面,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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多