Getting cloudfront up and running

by mark | 21 May 2022, 3:44 p.m.

This turned out to be both very easy and fiendish. 

I have a thing called CKEditor that lets me do things like upload image files. This is already set up to point at my AWS S3 bucket to store uploaded media files. I wanted to use the Cloudfront CDN to distribute them. So I had some fun and games to do this...

  • go into AWS and create a Cloudfront distribution which points at the S3 bucket 
  • Discover that you need an SSL certificate from Amazon for your domain, and they'll only give it to you if you go through some CNAME fun and games to authenticate your ownership of the domain 
  • Create another SSL cert as it needs to be made in the US, not the UK datacentre, there is a small dropdown you need to watch out for, so more CNAME thrashing
  • Finally create a CNAME that points from assets.rkdouglas.co.uk to the cloudfront distribution itself 
  • Tell Django to use cloudfront URLs for media uploads - the S3Boto3Storage subclass you made when getting S3 uploads working in the first place has a custom_domain attribute you point at your assets.rkdouglas.co.uk equivalent 
  • Realise that all the stuff is hardcoded to point at S3 already, so log in to PSQL and issue an update command to rewrite S3 links to CDN links

I spent hours doing this. Documentation is poor. If you find this via Google you are welcome. 

No comments

Back to all articles