0

I have a CloudFront distribution set up so that <domain>/api redirects me to <api-gateway-url>/<env>/api. However I find that sometimes CloudFront caches responses to GET requests and the browser does not redirect to the API Gateway endpoint and returns the cached response.

Example: /api/getNumber redirects to <api-gateway-url>/<env>/api/getNumber and returns me 2. I change the response so that it should return the number 300, but when I make a request through my browser now there is no redirect and I still get back the number two. The x-cache response header says cache hit from CloudFront.

jcharch
  • 150
  • 16
  • You are using the word "redirect" when it sounds like you mean "forward." A redirect is an HTTP 30x response that tells the browser to repeat the request at a different URL. Please confirm whether you actually mean "redirect" or whether CloudFront is actually *forwarding* the request. – Michael - sqlbot May 12 '19 at 02:58
  • @Michael-sqlbot so I had my CF configuration wrong and it was creating a redirect instead of an alias. I followed https://stackoverflow.com/questions/38050191/integrating-aws-api-gateway-with-cloud-front-without-exposing-origin?rq=1 and fixed this issue. Now I do get a 'Miss from CloudFront' response each time. But I am still not confident that CF won't cache my results and would like to guarantee a fresh response from this endpoint each time. – jcharch May 12 '19 at 21:31

1 Answers1

0

AWS CloudFront is often used for caching, thus decreasing the number of requests that will hit the back-end resources. Therefore you shouldn't use CloudFront on your testing environment if you want to imediately see changes.

In your case it seems, that your endpoint doesn't have any parameters (Path/Query), so essentially what CloudFront sees is the same request every time, naturally in this case you will hit the cache.

You have a couple of options to "fix" that:

  1. Diversify your API requests (using parameters for example)
  2. Use CloudFront's TTL options, to make CloudFront keep the cached objects less time

    NOTE: This is not advisable if this is production environment, because it might eliminate the whole point of caching and disrupt expected behavior

  3. Disable CloudFront's caching for those paths that don't take parameters and/or whose response will change often, thus keeping caching on for the rest of your distribution:

    https://aws.amazon.com/premiumsupport/knowledge-center/prevent-cloudfront-from-caching-files/

  4. And lastly if this is just your test environment, disable CloudFront, but the things above might later on apply to your production environment

nitheism
  • 537
  • 3
  • 14
  • Regarding point 3 - I'm adding a `Cache-Control: no-store` header on my origin. I'm seeing a `Cache-Control: no-cache` header in my response instead, but that's an issue for another question I suppose. If I add this header CF *should* always respect it and I'll never get a cached response, correct? – jcharch May 12 '19 at 21:41
  • @jcharch If you scroll a bit down in this document: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html You will see that although CloudFront respects that header there are some exceptions listed. But besides that in the last column for this option it says this: "CloudFront caching CloudFront caches objects for the value of the CloudFront minimum TTL." – nitheism May 13 '19 at 05:57