[cloudwatch] position of the widget in a dashboard cannot be changed
Description of the bug
The position of the widget in the cloudwatch dashboard cannot be changed using the method described in the documentation.
Reproduction Steps
initApiErrorWidgget(apiGateways: ApiGatewayConfig[]): cloudwatch.IWidget {
let let apiErrorWidget = new cloudwatch.GraphWidget({
// implem details
})
return apiErrorWidget;
}
constructor(scope: cdk.Construct, id: string, props: GlobalDashboardConfig) {
super(scope, id);
let globalDashboard = new cloudwatch.Dashboard(this, "globalDashboard", {
dashboardName: props.name,
})
let apiErrorWidget = this.initApiErrorWidgget(props.apiGateways)
globalDashboard.addWidgets(apiErrorWidget)
apiErrorWidget.position(9, 0)
}
Environment
- **CLI Version :1.50.0
- **Framework Version:1.50.0
- **Node.js Version:v12.18.1
- **OS :Darwin
- **Language (Version):TypeScript (3.9.6)
This is :bug: Bug Report
I confirm this behaviour. 'position' seems to have no effect.
- implement the
positionfunction
// create a wrapper with no position and let it auto fit
class WrappedWidgetWithNoPosition {
private child: IWidget;
constructor(child: IWidget) {
this.child = child;
}
get width() {
return this.child.width;
}
get height() {
return this.child.height;
}
toJson(){
return this.child.toJson();
}
position(x: number, y: number) {
}
}
- use
WrappedWidgetWithNoPositionto wrap ofGraphWidget
dashboard.addWidgets(new WrappedWidgetWithNoPosition(widget))
- change back to
GraphWidgetif this get fixed
I was trying to create LogQueryWidget where I am trying to set the "position(x, y)" by passing the x and y values dynamically its not modifying the position parameters. eg. Valid case : where i am able to create the widget but however the Position parameters i.e. x and y are automatically set by cdk.
widget= cloudwatch.LogQueryWidget(log_group_names=[xyz],
height=6,
width=6,
query_lines=query.split('|'),
title=xyz_test,
view=cloudwatch.LogQueryVisualizationType.TABLE,
region=ap-south-1
)
Invalid Case: If i try to set the position parameter it's create the cloudformation template. But the position parameters are not modified as per the values provide by us.
eg.
widget=cloudwatch.LogQueryWidget(log_group_names=[xyz],
height=6,
width=6,
query_lines=query.split('|'),
title=xyz_test,
view=cloudwatch.LogQueryVisualizationType.TABLE,
region=ap-south-1
)
widget.position(0,6)
Please have a look into this.
Instead of hard-coding position using the position method inherited from the ConcreteWidget class a workaround and probably easier way to position widgets is to use Columns and Rows which can be used to set the widgets matrix in a dashboard.
Example usage -
const dashboard = new Dashboard(this, "dashboard", {
dashboardName: "foo",
widgets: [
[
new Column(widget1),
new Column(widget2, widget3, widget4)
new Column(widget5, widget6, widget7)
],
[
widget8
],
[
widget9,
widget10
]
]
});
case confirmed here as well.
case confirmed still not fixed.
still not working
We've identified where the problem is.
The L2 construct is building a CfnDashboard object. In Cloudformation docs, widgets in the dashboard are built with a string. The CDK is building that string on line 101:
https://github.com/aws/aws-cdk/blob/6179ac16f2fcab5bf5e03a3629db281018d7585b/packages/%40aws-cdk/aws-cloudwatch/lib/dashboard.ts#L95-L109
Widgets are imported into that dashboard via rows:
https://github.com/aws/aws-cdk/blob/6179ac16f2fcab5bf5e03a3629db281018d7585b/packages/%40aws-cdk/aws-cloudwatch/lib/dashboard.ts#L125-L132
These widgets are turned into strings via the Column object as seen on line 99 of the first snippet. However, column position is adjusted in the following line.
And as you can see here:
https://github.com/aws/aws-cdk/blob/6179ac16f2fcab5bf5e03a3629db281018d7585b/packages/%40aws-cdk/aws-cloudwatch/lib/layout.ts#L83-L89
When column position is adjusted, it adjusts the position of individual widgets as well, thereby always setting them to 0.
We are investigating a possible fix
The intended way to position widgets is as @jain98 said above. This:
const widget1 = new AlarmWidget({ alarm: alarm, width: 16, height: 9 })
const widget2 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget3 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget4 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget5 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget6 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget7 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 })
const widget8 = new AlarmWidget({ alarm: alarm, width: 24, height: 4 })
const widget9 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 })
const widget10 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 })
new cloudwatch.Dashboard(this, "dashboard", {
dashboardName: "foo",
widgets: [
[
new Column(widget1),
new Column(widget2, widget3, widget4),
new Column(widget5, widget6, widget7)
],
[
widget8
],
[
widget9,
widget10
]
]
})
will look like this:
Each array of widgets is a new Row. Let the autofilling work for you 😄
The documentation having this is misleading, as the user is not supposed to be able to set the position manually.
const wd1 = new GraphWidget({ "width": 12,"height": 5, title: 'lamba 1', left: [new Metric({ metricName: 'Error', namespace: 'AWS/Lambda', dimensions: {'lambda_name': 'lambda_name'}, statistic: 'avg', }) ]})
dashboard.addWidgets(wd1)
How should I add position as X and Y ?
This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
Still an issue. What is the .position() command used for anyway?
still seems to be an issue.
example below.
invocation_graph_widget = aws_cloudwatch.GraphWidget(
title=f"Invocations - 1 hour",
height=6,
width=8,
left=Invocations_metric_graph,
)
errors_widget.position(x=2,y=0)
The intended way to position widgets is as @Jain98 said above. This:
const widget1 = new AlarmWidget({ alarm: alarm, width: 16, height: 9 }) const widget2 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 }) const widget3 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 }) const widget4 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 }) const widget5 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 }) const widget6 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 }) const widget7 = new AlarmWidget({ alarm: alarm, width: 4, height: 3 }) const widget8 = new AlarmWidget({ alarm: alarm, width: 24, height: 4 }) const widget9 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 }) const widget10 = new AlarmWidget({ alarm: alarm, width: 12, height: 4 }) new cloudwatch.Dashboard(this, "dashboard", { dashboardName: "foo", widgets: [ [ new Column(widget1), new Column(widget2, widget3, widget4), new Column(widget5, widget6, widget7) ], [ widget8 ], [ widget9, widget10 ] ] })will look like this:
Each array of widgets is a new
Row. Let the autofilling work for you 😄The documentation having this is misleading, as the user is not supposed to be able to set the position manually.
Thanks @peterwoodworth ! I was having a hard time positioning widgets with different heights, but this made it super simple.