Skip to content

Commit

Permalink
feat(cloudwatch): validate Dashboards with an end time must also ha…
Browse files Browse the repository at this point in the history
…ve 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*
  • Loading branch information
go-to-k committed Sep 20, 2023
1 parent af998c8 commit 097bd0f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/aws-cdk-lib/aws-cloudwatch/lib/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ export class Dashboard extends Resource {
}

if (props.start !== undefined && props.defaultInterval !== undefined) {
throw ('both properties defaultInterval and start cannot be set at once');
throw new Error('both properties defaultInterval and start cannot be set at once');
}

if (props.end !== undefined && props.start === undefined) {
throw new Error('If you specify a value for end, you must also specify a value for start.');
}

const dashboard = new CfnDashboard(this, 'Resource', {
Expand Down
29 changes: 29 additions & 0 deletions packages/aws-cdk-lib/aws-cloudwatch/test/dashboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,35 @@ describe('Dashboard', () => {

});

test('throws if both defaultInterval and start are specified', () => {
// GIVEN
const stack = new Stack();
// WHEN
const toThrow = () => {
new Dashboard(stack, 'Dash', {
start: '-P7D',
defaultInterval: Duration.days(7),
});
};

// THEN
expect(() => toThrow()).toThrow(/both properties defaultInterval and start cannot be set at once/);
});

test('throws if end is specified but start is not', () => {
// GIVEN
const stack = new Stack();
// WHEN
const toThrow = () => {
new Dashboard(stack, 'Dash', {
end: '2018-12-17T06:00:00.000Z',
});
};

// THEN
expect(() => toThrow()).toThrow(/If you specify a value for end, you must also specify a value for start./);
});

test('DashboardName is set when provided', () => {
// GIVEN
const app = new App();
Expand Down

0 comments on commit 097bd0f

Please sign in to comment.