CloudFrontWebDistribution S3 website bucket
See original GitHub issueI’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 :
- Route53 (domainname) -> Cloudfront (with certificate) -> S3 bucket (set to redirect to www.domainname)
- 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:
- Created 4 years ago
- Comments:13 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you want to use the website url, you must use a
customOriginSource
like that: https://github.com/aws/aws-cdk/blob/aac35ff29d1c9e9edb6cc4d70f28596f07762f2a/packages/%40aws-cdk/aws-route53-patterns/lib/website-redirect.ts#L55-L58@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 😃