cachingaws-lambdaaws-api-gatewayaws-samserverless-application-model

AWS enable caching with queryStringParameter PathParameter for SAM API Gateway


I would like to enable caching for API Gateway for my serverless functions, but having hard time to understand where to do it and what way.

I have tried to set up queryStringParameters in my serverless functions but that results in error, also tried to add them under my GLOBAL Api but no luck (also would prefer avoid doing this in global)

Also checked my resources in the API Gateway and caching is disabled for RequestParams and QueryStringParams are missing from there.

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: "/*"
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
......

***********************************EDIT*********************************

Found out if API Gateway does not know about the params then it will ignore it for caching https://forums.aws.amazon.com/thread.jspa?messageID=915838&#915838

I have tried to add multiple methodSetting entries to the template, and it seems the CF dont ignore it, but still the same result. Also im not sure how to do the same for queryStringParameters if possible.

  - ResourcePath: "/~1item~1/~1{itemCode}"
    CachingEnabled: true
    CacheDataEncrypted: true
    CacheTtlInSeconds: 60
    HttpMethod: "*"

***********************************EDIT*********************************

I would prefer a way to enable the caching for the RequestParams and QueryParams under every resource aka 'AWS::Serverless::Function'

Your help is much appreciated.


Solution

  • At this point there is no support from SAM framework to do this. They are planning to release an update where they enable the function more on this link: https://github.com/awslabs/serverless-application-model/issues/1140

    Until then the only solution i was able to come up os to create a CloudFront distribution in front of the API gateway, its kind of waste of resource but it works nicely.