Skip to content

Commit

Permalink
chore(cloudfront): warning on empty domainNames to alert a manual a…
Browse files Browse the repository at this point in the history
…ction (#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*
  • Loading branch information
Tietew authored Sep 9, 2024
1 parent 1132887 commit 01393c4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as cloudwatch from '../../aws-cloudwatch';
import * as iam from '../../aws-iam';
import * as lambda from '../../aws-lambda';
import * as s3 from '../../aws-s3';
import { ArnFormat, IResource, Lazy, Resource, Stack, Token, Duration, Names, FeatureFlags } from '../../core';
import { ArnFormat, IResource, Lazy, Resource, Stack, Token, Duration, Names, FeatureFlags, Annotations } from '../../core';
import { CLOUDFRONT_DEFAULT_SECURITY_POLICY_TLS_V1_2_2021 } from '../../cx-api';

/**
Expand Down Expand Up @@ -321,6 +321,10 @@ export class Distribution extends Resource implements IDistribution {
if (!Token.isUnresolved(certificateRegion) && certificateRegion !== 'us-east-1') {
throw new Error(`Distribution certificates must be in the us-east-1 region and the certificate you provided is in ${certificateRegion}.`);
}

if ((props.domainNames ?? []).length === 0) {
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.');
}
}

const originId = this.addOrigin(props.defaultBehavior.origin);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defaultOrigin, defaultOriginGroup } from './test-origin';
import { Match, Template } from '../../assertions';
import { Annotations, Match, Template } from '../../assertions';
import * as acm from '../../aws-certificatemanager';
import * as cloudwatch from '../../aws-cloudwatch';
import * as iam from '../../aws-iam';
Expand Down Expand Up @@ -473,6 +473,7 @@ describe('certificates', () => {
},
},
});
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]');
});

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

0 comments on commit 01393c4

Please sign in to comment.