sharath.g
sharath.g

Reputation: 311

How to add Query Results Widget to Cloudwatch dashboard using aws-cdk

As the title says, how do I do it? I want to transform my Cloudwatch dashobard (which mostly contains query results widget) into cdk. The aws-cloudwatch library currently only has AlarmWidget, GraphWidget, SingleValueWidget and TextWidget.

If there's no straight forward way to do it, is there at least some kind of a hack?

Thanks

Upvotes: 3

Views: 3486

Answers (2)

Aleksi
Aleksi

Reputation: 5026

LogQueryWidget was added in aws-cdk v1.37.0 (see release notes).

Upvotes: 1

Dejan Peretin
Dejan Peretin

Reputation: 12089

If that widget type is not supported you could use the CfnDashboard construct instead of the Dashboard. With that construct, you can set the raw dashboard body as json.

If you prefer the higher level approach of adding widgets, you could implement the IWidget interface yourself. You didn't specify which language you're using, here's an example in python:

from aws_cdk import (
    aws_cloudwatch as cw,
    core
)
import jsii
import json

@jsii.implements(cw.IWidget)
class QueryResultWidget:
    def __init__(self, properties, width=24, height=6):
        self.widget =  {
            "type": "log",
            "width": width,
            "height": height,
            "properties": properties
        }

    def position(self, x, y):
        self.widget["x"] = x
        self.widget["y"] = y

    def to_json(self):
        return [self.widget]

class DashboardStack(core.Stack):
    def __init__(self, app: core.App, id: str, **kwargs) -> None:
        super().__init__(app, id, **kwargs)

        dashboard = cw.Dashboard(self, "Dashboard")

        dashboard.add_widgets(QueryResultWidget({
            "query": "SOURCE '/aws/lambda/some_lambda' | fields @timestamp, @message\n| sort @timestamp desc\n| limit 20",
            "region": "eu-west-1",
            "stacked": False,
            "view": "table"
        }))

app = core.App()
DashboardStack(app, "DashboardStack")
app.synth()

Upvotes: 0

Related Questions