spenczar / lektor-s3

Plugin to deploy a Lektor project to an S3 bucket
MIT License
44 stars 10 forks source link

Invalidate Cloudfront #6

Closed singingwolfboy closed 8 years ago

singingwolfboy commented 8 years ago

I use CloudFront in front of my S3-hosted site, so that I can use AWS Certificate Manager to get HTTPS on my site. However, this means that every time I deploy to my S3 bucket, I need to invalidate objects in CloudFront. This pull request automates that process.

spenczar commented 8 years ago

This looks excellent, thank you!

singingwolfboy commented 8 years ago

@spenczar: I've moved the code into a invalidate_cloudfront() method, as you suggested. Thanks for the feedback!

Here's the documentation for CloudFront invalidation, and here's the documentation for CloudFront pricing. The established best practice (as far as I'm aware) is to not bother invalidating deleted objects from CloudFront, because AWS doesn't charge for keeping objects in CloudFront, only for accessing them -- and it does charge for doing object invalidation (after the first 1000 objects per month). Therefore, it's cheaper to make things immutable -- once an object goes into CloudFront, it's never modified, and updated versions are put into CloudFront with a new URL.

Does that make sense?

spenczar commented 8 years ago

While that makes sense from a cost-management perspective at large scale, I don't think it's the right policy with Lektor-based projects. If someone deletes a blog post and then calls lektor publish, I think they expect their blog post to disappear from the internet ASAP.

I guess we'll do a tiny bit of wasted work for people who are removing (say) static assets like images or javascript or stylesheets. But the expected workflow with Lektor is a static site whose assets probably don't get deleted super often. If someone is deleting thousands of objects per month, I think they are the exceptional case, and even then we've only cost them a relatively small amount - we're not going to be accidentally charging people a thousand bucks or anything.

Let's just invalidate everything in the diff set, including deleted objects. I think this is least likely to surprise people.

singingwolfboy commented 8 years ago

@spenczar: Would it make more sense to just invalidate everything on every deploy, then? Especially since I just found this in the documentation:

The charge to submit an invalidation path is the same regardless of the number of objects you're invalidating: a single object (/images/logo.jpg) or all of the objects that are associated with a distribution (/*). For more information, see Amazon CloudFront Pricing.

We could just invalidate /* on every deploy, which is easy and cheap. If the developer doesn't want that to happen, then can just not put their CloudFront distribution ID in the .lektorproject file.

spenczar commented 8 years ago

Oh, I think that sounds good. It's way simpler, which is great. One note: it should be /{self.key_prefix}* because the developer might have things under a key prefix in their bucket.

singingwolfboy commented 8 years ago

@spenczar: Got it -- I've updated the code again. As an aside, I don't know anything about key prefixes, and I don't see anything about that in the README. Can you double-check that I'm doing it right?

spenczar commented 8 years ago

Looks great! Yeah, key prefixes should be documented, I'll add that in another commit.

Thanks for this!