Serverless — enable caching on query string parameters in API Gateway
Since I started working at Yubl less than 2 weeks ago, I have been doing a lot of work with Amazon API Gateway & Lambda with the help of the Serverless framework. So far that experience has been really great.
One little caveat I ran into was that, it wasn’t clear on how to enable caching on query string and request path parameters. For instance, if I declare in my s-function.json file that my API Gateway endpoint has a query string parameter called query:
When the endpoint is deployed, you will see in the API Gateway console that ‘Caching’ is not enabled:
Which means when I enable caching on my deployed stage, the query string parameter would not be taken into account.
Users visiting the following URLs would get the same cached response back:
That’s obviously not OK.
Sadly, the same applied to request path parameters too:
Thankfully, the guys working on Serverless has been very actively in responding to their issues page and one of the team members picked up my ticket and offered some insight on how to make this work.
API Gateway’s own documentation eludes to two Integration parameters called cacheNamespace and cacheKeyParameters. Although, to say they’re poorly documented would be an understatement…
So, after some fiddling around:
- create an API with cached query string param
- deploy it
- export as Swagger + API Gateway Extensions
- inspect the JSON to see what value is outputted for cacheNamespace and cacheKeyParameters
Interestingly, when I put the above value — “method.input.params.query” — in my s-function.json I get the following error during deployment:
Invalid cache key parameter specified
what did work though, is the same value I had specified in my requestParameters dictionary, which is “method.request.querystring.query” in this case. ie
However, applying the same approaches to request path parameters proved fruitless.. I guess we’ll just have to wait for official documentation to be updated to see how it should work, which should be soon by the sound of things!
UPDATE 17/08/2016 : shortly after I posted this I did find a way to get request path parameters working too (which might have been a result of a Serverless update, though I can’t remember now). For request path parameters you need something along the lines of: