1

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?

khave
  • 23
  • 2
  • 6
  • `LogGroup.fromLogGroupName()` will allow you to fetch a log group by its exact name, although not exactly what you are looking for. – gshpychka Apr 22 '21 at 13:07

2 Answers2

1

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).

Tofig Hasanov
  • 3,303
  • 10
  • 51
  • 81
1

If you want to achieve this, you can create a custom resource using AwsCustomResource and AwsSdkCall classes to do the AWS SDK API call (as mentioned by @Tofig above) as part of the deployment. You can read data from the API call response as well and act on it as you want.