serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

SAM Api Gateway cache with queryStringParam and PathParam

Open IstvanSzilagyi opened this issue 5 years ago • 4 comments

Description: I would like to enable chaching for the API Gateway which distinguish requests based on QueryStringParameters and RequestParameters/PathParams, I was able to enable cache for the ServerlessRestApi but for some reasion doesn't matter what i do it just ignores the params defined. At this point im not even sure if this is an issue or bug, but this would be nice to know/have a feature where i could just simply define my Methods in the global section of a cloudformation template, and would include params(both query and path params) in caching.

I also made a stack overflow question regarding this, for more details please check: https://stackoverflow.com/questions/57907320/aws-enable-caching-with-querrystringparameter-pathparameter-for-sam-api-gateway

Example Yaml template

`AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
  Api:
    EndpointConfiguration: REGIONAL
    CacheClusterEnabled: true
    CacheClusterSize: "0.5"
    MethodSettings: 
      - CachingEnabled: true
        CacheDataEncrypted: true
        CacheTtlInSeconds: 60
        HttpMethod: "*"
        ResourcePath: "/*"
     - ResourcePath: "/~1item~1/~1{itemCode}"
        CachingEnabled: true
        CacheDataEncrypted: true
        CacheTtlInSeconds: 60
        HttpMethod: "*"
Resources:
......
  GetItem:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: GetItem.handler
      Runtime: nodejs8.10
      Timeout: 20
      CodeUri: "codes"
      Events:
        GetItem:
          Type: Api
          Properties:
            Path: /item/{itemCode}
            Method: get
......`

Observed result: Caching not enabled for params thus returning incorrect response

Expected result: Enable caching for params and distingush requests based on the params.

IstvanSzilagyi avatar Sep 13 '19 12:09 IstvanSzilagyi

@IstvanSzilagyi We have a change about to go out with release v1.15 that addresses this issue in part. Can you take a look at this and see if this would address your issue? It adds cacheKeyParameters to your lambda integration in the swagger.

praneetap avatar Sep 20 '19 17:09 praneetap

Thank you for getting back to me @praneetap Unfortunately this will not resolve everything in my issue, Query Params probably still would be ignored and also i should had to add Headers to the issue also for caching, I see them as missing feature currently from SAM which would be a huge improvement i believe. For now i will try to make a workaround with CF.

IstvanSzilagyi avatar Sep 23 '19 12:09 IstvanSzilagyi

@IstvanSzilagyi We noticed that the request parameters feature is incomplete during our internal QA before releasing this feature. You can follow the thread about it here: https://github.com/awslabs/serverless-application-model/issues/931#issuecomment-534463087

Caching should work with our next release. The following template (from the comment linked above) is a working example that will be available soon in SAM (see our release board):

Globals:
  Api:
    OpenApiVersion: '3.0.1'
    CacheClusterEnabled: true
    CacheClusterSize: '0.5'
    MethodSettings:
      - ResourcePath: /one
        HttpMethod: "GET"
        CachingEnabled: true # required to enable caching
        CacheTtlInSeconds: 15 # optional
Resources:
  ApiParameterFunction:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: |
        exports.handler = function(event, context, callback) {
            var returnVal = "undef";
            if (event.queryStringParameters.type === "time") {
                returnVal = "time " + Date.now();
            }

            if (event.queryStringParameters.type === "random") {
                returnVal = "Random " + Math.random();
            }

            callback(null, {
                "statusCode": 200,
                "body": returnVal
            });
        }
      Handler: index.handler
      Runtime: nodejs8.10
      Events:
        GetHtml:
          Type: Api
          Properties:
            Path: /one
            Method: get
            RequestParameters:
              - method.request.querystring.type:
                  Required: true
                  Caching: true
        AnotherGetHtml:
          Type: Api
          Properties:
            Path: /two
            Method: get

keetonian avatar Sep 27 '19 17:09 keetonian

@keetonian has this feature been completed?

ghost avatar Apr 24 '20 17:04 ghost

Tested the feature and it's working as expect. Closing this issue.

Feel free to re-open the issue if you have any other questions.

xazhao avatar Jan 06 '23 16:01 xazhao