Search code examples
amazon-web-servicescachingapi-gatewayaws-api-gateway

How to invalidate the cache of an API gateway based on path parameters and query parameters


I have a GET endpoint that looks like this:

GET /tenant/:tenantId/groups?user=userName  

Now, there is a corresponding POST endpoint as well. When the POST endpoint is used to update groups for the given username, I would like to defeat the cache for this GET endpoint with the username.

POST request could be made by any user and not necessarily just this user so that the front-end would not know to call the API with the

Cache-Control: max-age=0

How do we invalidate the cache of an API gateway based on path parameters and query parameters?


Solution

  • Are you using DynamoDB to store your data?

    If so, you could you use a DynamoDB Stream to trigger a Lambda function that clears the API Gateway cache for that specific record's cache key parameters.

    See this AWS resource if you need to set up an IAM policy or create a signed request. https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html