DianaIonita / serverless-api-gateway-caching

A plugin for the Serverless framework which helps with configuring caching for API Gateway endpoints.
ISC License
136 stars 35 forks source link

Deployment Error when Caching Request Body on POST Method #63

Closed Jordan-Eckowitz closed 4 years ago

Jordan-Eckowitz commented 4 years ago

I'm trying to enable caching on a POST method endpoint based on the request body. When I try and deploy I get the following error (replacing sensitive info with XXXX):

  Serverless Error --------------------------------------- 

  An error occurred: XXXX - Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: method.request.body] (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: a86f8247-9c10-4433-964c-386d334bfd64). 

Here are my configuration settings for the endpoint (replacing sensitive info with XXXX):

  events:
    - http:
        integration: lambda-proxy
        path: apps/XXXX/XXXX
        method: post
        cors: true
        authorizer:
          name: XXXX
          arn: >-
            XXXX
        caching:
          enabled: true
          cacheKeyParameters:
            - name: request.body

Is there something I'm missing?

Jordan-Eckowitz commented 4 years ago

I changed the endpoint to a GET request with query string parameters and its working. Just out of interest, is there a way to get this to work for a POST request?

I've only ever seen examples using GET but according to the AWS docs it can be done.

DianaIonita commented 4 years ago

Hi @Jordan-Eckowitz,

You can define caching on a POST method, however the issue seems to be with cache key parameters.

According to AWS Documentation on request data mapping expressions, it should be possible to use method.request.body or method.request.body.JSONPath_Expression (this plugin simplifies the declaration and omits the method prefix). However, AWS seems to reject CloudFormation templates which use those mappings. I also can't find a way in the API Gateway console to click through and add body or a property on the body as a cache key parameter. Maybe I'm missing something though, so please let me know if it's possible through the UI.

In the meantime, I should update the documentation of the plugin and remove request.body and request.body.JSONPath_EXPRESSION as possible cache key parameters.

Jordan-Eckowitz commented 4 years ago

Hi @DianaIonita,

Thanks for the response and creating this plugin - its saved me a lot of time!

There's a section here called Override API Gateway Stage-Level Caching for Method Caching with details on how to setup custom method caching if an endpoint isn't a GET method. It looks like this would need to be enabled for request body mapping to work.

These are the steps they outline in the docs:

To configure API caching for individual methods using the console:

Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway.

Go to the API Gateway console.

Choose the API.

Choose Stages.

In the Stages list for the API, expand the stage and choose a method in the API.

Choose Override for this method in Settings.

In the Cache Settings area, you can set or clear Enable Method Cache or customize any other desired options. (This section is shown only if stage-level caching is enabled.)
DianaIonita commented 4 years ago

Hi Jordan,

I was looking at that page and I can see options for adding cache key parameters from the query string, header, but nothing about the body:

image

Jordan-Eckowitz commented 4 years ago

You've got to go to the endpoint under the Stages section of the API.

image

DianaIonita commented 4 years ago

Yes, that's already enabled. The issue is with configuring the cache key parameters under Resources.

DianaIonita commented 4 years ago

Feel free to reopen this if you have any more information about the issue.

BorjaMacedo commented 4 years ago

65 Solved this!

fffan64 commented 4 years ago

any news on this ? need caching for post body (entire or some specific keys)