Search code examples
amazon-web-servicesamazon-cloudwatchaws-cdkamazon-cloudwatchlogsaws-cloudwatch-log-insights

List all LogGroups using cdk


I am quite new to the CDK, but I'm adding a LogQueryWidget to my CloudWatch Dashboard through the CDK, and I need a way to add all LogGroups ending with a suffix to the query.

Is there a way to either loop through all existing LogGroups and finding the ones with the correct suffix, or a way to search through LogGroups.

const queryWidget = new LogQueryWidget({
    title: "Error Rate",
    logGroupNames: ['/aws/lambda/someLogGroup'],
    view: LogQueryVisualizationType.TABLE,
    queryLines: [
        'fields @message',
        'filter @message like /(?i)error/'
    ],
  })

Is there anyway I can add it so logGroupNames contains all LogGroups that end with a specific suffix?


Solution

  • You cannot do that dynamically (i.e. you can't make this work such that if you add a new LogGroup, the query automatically adjusts), without using something like AWS lambda that periodically updates your Log Query.

    However, because CDK is just a code, there is nothing stopping you from making an AWS SDK API call inside the code to retrieve all the log groups (See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchLogs.html#describeLogGroups-property) and then populate logGroupNames accordingly.

    That way, when CDK compiles, it will make an API call to fetch LogGroups and then generated CloudFormation will contain the log groups you need. Note that this list will only be updated when you re-synthesize and re-deploy your stack.

    Finally, note that there is a limit on how many Log Groups you can query with Log Insights (20 according to https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html).