By using AWS re:Post, you agree to the Terms of Use

How to add domain alias to existing CloudFront distribution using CDK

0

Hi all,

I just deployed a CloudFormation solutions from the AWS Solutions. The solutions included a new CloudFront distribution. My challenge is that I want to add a custom domain mysite.example.com to the dxxxxxx.cloudfront.net distribution. I already created an alias and certificate using Certificate Manager. My question is how do I add a new domain to the existing CloudFront.

I understand that we can import an existing distribution using Distribution.fromDistributionAttributes.

for example

const distribution = cloudfront.Distribution.fromDistributionAttributes(this, 'ImportedDist', {
  domainName: 'd111111abcdef8.cloudfront.net',
  distributionId: '012345ABCDEF',
});

Let's say I have the alias domain name and certificate ARN ready to use.

const domainName = 'mysite.example.com';
const certificateArn = 'arn:aws:acm:us-east-1: 123456789012:certificate/abcdefgh-1234-5678-9012-abcdefghujkl';

Where do I go from here?

Regards,

4 Answers
2
Accepted Answer

You won't be able to add it in (easily) with CDK. Because you are looking it up from attributes, when it synths the template, there won't be a CloudFront distribution in the resource section of the template.

You would have to create a stack with CDK that contains a distribution. Synth the template for that stack. Then create the stack manually using the template and import the CloudFront Distribution into the stack.

This would then allow you to interact with the Distribution via CDK.

The easier alternative would be to use a Custom Resource to use the Update Distribution API to add these values.

answered 10 months ago
  • Creating Custom Resource to use the Update Distribution API is actually a great idea! Thanks!

1

Unfortunately it is not possible to modify an imported existing distribution - you would need to create a new distribution

Existing distributions can be imported as well; note that like most imported constructs, an imported distribution cannot be modified. However, it can be used as a reference for other higher-level constructs

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-cloudfront-readme.html

You could theoretically create a CustomResource that would called a Lambda function to update the distribution via an API call, if you really needed to have this functionality bundled into your CDK stack

profile picture
answered 10 months ago
0

In CloudFront, an alternate domain name, also known as a CNAME, lets you use your own domain name (for example, www.example.com) in your files’ URLs instead of using the domain name that CloudFront assigns to your distribution. When you create a distribution, CloudFront provides a domain name for the distribution, such as d111111abcdef8.cloudfront.net. 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.

You can use the domainNames? parameter

see https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.Distribution.html#static-fromwbrdistributionwbrattributesscope-id-attrs

Type: string[] (optional, default: The distribution will only support the default generated name (e.g., d111111abcdef8.cloudfront.net)) 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.

With the AWS CLI you would call

aws cloudfront associate-alias --alias www.example.com --target-distribution-id EDFDVBD6EXAMPLE https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html

answered 10 months ago
0

Hi, I believe that's not possible at the moment. The documentation states

Existing distributions can be imported as well; note that like most imported constructs, an imported distribution cannot be modified. However, it can be used as a reference for other higher-level constructs.

profile picture
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions