Serverless 1.X — enable API Gateway caching on request parameters

Hav­ing pre­vi­ous­ly blogged about the untrod­den path to enable caching on API Gate­way request para­me­ters in the Server­less frame­work 0.5.X, it’s a lit­tle dis­ap­point­ing that it’s still not offi­cial­ly fixed in the 1.X ver­sions…

The Problem

The prob­lem is two-fold:

  1. there’s cur­rent­ly no way to spec­i­fy caching should be enabled for path & query string para­me­ters
  2. the Cloud­For­ma­tion tem­plate Server­less 1.X gen­er­ates for API Gate­way is miss­ing a few option­al fields, these miss­ing fields stop you from man­u­al­ly enable caching in the API Gate­way man­age­ment con­sole too

After you deploy your Lamb­da func­tion with asso­ci­at­ed API, if you go to the man­age­ment con­sole and enable caching on path or request para­me­ters you will get an error say­ing “Invalid cache key para­me­ter spec­i­fied”.

The Workaround

A friend point­ed me to a neat trick to mod­i­fy the Cloud­For­ma­tion tem­plate that Server­less 1.X auto-gen­er­ates for you.

After the project is deployed, you can go to Cloud­For­ma­tion and view the tem­plate that Server­less has gen­er­at­ed. These tem­plates are pret­ty big (and poor­ly for­mat­ted), so I find it eas­i­er to open them up in the Design­er view and use that view to nav­i­gate to the end­point I’m look­ing for.

Once you find the resource tem­plate for the end­point, write down its name. Now go back to the serverless.yml file in your project, and add the resource name to the resources sec­tion at the bot­tom. You only need to include fields that you want to update or add to the tem­plate.

The Cloud­For­ma­tion syn­tax for an API Gate­way method looks like this:

We also need to fill in some blanks for the Integration sec­tion:

For more details on the Cloud­For­ma­tion syn­tax, see here and here.

After some tri­al-and-error, the min­i­mum set of fields I had to add are:

Rede­ploy with Server­less and the path para­me­ter is enabled for caching:

Wrap Up

I hope you have found this post use­ful, though I’m sur­prised by the lack of infor­ma­tion out there dur­ing my research and the lack of offi­cial sup­port from the Server­less frame­work.

You know of a bet­ter way to do this, please let me know in the com­ments.


Like what you’re read­ing? Check out my video course Pro­duc­tion-Ready Server­less and learn the essen­tials of how to run a server­less appli­ca­tion in pro­duc­tion.

We will cov­er top­ics includ­ing:

  • authen­ti­ca­tion & autho­riza­tion with API Gate­way & Cog­ni­to
  • test­ing & run­ning func­tions local­ly
  • CI/CD
  • log aggre­ga­tion
  • mon­i­tor­ing best prac­tices
  • dis­trib­uted trac­ing with X-Ray
  • track­ing cor­re­la­tion IDs
  • per­for­mance & cost opti­miza­tion
  • error han­dling
  • con­fig man­age­ment
  • canary deploy­ment
  • VPC
  • secu­ri­ty
  • lead­ing prac­tices for Lamb­da, Kine­sis, and API Gate­way

You can also get 40% off the face price with the code ytcui. Hur­ry though, this dis­count is only avail­able while we’re in Manning’s Ear­ly Access Pro­gram (MEAP).