Search code examples
amazon-web-servicesamazon-cloudfront

How can I replace my CloudFront distribution with another without user downtime


Initial Setup

  • On AWS I have a Elastic Beanstalk (EB) server that serves pages.
  • During any month for a particular request it will always serve the same data so I set up a Cloudfront distribution to better serve customers and reduce server hits.
  • In Route53 have domain configured to point to Cloudfront distribution
  • All customer access is via this domain

Each Month

  • I deploy a new version of EB Server
  • I then wanted to invalidate all pages within my CloudFront so that it would get newest data from EB Server.

But invalidation is slow and expensive so instead my plan was to just create a new Cloudfront distribution. then modify my Route53 domain to point to the new CloudFront, then once done delete the original CloudFront distribution.

The trouble is that CloudFront does not let you create a new distribution if you set an Alternate CName that is already used by another distribution. So I cannot create my new distribution until I have disabled and then deleted the existing one, and then I have to deploy my new one, this takes ages and would be site would be down for that period.

My alternative is as follows

  • Deploy new EB Server
  • Point Route53 to Load balancer of new EB Server
  • Disable CloudFront Distribution, and wait.
  • Delete CloudFront Distribution, and wait.
  • Create new CloudFront Distribution, and wait.
  • Point Route 53 Domain to new CloudFront Distribution

Solution

  • You actually don't have to wait for the distribution to be deleted to reuse the cname. I do something similar.

    • create a new distribution that has identical settings as my current one, minus the cname settings
    • remove the cname from the old distribution, save
    • immediately add cname to new distribution
    • update DNS to point to new distribution
    • keep old distribution around until both are deployed
    • disable and then delete, to clean up

    Since I alias to the cloudfront distribution, I don't worry about resolvers caching my old cloudfront provided DNS name. It's worked rather well for me.