16

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?

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

What if I need it for a POST request? What should be used to key response into cache?
@KuldeepYadav why would you cache POST request response? shouldn't the POST request create a resource? if you want the caching to handle the duplicate case, maybe create a separate endpoint to check for duplicates, which would be a GET request, and this one can be cached.
Why would AWS require this setting, or even offer it? Different URLs point to different resources - i.e. a different query param points to a different server resource.
4

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: "'*'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.