Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway caching ignores query parameters

I'm configuring the caching on AWS API Gateway side to improve performance of my REST API. The endpoint I'm trying to configure is using a query parameter. I already enabled caching on AWS API Gateway side but unfortunately had to find out that it's ignoring the query parameters when building the cache key.

For instance, when I make first GET call with query parameter "test1"

GET https://2kdslm234ds9.execute-api.us-east-1.amazonaws.com/api/test?search=test1

Response for this call is saved in cache, and when after that I make call another query parameter - "test2"

GET https://2kdslm234ds9.execute-api.us-east-1.amazonaws.com/api/test?search=test2

I get again response for first call.

Settings for caching are pretty simple and I didn't find something related to parameters configuration.

enter image description here

How can I configure Gateway caching to take into account query parameters?

like image 257
Gleb Avatar asked Feb 20 '18 13:02

Gleb


2 Answers

You need to configure this option in the Gateway API panel.

  • Choose your API and click Resources.
  • Choose the method and see the URL Query String session.
  • If there is no query string, add one.
  • Mark the "caching" option of the query string.
  • Perform the final tests and finally, deploy changes.

Screenshot

like image 75
Tiago Souza Avatar answered Oct 09 '22 06:10

Tiago Souza


The following is how we can achieve this utilising SAM:

The end result in the AWS API Gateway console must display that the set caching checkbox is:

enter image description here

The *.yml template for the API Gateway would be:

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      CacheClusterEnabled: true
      CacheClusterSize: '0.5'
      MethodSettings:
        - HttpMethod: GET
          CacheTtlInSeconds: 120
          ResourcePath: "/getData"
          CachingEnabled: true
      DefinitionBody:
        swagger: 2.0
        basePath: /Prod
        info:
          title: OutService
        x-amazon-apigateway-policy:
          Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Principal: "*"
              Action: execute-api:Invoke
              Resource:
                - execute-api:/*/*/*
        paths:
          "/getData":
            get:
              # ** Parameter(s) can be set here **
              parameters:
                - name: "path"
                  in: "query"
                  required: "false"
                  type: "string"
              x-amazon-apigateway-integration:
              # ** Key is cached **
                cacheKeyParameters:
                  - method.request.querystring.path
                httpMethod: POST
                type: aws_proxy
                uri:
                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${OutLambda.Arn}/invocations
              responses: {}
      EndpointConfiguration: PRIVATE
      Cors:
        AllowHeaders: "'*'"
like image 3
Igor L. Avatar answered Oct 09 '22 08:10

Igor L.