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

问题描述 投票:1回答:1

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

如果没有直接的方法,是否至少有某种黑客手段?

谢谢

amazon-web-services amazon-cloudwatch aws-cdk aws-cloudwatch-log-insights
1个回答
0
投票
如果您更喜欢添加小部件的高级方法,则可以自己实现IWidget接口。您未指定要使用的语言,这是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()

热门问题
推荐问题
最新问题