diff --git a/packages/aws-cdk-lib/aws-cloudwatch/lib/dashboard.ts b/packages/aws-cdk-lib/aws-cloudwatch/lib/dashboard.ts index bf11d4b055d55..632580d6be9df 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch/lib/dashboard.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch/lib/dashboard.ts @@ -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', { diff --git a/packages/aws-cdk-lib/aws-cloudwatch/test/dashboard.test.ts b/packages/aws-cdk-lib/aws-cloudwatch/test/dashboard.test.ts index 9c278ea600b37..e2798347b1c96 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch/test/dashboard.test.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch/test/dashboard.test.ts @@ -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();