Search code examples
amazon-web-servicesamazon-s3amazon-cloudfrontamazon-route53

Map wildcard subdomains to S3 static site folders


Is there a way that, by using a mix of the AWS Cloudfront, S3 and Route 53 to set up a set of static websites in such a way that an indefinite number of subdomains (wildcard) are mapped to a single S3 bucket's folders?

Eg:

http://first.example.com maps to https://s3.amazonaws.com/example.com/first/index.html

http://second.example.com maps to https://s3.amazonaws.com/example.com/second/index.html


Solution

  • yes, you can do it with Lambda@Edge functions.

    First, map the wildcard (*) subdomain on Route53.

    Then add the wildcard domain *.example.com to the alternate domain names in CloudFront

    Write a Lambda function that changes the origin path based on the subdomain in the request. See an example here