如何使用 boto3 lib 在 lambda 函数中获取所有区域中的所有 aws 资源
How to fetch all aws resources in all regions in lambda function, with boto3 lib
我正在尝试使用 boto3 lib 记录我在所有区域(有多个帐户)的所有 aws 资源。
我发现 aws config 很有用。
我已经创建了聚合器
ConfigurationAggregator:
Type: 'AWS::Config::ConfigurationAggregator'
Properties:
AccountAggregationSources:
- AccountIds: !Ref AccountIds
AllAwsRegions: !Ref AllAwsRegions
ConfigurationAggregatorName: MyAggregator
我浏览了 aws config 的 boto3 lib 文档
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/config.html#ConfigService.Client.batch_get_aggregate_resource_config
但它需要各种必需的参数,如 resourceid 、区域帐户 id、资源类型。
这是最简单的 boto3 API,除了聚合器名称,我不需要传递任何东西,在 return 中,我得到了所有种类的 aws 资源的列表,在所有地区。
我不担心资源是否合规,我只想一次性记录每一个资源。
我认为 select_aggregate_resource_config 是您所需要的。查询示例可以是:SELECT resourceId, resourceName, resourceType
。在代码中尝试之前,您可以在 AWS Web 控制台中使用高级查询。
解决方案是创建多账户/多区域aggregator
并在下面使用该聚合器名称 aggregation 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
我正在尝试使用 boto3 lib 记录我在所有区域(有多个帐户)的所有 aws 资源。
我发现 aws config 很有用。
我已经创建了聚合器
ConfigurationAggregator:
Type: 'AWS::Config::ConfigurationAggregator'
Properties:
AccountAggregationSources:
- AccountIds: !Ref AccountIds
AllAwsRegions: !Ref AllAwsRegions
ConfigurationAggregatorName: MyAggregator
我浏览了 aws config 的 boto3 lib 文档 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/config.html#ConfigService.Client.batch_get_aggregate_resource_config
但它需要各种必需的参数,如 resourceid 、区域帐户 id、资源类型。
这是最简单的 boto3 API,除了聚合器名称,我不需要传递任何东西,在 return 中,我得到了所有种类的 aws 资源的列表,在所有地区。
我不担心资源是否合规,我只想一次性记录每一个资源。
我认为 select_aggregate_resource_config 是您所需要的。查询示例可以是:SELECT resourceId, resourceName, resourceType
。在代码中尝试之前,您可以在 AWS Web 控制台中使用高级查询。
解决方案是创建多账户/多区域aggregator 并在下面使用该聚合器名称 aggregation 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