带有查询参数的方法的SAM API Gateway缓存

时间:2019-07-01 15:06:35

标签: amazon-web-services lambda amazon-cloudformation aws-api-gateway serverless-application-model

使用以下SAM模板:

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:
              x-amazon-apigateway-integration:
                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: "'*'"

现在/ getData接受查询参数,例如-/getData?path=abcd/efgh,响应为1234

当我使用路径/getData?path=abcd/efgh触发API时,它会正确缓存-响应1234

但是,在我使用不同的查询参数触发api之后-例如/getData?path=uvw/xyz期望响应789返回为第一个请求缓存的响应-1234

如何确保将缓存应用于具有查询参数的路径?

一系列发出的请求及其相应响应的示例:

/getData?path=abcd/efgh-> 1234返回并缓存在11:01:01

/getData?path=uvw/xyz-> 789返回并缓存在11:01:02

/getData?path=abcd/efgh-> 1234在11:01:20从缓存中返回

/getData?path=uvw/xyz-> 789在11:01:31从缓存中返回

编辑

我正在尝试利用RequestParameters,然后将它们映射到CacheKeyParameters,如这两篇文章所述-https://medium.com/@dougmoscrop/i-set-up-api-gateway-caching-here-are-some-things-that-surprised-me-7526d954fbe6https://theburningmonk.com/2016/04/serverless-enable-caching-on-query-string-parameters-in-api-gateway/,但是这两个都使用无服务器框架,我无法弄清楚它如何适合我的模板

1 个答案:

答案 0 :(得分:1)

AWS API Gateway控制台中的最终结果必须显示设置缓存复选框为:

enter image description here

我们可以这样实现:

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:
              # ** Missing param start **
              parameters:
                - name: "path"
                  in: "query"
                  required: "false"
                  type: "string"
              # ** Missing param end **
              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: "'*'"