Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removes the requirement to have a domain name when using SSL certificates #26605

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ new cloudfront.Distribution(this, 'myDist', {
When you create a distribution, CloudFront assigns a domain name for the distribution, for example: `d111111abcdef8.cloudfront.net`; this value can
be retrieved from `distribution.distributionDomainName`. CloudFront distributions use a default certificate (`*.cloudfront.net`) to support HTTPS by
default. If you want to use your own domain name, such as `www.example.com`, you must associate a certificate with your distribution that contains
your domain name, and provide one (or more) domain names from the certificate for the distribution.
your domain name, and optionally provide one (or more) domain names from the certificate for the distribution.

The certificate must be present in the AWS Certificate Manager (ACM) service in the US East (N. Virginia) region; the certificate
may either be created by ACM, or created elsewhere and imported into ACM. When a certificate is used, the distribution will support HTTPS connections
Expand Down
7 changes: 1 addition & 6 deletions packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ export interface DistributionProps {
* Alternative domain names for this distribution.
*
* If you want to use your own domain name, such as www.example.com, instead of the cloudfront.net domain name,
* you can add an alternate domain name to your distribution. If you attach a certificate to the distribution,
* you must add (at least one of) the domain names of the certificate to this list.
* you can add an alternate domain name to your distribution.
*
* @default - The distribution will only support the default generated name (e.g., d111111abcdef8.cloudfront.net)
*/
Expand Down Expand Up @@ -306,10 +305,6 @@ 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) {
throw new Error('Must specify at least one domain name to use a certificate with a distribution');
}
}

const originId = this.addOrigin(props.defaultBehavior.origin);
Expand Down
28 changes: 13 additions & 15 deletions packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,23 +453,21 @@ describe('certificates', () => {
}).toThrow(/Distribution certificates must be in the us-east-1 region and the certificate you provided is in eu-west-1./);
});

test('adding a certificate without a domain name throws', () => {
test('adding a certificate without a domain name is permitted', () => {
const certificate = acm.Certificate.fromCertificateArn(stack, 'Cert', 'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012');
new Distribution(stack, 'Dist', {
defaultBehavior: { origin: defaultOrigin() },
certificate: certificate,
});

expect(() => {
new Distribution(stack, 'Dist1', {
defaultBehavior: { origin: defaultOrigin() },
certificate,
});
}).toThrow(/Must specify at least one domain name/);

expect(() => {
new Distribution(stack, 'Dist2', {
defaultBehavior: { origin: defaultOrigin() },
domainNames: [],
certificate,
});
}).toThrow(/Must specify at least one domain name/);
Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', {
DistributionConfig: {
ViewerCertificate: {
AcmCertificateArn: 'arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012',
},
},
});
});
});

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