Skip to content

Commit 097bd0f

Browse files
authored
feat(cloudwatch): validate Dashboards with an end time must also have a start time (#27124)
This PR adds a validation for a `start` and an `end` in `Dashboard`. It throws an error if you specify an `end` without a `start`. While it is possible to deploy a dashboard with only an end time in CloudFormation, the setting will be ignored and the dashboard displayed with the default time period. With this validation, we are stopping the faulty deployment early. API Reference contains the following description and the user will not be aware of this situation. If you specify a value for end, you must also specify a value for start. https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Dashboard-Body-Structure.html And each Widget in graph.ts also validates the same case. https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-cloudwatch/lib/graph.ts#L245-L247 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent af998c8 commit 097bd0f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ export class Dashboard extends Resource {
133133
}
134134

135135
if (props.start !== undefined && props.defaultInterval !== undefined) {
136-
throw ('both properties defaultInterval and start cannot be set at once');
136+
throw new Error('both properties defaultInterval and start cannot be set at once');
137+
}
138+
139+
if (props.end !== undefined && props.start === undefined) {
140+
throw new Error('If you specify a value for end, you must also specify a value for start.');
137141
}
138142

139143
const dashboard = new CfnDashboard(this, 'Resource', {

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

+29
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,35 @@ describe('Dashboard', () => {
161161

162162
});
163163

164+
test('throws if both defaultInterval and start are specified', () => {
165+
// GIVEN
166+
const stack = new Stack();
167+
// WHEN
168+
const toThrow = () => {
169+
new Dashboard(stack, 'Dash', {
170+
start: '-P7D',
171+
defaultInterval: Duration.days(7),
172+
});
173+
};
174+
175+
// THEN
176+
expect(() => toThrow()).toThrow(/both properties defaultInterval and start cannot be set at once/);
177+
});
178+
179+
test('throws if end is specified but start is not', () => {
180+
// GIVEN
181+
const stack = new Stack();
182+
// WHEN
183+
const toThrow = () => {
184+
new Dashboard(stack, 'Dash', {
185+
end: '2018-12-17T06:00:00.000Z',
186+
});
187+
};
188+
189+
// THEN
190+
expect(() => toThrow()).toThrow(/If you specify a value for end, you must also specify a value for start./);
191+
});
192+
164193
test('DashboardName is set when provided', () => {
165194
// GIVEN
166195
const app = new App();

0 commit comments

Comments
 (0)