6

I have deployed my React app on s3. I am using Cloudfront to use a certificate and reach my s3 bucket through HTTPS. After struggling setting it all up, I managed to set it all up, it is now working well.

Now I updated my project, created a new version of bundle.js, uploaded it to s3.

My issue now is that mydomain.com points to the V1 of bundle.js

So what I tried to dig up a little bit more, and here is what I found:

  • mydomain.com points to V1
  • xxxxx.cloudfront.net points to V1
  • mydomain.com.s3-website-eu-west-1.amazonaws.com points to V2

So my guess is that for some reason, cloudfront points to the V1, but why ? Is there some cache somewhere in there ?

Here is the config, in case it helps:

  • Route53 Type A points to xxxxxx.cloudfront.net
  • cloudfront domain is xxxxxx.cloudfront.net
  • cloudfront CNAMES are mydomain.com and www.mydomain.com
  • cloudfront origin domain name and path is mydomain.com.s3-website-eu-west-1.amazonaws.com
  • s3 bucket is mydomain.com

PS : Just to double check that the issue was not only coming from bundle.js, I deleted the background image from the bucket, but somehow, it is still found and used when accessing mydomain.com (so showing the V1)

4
  • 2
    CloudFront is most likely serving your site from their cache, rather than hitting the server every time. Commented Mar 28, 2017 at 10:32
  • Thanks @JoeClay for the suggestion. I checked few more posts, and it seems to be that. I am now trying to find out how to force clear it Commented Mar 28, 2017 at 10:42
  • Great! I don't know enough about CloudFront to tell you how to fix it, but I'm glad I could point you in the right direction. Commented Mar 28, 2017 at 10:43
  • THANK YOU! I did a pretty major website update and I would go to the s3 bucket and manually execute index.html and it had the new version, if I went to the domain it had the old?!? Was going nuts trying to figure out WHY, Thank You for the answer! Commented Sep 28, 2022 at 20:41

3 Answers 3

13

As @Joe Clay confirmed, Cloudfront was caching everything.

To force clear the cache using the AWS console (I found in some docs that it can be done using their API), here are the steps I followed:

  • log in to AWS console
  • go to Cloudfront and see details of your distribution
  • go to invalidations tab, and click on Create invalidation
  • put in object path * and save
  • (took about 5 good minutes to complete)
  • Refresh the website mydomain.com (might need to clean the browser cache)
  • and voila !

Hope this answer can help anyone stuck with the same problem!

Sign up to request clarification or add additional context in comments.

3 Comments

This 5 minutes will be counted as a downtime?
We shouldn't do this manually every time. Did you got any solution to automate it with your CI/CD? (Probably we can clear CDN cache on every build (time taking) or use versioning on objects)
I agree it shouldn't be done manually. As this project is a small personal one, I don't mind doing it (a few times a year), and I haven't investigated on automation. If anyone has a good way of doing it I'd be happy to know how :)
5

I know this is old question. But i found a way to automate this process using my deployment script. Heres an entry from my package.json file

"deploy": "npm run build && aws s3 sync --delete build/ s3://bucket-name && npm run invalidateCache",
"invalidateCache": "aws cloudfront create-invalidation --distribution-id E23232323 --paths '/*'",

After this all you have to do is to run npm run deploy.

Explanation of commands.

  1. deploy: This creates a new build for my react app. Then it deletes all old files from my s3 bucket and uploads new files. After that it runs my invalidateCache command
  2. invalidateCache: This creates a new invalidation in my cloudfront distribution. You have to provide the distribution id which you can find in the below image. The last thing is the path. I entered /* so that it clears cache for all paths. enter image description here

Hope this helps :)

Comments

1

I know this is an old question. This can also be achieved using lambda fn which will invalidate the cloudfront cache for any update in the source origin s3 bucket.

  1. Create a lambda function which gets triggered for any update in sourcce s3 bucket files.
  2. In the lambda code part, write a code to invalidate cloudfront cache.

Code snippet is here for the same.

from __future__ import print_function

    import boto3
    import time

    def lambda_handler(event, context):
        path = []
        for items in event["Records"]:
            if items["s3"]["object"]["key"] == "index.html":
                path.append("/")
            else:
                path.append("/" + items["s3"]["object"]["key"])
        print(path)
        client = boto3.client('cloudfront')
        invalidation = client.create_invalidation(DistributionId='cloudfrontdistribution id',
            InvalidationBatch={
                'Paths': {
                    'Quantity': 1,
                    'Items': path
            },
            'CallerReference': str(time.time())
        })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.