Route cloudfront wildcard CNAME subdomain to a folder in s3 bucket
Asked Answered
C

1

6

Is there a way to configure a single CloudFront distribution with S3 so that hello.world.com will serve files from my-bucket.s3.amazonaws.com/hello, and foo.world.com will serve files from my-bucket.s3.amazonaws.com/foo?

I've added a new distribution with wildcard CNAME of *.world.com and I can add origins as described (by specifying root folder), but I don't know how to route requests based on subdomain. In the behaviour settings, there is path pattern but I don't think it applies to a domain, rather than the path to a file.

Basically I wanna do path rewrite to replace the first directory with the subdomain on a single cloudfront distribution with a single s3 bucket.

Cosgrave answered 29/1, 2017 at 14:11 Comment(7)
Have you tried creating separate CloudFront distributionsWedekind
This can be done, and I've been working on an answer. If you're familiar with my answers, they're sometimes rather, um, thorough... so I need a bit of time. But it occurs to me that there are questions that might be relevant: how many wildcards are involved? <10? <100? <1000? More? Is there a reason you want this in a single distribution, instead of multiple? Using multiple distributions would not change the cost, and you could still do foo.example.com/buzz >> example-bucket/foo/buzz and bar.example.com/buzz >> example-bucket/bar/buzz.Pleasant
@Michael-sqlbot what i want is to allow users register with a username, and store their uploads under user.website.com. I could create many cloudfront distributions but that would require using AWS API to create one for each user. Say I will have 1000 users for starters -- do you mind sharing your solution please mate?Cosgrave
@Michael-sqlbot did you ever write that answer? I have the exact same use-case and want users to be able to choose their own subdomain and then serve their content (static websites) from a folder in S3.Vogel
Never mind, this can likely be done in a few lines of Lambda@Edge. I'll post an answer when I'm done. But might take a while since I'm in research phase, not building yet.Vogel
@Vogel I believe what you're looking for is in my answer at https://mcmap.net/q/968445/-serving-a-multitude-of-static-sites-from-a-wildcard-domain-in-awsPleasant
@Michael-sqlbot yes pretty much, thanks!Vogel
C
0

I think I need to configure s3 rather than cloudfront, http://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html

Cosgrave answered 29/1, 2017 at 14:35 Comment(2)
#29166516Cosgrave
This doesn't accomplish the intended result. Redirect rules only examine the path, and they can only be used to 30X redirect the browser, they do not do any kind of internal rewriting -- just external redirects.Pleasant

© 2022 - 2024 — McMap. All rights reserved.