Francis
Francis

Reputation: 15

API Gateway caching not calling Lambda function

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?

Upvotes: 1

Views: 1486

Answers (1)

Deepak Singhal
Deepak Singhal

Reputation: 10874

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.

Upvotes: 3

Related Questions