AWS API Gateway caching ignores query parameters

 ̄綄美尐妖づ 提交于 2020-05-12 11:55:06

问题


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.

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


回答1:


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




回答2:


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:

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


来源:https://stackoverflow.com/questions/48886715/aws-api-gateway-caching-ignores-query-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!