question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set Alternative Domain Name (CName) in aws-cloudfront

See original GitHub issue

I can’t seem to find how to set the alternative domain name in cloudfront. I looked through the source code but couldn’t find any. Here is my code

import * as cloudfront from '@aws-cdk/aws-cloudfront';
// import * as route53 from '@aws-cdk/aws-route53';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/cdk';
import { ViewerProtocolPolicy } from '@aws-cdk/aws-cloudfront';

export interface StaticSiteProps {
  domainName: string;
  siteSubDomain: string;
  certificateArn?: string;
  s3Region: string;
}

export class StaticSite extends cdk.Construct {
  constructor(parent: cdk.Construct, name: string, props: StaticSiteProps) {
    super(parent, name);
    const siteDomain = props.siteSubDomain + '.' + props.domainName;
    const siteBucket = new s3.Bucket(this, 'SiteBucketReal', {
      bucketName: siteDomain,
      websiteIndexDocument: 'index.html',
      websiteErrorDocument: 'index.html',
      publicReadAccess: true,
    });
    new cdk.Output(this, 'SiteBucketName', { value: siteBucket.bucketName });
    const distribution = new cloudfront.CloudFrontWebDistribution(
      this,
      'SiteDistribution',
      {
        // TODO: Set Alternative Domain Name?
        viewerProtocolPolicy: ViewerProtocolPolicy.AllowAll,
        originConfigs: [
          {
            customOriginSource: {
              domainName: `${siteDomain}.s3-website-${
                props.s3Region
              }.amazonaws.com`,
            },
            behaviors: [{ isDefaultBehavior: true }],
          },
        ],
      },
    );
    new cdk.Output(this, 'DistributionId', {
      value: distribution.distributionId,
    });    
  }
}

I’m trying to translate this https://aws.amazon.com/premiumsupport/knowledge-center/cloudfront-serve-static-website/ to aws-cdk

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
christiankaindlcommented, Mar 11, 2021

For us, the easiest way to achieve CNAME aliases + custom certificate was to use the viewerCertificate option in the cloudfront.CloudFrontWebDistribution constructor:

const distribution = new cloudfront.CloudFrontWebDistribution(
      this,
      "my-cdn",
      {
        viewerCertificate: {
          aliases: ['your-domain.com'],
          props: {
            acmCertificateArn: certificateArn, // optional
            sslSupportMethod: 'sni-only',
            minimumProtocolVersion: 'TLSv1.1_2016'
            // All `props` options here: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.CfnDistribution.ViewerCertificateProperty.html
          }
        }
      }
    );

VS Code marks aliasConfiguration as deprecated, which is why we use the above instead. There’s also additional props options: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cloudfront.CfnDistribution.ViewerCertificateProperty.html

3reactions
thiskevinwangcommented, Mar 5, 2021

as of 1.91.0 -

With new cloudfront.Distribution

  • it’s easy to add “Alternate Domain Names (CNAMEs)”
  • but it’s seemingly impossible to integrate with an API Gateway Origin
  • Security Policy defaults to TLSv1.2_2019 which is nice

With new cloudfront.CloudFrontWebDistribution

  • everything looks deprecated
  • but at least its possible to add an API Gateway “customOriginSource”
  • Security Policy defaults to TLSv1 which is bad

are there plans to improve the Distribution API?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using custom URLs by adding alternate domain names ...
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...
Read more >
Amazon CloudFront Alternate Domain Names
And only then update your distribution to add an alternate domain name: Open Amazon CloudFront console -> General view -> Edit -> Alternate...
Read more >
Configuring alternate domain names and HTTPS
To use alternate domain names in the URLs for your files and to use HTTPS between viewers and CloudFront, perform the applicable procedures....
Read more >
Set up Your Domain with CloudFront
Head over to the details of your CloudFront Distribution and hit Edit. ... And type in your new domain name in the Alternate...
Read more >
How to Setup Custom Domain for Amazon CloudFront?
Now, you can click on “Edit” and then add the custom domain to the “Alternate Domain Names” option. The alternate domain CloudFront is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found