navapbc / template-infra

A template to set up foundational infrastructure for your application in AWS
Apache License 2.0
11 stars 2 forks source link

Provide an optional CDN resource for web applications with static assets #459

Open sawyerh opened 1 year ago

sawyerh commented 1 year ago

Web applications include static assets (images, fonts, css, client-side JS bundles) and can also include static HTML pages. A CDN, like Cloudfront, provides a caching layer that can improve the performance and scaleability of these web applications. It could be helpful if the Infra template provided a quick way to setup a CDN on a project.

Technical design

Proposed design

Can reference a variation of this approach here.

Alternatives considered

Testing

Release management

sawyerh commented 1 year ago

cc @daphnegold @SammySteiner @acouch in case y'all go this route on your project

lorenyu commented 10 months ago

@sawyerh thanks sawyer. added my thoughts on technical design

sawyerh commented 9 months ago

I implemented most of this today on a project. I did it quickly so probably room for improvement, but hopefully a useful starting point

lorenyu commented 9 months ago

awesome! might need to pick your brain later to see how it works and what we can use.

some initial questions:

sawyerh commented 9 months ago

@lorenyu Good questions.

is it best practice these days to push static assets to s3 and then put the cdn in front of it? for my own understanding, is it also common to have the cdn pull from the server directly, or is that not ideal due to cache stampede issues?

I initially had the S3 bucket because I couldn't figure out how to get Django to serve the static assets but finally figured that out. In the latest changes on the repo I linked, the S3 bucket is no longer part of the infra. I think having the CDN pull from the server directly seems fine but I haven't researched it too deeply. Seems fine if it is a relatively long cache.

is the idea that eventually if we want to use a custom domain that the assets would be served from that domain, or would we keep the cdn domain?

I think using the default Cloudfront domain would suffice for the template and most teams could get away with just using the default. A custom domain would be useful is if it's a static site and you are also putting the static site behind Cloudfront.