Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly disable CloudFront caching for API requests

I have AWS CloudFront set up to serve static content and an API server from the same domain. This means I have two behaviors, one serving the API gateway from /api and one serving content from S3 for all other paths.

The problem is that I don't want CF to cache anything from the API server. I was surprised to find that there does not seem to be a "master setting" to completely disable caching behavior; instead the docs refer to using Cache-control: no-cache on the origin or turning on "Cache based on all headers" in the CF behavior.

However, none of these solutions completely satisfy my requirement on simply not caching and passing through all headers. If I add Cache-control: no-cache to my origin, CF seems to respect that, but there is still the question of CF settings. CF has a setting "Cache based on headers: All/None/Whitelist". The docs says that to disable caching, "All" should be used, which makes sense (although a bit vague as compared to having an actual setting: Disable caching: on/off). However, as soon as I set this setting to "All", the entire behavior is disabled and my API requests will not reach the API gateway at all, but default to the S3 behavior used for non-API requests. I cannot find any explanation for this, it's like the entire behavior fails or is disabled without explanation.

The other problem is that headers not present in the "Cache based on" will not only be excluded from caching (which I don't want anyway), but also stripped out of the request before it's forwarded. This may make sense for a cache to work as intended, but since I don't want any caching it's quite frustrating to have to make sure to white-list all the headers I ever use. It would feel much better to rely on "All" than having to make sure the white-list is always up to date.

So:

1) is there a better, clearer way to disable caching completely for one path of a CloudFront distribution? Ideally it shouldn't even rely on the origin setting certain headers, it should just completely disable any attempt to cache requests within the configured path.

2) Why is my entire API gateway target disabled when I select "All" in the "Cache based on headers" box? What's happening here?

like image 870
JHH Avatar asked Oct 08 '18 14:10

JHH


People also ask

How do I disable cache in API?

Just use the Cache-Control: no-cache header. Implement it as delegating-Handler and make sure your header is applied (with MS Owin Implementation hook up on OnSendingHeaders() .

Can CloudFront cache API response?

Cloudfront will cache the json response based on your cache control headers. However, you'll likely have to deal with cross domain issue if your single page app is not served from api.yourdomain.com. Cloudfront supports OPTIONS request which means it should be able to support CORS.

How do I invalidate AWS API gateway cache?

To invalidate an existing cache entry of a request and retrieve the latest data from the integration endpoint, one must send the request together with the Cache-Control: max-age=0 header.

Should I put CloudFront in front of API gateway?

Therefore, it's a best practice to place the CloudFront Distribution in front of the API Gateway and then enable protection on that distribution. Caching definitely could be a reason to use CloudFront with API Gateway.


1 Answers

Just had the same issue and ended up contacting AWS help on it.

Based on the AWS associate, the reason why "Cache based on headers = All" doesn't work on API gateway is that, forwarding of "Host" header makes it non-applicable.

The way that it worked for us is by setting the TTL on the API gateway behavior to zero, for both Max and min value.

like image 93
Jibin Liu Avatar answered Oct 18 '22 12:10

Jibin Liu