Skip to content

Commit 6c73d8a

Browse files
gcacacerix0rrr
authored andcommitted
fix(aws-cloudwatch): remove workaround on optional DashboardName
There was a bug in CloudFormation which caused updates to `AWS::CloudWatch::Dashboard` to fail if `DashboardName` was not supplied. Now that the bug has been fixed we can revert the workaround in CDK which generates a name if the user doesn't supply one. Fixes #213.
1 parent 9c2220c commit 6c73d8a

File tree

5 files changed

+23
-29
lines changed

5 files changed

+23
-29
lines changed

packages/@aws-cdk/aws-cloudwatch/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,6 @@ The following widgets are available:
103103
- `SingleValueWidget` -- shows the current value of a set of metrics.
104104
- `TextWidget` -- shows some static Markdown.
105105

106-
> Warning! Due to a bug in CloudFormation, you cannot update a Dashboard after
107-
> initially creating it if you let its name automatically be generated. You
108-
> must set `dashboardName` if you intend to update the dashboard after creation.
109-
>
110-
> (This note will be removed once the bug is fixed).
111-
112106
### Graph widget
113107

114108
A graph widget can display any number of metrics on either the `left` or

packages/@aws-cdk/aws-cloudwatch/lib/dashboard.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,12 @@ export interface DashboardProps {
5252
*/
5353
export class Dashboard extends Resource {
5454
private readonly rows: IWidget[] = [];
55-
private readonly dashboard: CfnDashboard;
5655

5756
constructor(scope: Construct, id: string, props?: DashboardProps) {
5857
super(scope, id);
5958

60-
// WORKAROUND -- Dashboard cannot be updated if the DashboardName is missing.
61-
// This is a bug in CloudFormation, but we don't want CDK users to have a bad
62-
// experience. We'll generate a name here if you did not supply one.
63-
// See: https://github.com/awslabs/aws-cdk/issues/213
64-
const dashboardName = (props && props.dashboardName) || new Token(() => this.generateDashboardName()).toString();
65-
66-
this.dashboard = new CfnDashboard(this, 'Resource', {
67-
dashboardName,
59+
new CfnDashboard(this, 'Resource', {
60+
dashboardName: (props && props.dashboardName) || undefined,
6861
dashboardBody: new Token(() => {
6962
const column = new Column(...this.rows);
7063
column.position(0, 0);
@@ -95,12 +88,4 @@ export class Dashboard extends Resource {
9588
const w = widgets.length > 1 ? new Row(...widgets) : widgets[0];
9689
this.rows.push(w);
9790
}
98-
99-
/**
100-
* Generate a unique dashboard name in case the user didn't supply one
101-
*/
102-
private generateDashboardName(): string {
103-
// Combination of stack name and LogicalID, which are guaranteed to be unique.
104-
return this.node.stack.name + '-' + this.dashboard.logicalId;
105-
}
10691
}

packages/@aws-cdk/aws-cloudwatch/test/integ.alarm-and-dashboard.expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
]
7272
]
7373
},
74-
"DashboardName": "aws-cdk-cloudwatch-DashCCD7F836"
74+
"DashboardName": "MyCustomDashboardName"
7575
}
7676
}
7777
}

packages/@aws-cdk/aws-cloudwatch/test/integ.alarm-and-dashboard.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const alarm = metric.newAlarm(stack, 'Alarm', {
2727
});
2828

2929
const dashboard = new cloudwatch.Dashboard(stack, 'Dash', {
30+
dashboardName: 'MyCustomDashboardName',
3031
start: '-9H',
3132
end: '2018-12-17T06:00:00.000Z',
3233
periodOverride: PeriodOverride.Inherit

packages/@aws-cdk/aws-cloudwatch/test/test.dashboard.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,35 @@ export = {
120120
test.done();
121121
},
122122

123-
'work around CloudFormation bug'(test: Test) {
124-
// See: https://github.com/awslabs/aws-cdk/issues/213
125-
123+
'DashboardName is set when provided'(test: Test) {
126124
// GIVEN
127125
const app = new App();
128126
const stack = new Stack(app, 'MyStack');
129127

130128
// WHEN
131-
new Dashboard(stack, 'MyDashboard');
129+
new Dashboard(stack, 'MyDashboard', {
130+
dashboardName: 'MyCustomDashboardName'
131+
});
132132

133133
// THEN
134134
expect(stack).to(haveResource('AWS::CloudWatch::Dashboard', {
135-
DashboardName: 'MyStack-MyDashboardCD351363'
135+
DashboardName: 'MyCustomDashboardName'
136136
}));
137137

138+
test.done();
139+
},
140+
141+
'DashboardName is not generated if not provided'(test: Test) {
142+
// GIVEN
143+
const app = new App();
144+
const stack = new Stack(app, 'MyStack');
145+
146+
// WHEN
147+
new Dashboard(stack, 'MyDashboard');
148+
149+
// THEN
150+
expect(stack).to(haveResource('AWS::CloudWatch::Dashboard', {}));
151+
138152
test.done();
139153
}
140154
};

0 commit comments

Comments
 (0)