Skip to content

Commit acdf7d3

Browse files
authored
fix(cloudfront): requirement of domainNames prevents moving a domain name between distributions (#31001)
### Issue # (if applicable) Closes #29960. ### Reason for this change When I want to move a domain name from a distribution to another distribution, I must create a distribution with a certificate associated but no domain names. ### Description of changes Re-submit of previous #29329. Removed the validation that `domainNames` must not be blank when a certificate is associated. ### Description of how you validated changes Updated a unit test to allow absent domainNames when a certificate is associated. See AWS Documentation for details: Using custom URLs by adding alternate domain names (CNAMEs) > Moving an alternate domain name to a different distribution https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html#alternate-domain-names-move ### 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 5387605 commit acdf7d3

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

Diff for: packages/aws-cdk-lib/aws-cloudfront/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ new cloudfront.Distribution(this, 'myDist', {
115115
});
116116
```
117117

118+
#### Moving an alternate domain name to a different distribution
119+
120+
When you try to add an alternate domain name to a distribution but the alternate domain name is already in use on a different distribution, you get a `CNAMEAlreadyExists` error (One or more of the CNAMEs you provided are already associated with a different resource).
121+
122+
In that case, you might want to move the existing alternate domain name from one distribution (the source distribution) to another (the target distribution). The following steps are an overview of the process. For more information, see [Moving an alternate domain name to a different distribution](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/alternate-domain-names-move.html).
123+
124+
1. Deploy the stack with the target distribution. The `certificate` property must be specified but the `domainNames` should be absent.
125+
2. Move the alternate domain name by running CloudFront `associate-alias` command. For the example and preconditions, see the AWS documentation above.
126+
3. Specify the `domainNames` property with the alternative domain name, then deploy the stack again to resolve the drift at the alternative domain name.
127+
118128
#### Cross Region Certificates
119129

120130
> **This feature is currently experimental**

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ export interface DistributionProps {
129129
*
130130
* If you want to use your own domain name, such as www.example.com, instead of the cloudfront.net domain name,
131131
* you can add an alternate domain name to your distribution. If you attach a certificate to the distribution,
132-
* you must add (at least one of) the domain names of the certificate to this list.
132+
* you should add (at least one of) the domain names of the certificate to this list.
133+
*
134+
* When you want to move a domain name between distributions, you can associate a certificate without specifying any domain names.
135+
* For more information, see the _Moving an alternate domain name to a different distribution_ section in the README.
133136
*
134137
* @default - The distribution will only support the default generated name (e.g., d111111abcdef8.cloudfront.net)
135138
*/
@@ -318,10 +321,6 @@ export class Distribution extends Resource implements IDistribution {
318321
if (!Token.isUnresolved(certificateRegion) && certificateRegion !== 'us-east-1') {
319322
throw new Error(`Distribution certificates must be in the us-east-1 region and the certificate you provided is in ${certificateRegion}.`);
320323
}
321-
322-
if ((props.domainNames ?? []).length === 0) {
323-
throw new Error('Must specify at least one domain name to use a certificate with a distribution');
324-
}
325324
}
326325

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

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

+13-14
Original file line numberDiff line numberDiff line change
@@ -457,23 +457,22 @@ describe('certificates', () => {
457457
}).toThrow(/Distribution certificates must be in the us-east-1 region and the certificate you provided is in eu-west-1./);
458458
});
459459

460-
test('adding a certificate without a domain name throws', () => {
460+
test('adding a certificate without a domain name', () => {
461461
const certificate = acm.Certificate.fromCertificateArn(stack, 'Cert', 'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012');
462462

463-
expect(() => {
464-
new Distribution(stack, 'Dist1', {
465-
defaultBehavior: { origin: defaultOrigin() },
466-
certificate,
467-
});
468-
}).toThrow(/Must specify at least one domain name/);
463+
new Distribution(stack, 'Dist1', {
464+
defaultBehavior: { origin: defaultOrigin() },
465+
certificate,
466+
});
469467

470-
expect(() => {
471-
new Distribution(stack, 'Dist2', {
472-
defaultBehavior: { origin: defaultOrigin() },
473-
domainNames: [],
474-
certificate,
475-
});
476-
}).toThrow(/Must specify at least one domain name/);
468+
Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', {
469+
DistributionConfig: {
470+
Aliases: Match.absent(),
471+
ViewerCertificate: {
472+
AcmCertificateArn: 'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012',
473+
},
474+
},
475+
});
477476
});
478477

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

0 commit comments

Comments
 (0)