← Build log · Part 8 of 8

One CloudFront distribution, many sites

A small viewer-request function routes hostnames to folders, and one flag in a deploy script stops it deleting them.

AWSInfrastructure

By now there were four hostnames to serve: the company site, a brand guide, a profile and this blog. Four distributions would work, but one distribution with a router is less to look after.

Certificates cannot be edited

To add a name, you request a new certificate that lists every name and swap it onto the distribution. The script looks for an existing certificate with all the names first, so re-running it reuses what is there.

The router

A CloudFront Function runs on each request, looks at the Host header and prefixes the path. The three original hostnames are left alone and keep serving the bucket root.

var sites = {
  'brand.kulkarniinc.com':   '/sites/brand',
  'profile.kulkarniinc.com': '/sites/profile',
  'blog.kulkarniinc.com':    '/sites/blog'
};

Paths that end in a slash, or have no file extension, get /index.html appended.

Two gotchas

  • The company site's deploy uses aws s3 sync --delete, which would happily remove the other sites' folders. It now has --exclude "sites/*".
  • Files that are not content-hashed need a CloudFront invalidation after each deploy, or visitors keep the old copy. The deploy scripts create one.

One more limit to know: the distribution's error pages fall back to the company site's home page, so a mistyped URL on a subdomain shows that page rather than a custom 404.

Kulkarni Inc.
Notes from building the Kulkarni Inc. stack. See all