Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I deploy a new cloudfront s3 version without a small period of unavailability?

I am utilizing AWS cloudfront with an S3 origin. I'm using a webpack plugin to cache-bust using chunked hash file names for all of my static files excluding index.html, which I will simply invalidate using the cloudfront feature upon each new release.

I plan on using a jenkins build to run aws s3 sync ./dist s3://BUCKET-NAME/dist --recursive --delete which will swap out the new chunked files as necessary. Then I will overwrite the index.html file to use the new chunked reference. During the few seconds (max) it takes to swap out the old files for new, it is possible that a user will make a request to the website from a region in which cloudfront has not cached the resources, at which point they'll be unavailable because I have just deleted them.

I could not find any information about avoiding this edge case.

like image 858
apdm Avatar asked Sep 12 '17 00:09

apdm


People also ask

How do I put S3 on CloudFront?

Open the CloudFront console. Choose Create Distribution. Under Origin, for Origin domain, choose your S3 bucket's REST API endpoint from the dropdown list. Or, enter your S3 bucket's website endpoint.

How long does it take to deploy CloudFront?

CloudFront can take up to 25 minutes to deploy. This is because CloudFront delivers content through a worldwide network of low latency and high performance edge locations. It can take additional time depending on how long it takes to propagate changes in configurations such as certificates, origins, settings and more.

Can I use S3 without CloudFront?

You can use the S3 domain with the Amazon SSL certificate like: https://my-example-bucket.s3-website-us-east-1.amazonaws.com . If you want to use a custom domain with SSL, and you can't use CloudFront, then you will need to look into placing some other proxy in front of S3 like your own Nginx server or something.


1 Answers

Yes, it can happen that a person near a different edge location experience the missing files. To solve this, you need to change the approach of doing new deployments since cache busting and time is unpredictable at request-response level. One commonly used pattern is to keep different directories(paths) for each new deployment in S3 as follows.

For release v1.0
/dist/v1.0/js/*
/dist/v1.0/css/*
/dist/index.html <- index.html for v1.0 release which has reference for js & css in /dist/v1.0 path

For release v1.1
/dist/v1.1/js/*
/dist/v1.1/css/*
/dist/index.html <- index.html for v1.1 release which has reference for js & css in /dist/v1.1 path

After each deployment, a user will receive either the old version(v1.0) or new version(v1.1) of the index.html, which will still working during the transition period until the edge cache is busted.

You can automate the versioning with Jenkins either incrementing the version or using parameterize build plugin.

This will also be useful to do immutable deployments, where in a case of a critical issue, you can rollback to the previous deployments. Apart from that you can configure S3 lifecycle management rules to archive the older versions.

like image 84
Ashan Avatar answered Nov 03 '22 07:11

Ashan