Skip to content

Commit 01393c4

Browse files
authored
chore(cloudfront): warning on empty domainNames to alert a manual action (#31367)
### Reason for this change Follow-up to #31001. The user need to run `associate-alias` cli command manually when moving an alternate domain name. Adding a warning will help users not to forget. ### Description of changes Add a warning annotation when `domainNames` is empty. ### Description of how you validated changes Added an expectation to validate the warning. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1132887 commit 01393c4

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Diff for: packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as cloudwatch from '../../aws-cloudwatch';
1515
import * as iam from '../../aws-iam';
1616
import * as lambda from '../../aws-lambda';
1717
import * as s3 from '../../aws-s3';
18-
import { ArnFormat, IResource, Lazy, Resource, Stack, Token, Duration, Names, FeatureFlags } from '../../core';
18+
import { ArnFormat, IResource, Lazy, Resource, Stack, Token, Duration, Names, FeatureFlags, Annotations } from '../../core';
1919
import { CLOUDFRONT_DEFAULT_SECURITY_POLICY_TLS_V1_2_2021 } from '../../cx-api';
2020

2121
/**
@@ -321,6 +321,10 @@ export class Distribution extends Resource implements IDistribution {
321321
if (!Token.isUnresolved(certificateRegion) && certificateRegion !== 'us-east-1') {
322322
throw new Error(`Distribution certificates must be in the us-east-1 region and the certificate you provided is in ${certificateRegion}.`);
323323
}
324+
325+
if ((props.domainNames ?? []).length === 0) {
326+
Annotations.of(this).addWarningV2('@aws-cdk/aws-cloudfront:emptyDomainNames', 'No domain names are specified. You will need to specify it after running associate-alias CLI command manually. See the "Moving an alternate domain name to a different distribution" section of module\'s README for more info.');
327+
}
324328
}
325329

326330
const originId = this.addOrigin(props.defaultBehavior.origin);

Diff for: packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defaultOrigin, defaultOriginGroup } from './test-origin';
2-
import { Match, Template } from '../../assertions';
2+
import { Annotations, Match, Template } from '../../assertions';
33
import * as acm from '../../aws-certificatemanager';
44
import * as cloudwatch from '../../aws-cloudwatch';
55
import * as iam from '../../aws-iam';
@@ -473,6 +473,7 @@ describe('certificates', () => {
473473
},
474474
},
475475
});
476+
Annotations.fromStack(stack).hasWarning('/Stack/Dist1', 'No domain names are specified. You will need to specify it after running associate-alias CLI command manually. See the "Moving an alternate domain name to a different distribution" section of module\'s README for more info. [ack: @aws-cdk/aws-cloudfront:emptyDomainNames]');
476477
});
477478

478479
test('use the TLSv1.2_2021 security policy by default', () => {

0 commit comments

Comments
 (0)