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.

CloudFrontWebDistribution S3 website bucket

See original GitHub issue

I’m trying to setup a simple webpage with static html. I want to have redirect from domainname to www.domainname and it should support SSL. What I have setup is :

  1. Route53 (domainname) -> Cloudfront (with certificate) -> S3 bucket (set to redirect to www.domainname)
  2. Route53 (www.domainname) -> Cloudfront (with certificate) -> S3 bucket (serves files)

The thing is that the origin path in Cloudfront for the redirect is using the URL directly to the S3 bucket and not the website version which is needed for redirect to work properly.

    // ---- Buckets ----
    const websiteBucket = new s3.Bucket(this, 'WebsiteBucket', {
      bucketName: `www.${domainName}`,
      websiteErrorDocument: 'index.html',
      websiteIndexDocument: 'index.html',
      publicReadAccess: true
    });

    const redirectBucket = new s3.Bucket(this, 'WebsiteRedirectBucket', {
      bucketName: domainName,
      websiteRedirect: {
        hostName: `www.${domainName}`
      },
      publicReadAccess: true
    });

    // ---- Cloudfront ----
    const websiteCloudfront = new cloudfront.CloudFrontWebDistribution(this, 'WebsiteCloudfront', {
      originConfigs: [{
        s3OriginSource: { s3BucketSource: websiteBucket },
        behaviors: [{ 
          compress: true,
          isDefaultBehavior: true,
        }],
      }],
      viewerCertificate: cloudfront.ViewerCertificate.fromAcmCertificate(
        certificate,
        { aliases: [`www.${domainName}`] }
      ),
    });

    const websiteRedirectCloudfront = new cloudfront.CloudFrontWebDistribution(this, 'WebsiteRedirectCloudfront', {
      originConfigs: [{
        s3OriginSource: { s3BucketSource: redirectBucket },
        behaviors: [{ 
          isDefaultBehavior: true,
        }],
      }],
      viewerCertificate: cloudfront.ViewerCertificate.fromAcmCertificate(
        certificate,
        { aliases: [domainName] }
      ),
    });

    // ---- DNS Records ----
    new route53.ARecord(this, 'WebsiteAlias', {
      recordName: 'www',
      target: route53.RecordTarget.fromAlias(new route53targets.CloudFrontTarget(websiteCloudfront)),
      zone,
    });

    new route53.ARecord(this, 'WebsiteRedirectAlias', {
      target: route53.RecordTarget.fromAlias(new route53targets.CloudFrontTarget(websiteRedirectCloudfront)),
      zone
    });

Is there a workaround I can use?

This is 🐛 Bug Report

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
jogoldcommented, Jan 13, 2020
1reaction
johankvintcommented, Jan 21, 2020

@jogold I ended up using customOriginSource. Thanks But it would be great if you could just send in a S3 bucket with redirect properties and it would be automatic for you 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use CloudFront to serve a static website hosted on Amazon S3
Create a CloudFront web distribution. In addition to the distribution settings ... Enter your Amazon S3 bucket name in Origin Domain field.
Read more >
Setup a CloudFront distribution with SSL, custom domain and ...
In this section, we are going to create an S3 bucket, open the bucket for public acess, set it up for static website...
Read more >
OAI or not OAI for serving a static website in S3 using ...
Yes best practice is to use OAI with CloudFront with origin 'S3 Static Bucket website'. If you are using CloudFront/Distribution API you must...
Read more >
aws s3 cloudfront typescript aws cdk - Dennis O'Keeffe Blog
Deploying Static Websites To AWS S3 + CloudFront + Route53 Using ... of a static website to an S3 bucket that has CloudFront...
Read more >
How to Use Amazon CloudFront to Serve a Static Website
We utilize the Amazon S3 console to establish a bucket and upload the website files. Next, we'll set up an Amazon CloudFront web...
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