0
votes

I'm using Amazon API Gateway to execute a Lambda function when the API endpoint is called. In my Lambda function I'm updating a DynamoDB table.

Whenever I call the API with caching disabled using Chrome Developer Tools, the DynamoDB table is updated.

When I have caching enabled, the first request from my API updates the table, every subsequent request is much faster but doesn't update the table.

I'm assuming that CloudFront is caching the responses so as to not have to call the Lambda function each time.

Is there any way to force the Lambda function to be executed with each request?

1
The entire point of caching is to not call the Lambda function. It sounds like you don't actually want caching enabled. - Mark B
@MarkB I don't want caching but CloudFront is added automatically when you add a custom domain name. - Francis
Ah, I misread your question. First make sure you have caching disabled in the API settings. If you have that disabled then CloudFront won't cache your API responses. It sounds like your actual issue is caching in the browser. What is the cache-control header value in your API responses? - Mark B
By default it's cache-control: private, when I use Chrome Developer Tools it's cache-control: no-cache. - Francis
I think you're correct, it's just the browser cache. I thought CloudFront was responding to the cache-control header. - Francis

1 Answers

2
votes

Few possible solutions:

  1. CloudFront should be used only when u want caching. In this case you don't need it; so call API endpoint directly from browser instead of calling CF end point. This will also save your cloudfront cost.

  2. With each request add a timestamp.

  3. If you have to use CF; you can configure it very easily as to what requests should ALWAYS go to API end points ( which serves dynamic content) while which one should be cached.

  4. Probably you are calling CF as a GET request; just make it POST which is NEVER cached. Ideally, as you are updating table it should be a POST request. This should be simplistic solution with minimal and right changes.