如何获取所有日志组名称

how to get all log groups names

我有一个 lambda 可以将我们所有的日志组导出到 s3,目前我正在使用 cloudwatchlogs.describeLogGroups 列出 我们所有的日志组。

const logGroupsResponse = await cloudwatchlogs.describeLogGroups({ limit: 50 })

问题是我们有 69 个日志组,有什么方法可以列出 aws 帐户中绝对所有日志组的(ID 和名称)。我看到可以有 1000 个日志组。这是我们控制台的截图:

为什么 cloudwatchlogs.describeLogGroups 只允许限制 50 个,这太小了?

假设您使用的是 AWS JS SDK v2,describeLogGroups API 在其响应中提供 nextToken 并接受 nexToken。此令牌用于通过发送多个请求来检索多个日志组(超过 50 个)。我们可以使用以下模式来完成此操作:

const cloudwatchlogs = new AWS.CloudWatchLogs({region: 'us-east-1'});
let nextToken = null;
do {
    const logGroupsResponse = await cloudwatchlogs.describeLogGroups({limit: 50, nextToken: nextToken}).promise();
    
    // Do something with the retrieved log groups
    console.log(logGroupsResponse.logGroups.map(group => group.arn));

    // Get the next token. If there are no more log groups, the token will be undefined
    nextToken = logGroupsResponse.nextToken;
} while (nextToken);

我们正在循环查询 AWS API,直到没有更多日志组为止。