CloudFront can’t route traffic based on the (sub)domain of the request.

CloudFront can’t route traffic based on the (sub)domain of the request.

Consider the popular scenario where a site is using a subdomain to serve static assets that can be effectively cached in a CDN.

www.sls-site.com servers HTML documents

cdn.sls-site.com servers CSS, JS, fonts, images and video

You decide to use CloudFront and you start to configure your distribution.

You add an Origin for your backend HTML renderer (like WordPress) and point it to its address.

You add a second Origin for you static assets and point to a S3 bucket.

At first one might think that since we can point multiple domains to a single CloudFront distribution we can easily manage what origin servers a request based on the domain it came from. Sadly this is not the case.

A CacheBehavior describes how requests are handled by the CDN. This includes matching by request path (path only), target origin, https and cache policies.

When you define your CacheBehaviors there’s only the Path Pattern property that allows you to specify a matching pattern. So no matter how many domains you have CNAME to the distribution they are all treated the same.