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

Amazon web services 如何使用aws cdk将查询结果小部件添加到Cloudwatch仪表板,amazon-web-services,amazon-cloudwatch,aws-cdk,aws-cloudwatch-log-insights,Amazon Web Services,Amazon Cloudwatch,Aws Cdk,Aws Cloudwatch Log Insights,正如标题所说,我该怎么做?我想将我的Cloudwatch dashobard(主要包含查询结果小部件)转换为cdk。aws cloudwatch库目前只有AlarmWidget、GraphWidget、SingleValueWidget和TextWidget 如果没有直截了当的方法,至少有某种黑客吗 谢谢如果该小部件类型不受支持,您可以使用构造而不是仪表板。使用该构造,您可以将原始仪表板主体设置为json 如果您更喜欢添加小部件的更高级别方法,您可以自己实现该接口。您没有指定使用哪种语言,下面是

正如标题所说,我该怎么做?我想将我的Cloudwatch dashobard(主要包含查询结果小部件)转换为cdk。aws cloudwatch库目前只有AlarmWidget、GraphWidget、SingleValueWidget和TextWidget

如果没有直截了当的方法,至少有某种黑客吗


谢谢

如果该小部件类型不受支持,您可以使用构造而不是仪表板。使用该构造,您可以将原始仪表板主体设置为json

如果您更喜欢添加小部件的更高级别方法,您可以自己实现该接口。您没有指定使用哪种语言,下面是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()
已添加到aws cdk v1.37.0中(请参阅)