简体   繁体   English

如何使用boto3 lib获取lambda function中所有区域的所有aws资源

[英]How to fetch all aws resources in all regions in lambda function, with boto3 lib

I am tring to log all my aws resources in all regions, (with multiple accounts) using boto3 lib.我正在尝试使用 boto3 lib 记录所有区域中的所有 aws 资源(具有多个帐户)。

I found that aws config is helpful.我发现 aws config 很有帮助。

I have already created aggregator我已经创建了聚合器

  ConfigurationAggregator:
    Type: 'AWS::Config::ConfigurationAggregator'
    Properties:
      AccountAggregationSources:
        - AccountIds: !Ref AccountIds
          AllAwsRegions: !Ref AllAwsRegions
      ConfigurationAggregatorName: MyAggregator

And i went through boto3 lib docs for aws config https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/config.html#ConfigService.Client.batch_get_aggregate_resource_config我浏览了 aws config https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/config.html#ConfigService.Client.batch_get_aggregate_resource_config的 boto3 lib 文档

But it requires various REQUIRED parameters like resourceid, region account id, resource type.但它需要各种 REQUIRED 参数,如资源 ID、区域帐户 ID、资源类型。

Which is the simplest boto3 API where i don't have to pass anything except Aggregator name, and in return i get list of all and everykind kind of aws resources, in all the regions.这是最简单的boto3 API,除了聚合器名称,我不需要传递任何东西,作为回报,我得到所有地区所有种类的aws资源的列表。

I am not worried about whether resource is complianced or not, i just want to log each and every resource in one go.我不担心资源是否合规,我只想在一个 go 中记录每个资源。

I think select_aggregate_resource_config is what you need.我认为select_aggregate_resource_config是您所需要的。 Example of query can be: SELECT resourceId, resourceName, resourceType .查询示例可以是: SELECT resourceId, resourceName, resourceType You can play with advanced queries in AWS Web Console before you try it in code.在代码中尝试之前,您可以在 AWS Web 控制台中使用高级查询。

Solution was to create an multi acc / multi region aggregator And use that aggregator name in below aggregation function解决方案是创建一个多 acc / 多区域聚合器并在下面的聚合 function中使用该聚合器名称

            nextToken = ""
            res = []
            while (nextToken != None):
                data = client.list_aggregate_discovered_resources(ConfigurationAggregatorName=AWS_AGG_NAME, ResourceType=tp, Limit=AGG_LIMIT, NextToken=nextToken)
                do_your_logic_with_resource(rc)
                res = res + data['ResourceIdentifiers']
                nextToken = data['NextToken'] if 'NextToken' in data else None
            return res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM