Search code examples
amazon-web-servicesamazon-cloudfront

Can I serve multiple domains with CloudFront?


Can I implement the following behavior with AWS CloudFront:

https://{id}.example.com/ -> https://{id}.execute-api.us-east-1.amazonaws.com/prod/

I want to make a CloudFront distribution that would simplify the API Gateway domains.

The {id} part is a placeholder, not a specific ID. I want *.example.com to match and redirect to https://*.execute-api.us-east-1.amazonaws.com/prod/ but with the * replaced by the part of the domain that matched.


Solution

  • I assume you want the Origin to be as https://{id}.execute-api.us-east-1.amazonaws.com/prod/ rather then giving new location (302) to Client. In this case, you can use lambda@edge to dynamically choose origin and pass the value you want. Here is an example:

    https://aws.amazon.com/blogs/networking-and-content-delivery/dynamically-route-viewer-requests-to-any-origin-using-lambdaedge/

    Whitelist the HOST header so you can parse the HOST header in the origin request function and add the parsed value in setting origin.