简体   繁体   English

如何使用 aws-cdk 将查询结果小部件添加到 Cloudwatch 仪表板

[英]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.我想将我的 Cloudwatch dashobard(主要包含查询结果小部件)转换为 cdk。 The aws-cloudwatch library currently only has AlarmWidget, GraphWidget, SingleValueWidget and TextWidget. aws-cloudwatch 库目前只有 AlarmWidget、GraphWidget、SingleValueWidget 和 TextWidget。

If there's no straight forward way to do it, is there at least some kind of a hack?如果没有直接的方法来做到这一点,至少有某种黑客吗?

Thanks谢谢

If that widget type is not supported you could use the CfnDashboard construct instead of the Dashboard.如果不支持该小部件类型,您可以使用CfnDashboard构造而不是 Dashboard。 With that construct, you can set the raw dashboard body as json.使用该构造,您可以将原始仪表板主体设置为 json。

If you prefer the higher level approach of adding widgets, you could implement the IWidget interface yourself.如果您更喜欢添加小部件的高级方法,您可以自己实现IWidget接口。 You didn't specify which language you're using, here's an example in python:您没有指定您使用的是哪种语言,以下是 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()

LogQueryWidget已添加到 aws-cdk v1.37.0(请参阅发行说明)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM