DianaIonita/serverless-api-gateway-caching

Caching Settings Not Overriding Parent Settings

Closed this issue · 4 comments

Hello, am trying to configure API Gateway caching using two endpoints, / which is cached, and /admin/, which is uncached.

I have tried two different ways:

    events:
      - http:
          path: /
          method: get
          cors: true # <-- CORS!
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.querystring.country
              - name: request.querystring.siteCode
              - name: request.querystring.slug
              - name: request.path.id
      - http:
          path: /admin/
          method: get
          cors: true # <-- CORS!
          authorizer: aws_iam

or

    events:
      - http:
          path: /
          method: get
          cors: true # <-- CORS!
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.querystring.country
              - name: request.querystring.siteCode
              - name: request.querystring.slug
              - name: request.path.id
      - http:
          path: /admin/
          method: get
          cors: true # <-- CORS!
          authorizer: aws_iam
          caching:
            enabled: false

Either way, the resulting deployment does not override the / method
Screen Shot 2020-10-02 at 10 25 35 AM

Is this the desired effect, and is there any way to get around this, other than converted them to sibling endpoints (aka /admin and /not-admin?

Hi @bjclark13,

Thanks for raising this issue.
Make sure you're also configuring caching at the root level:

plugins:
  - serverless-api-gateway-caching

custom:
  apiGatewayCaching:
    enabled: true

If you're still having trouble, please paste as much of the serverless.yml configuration as you're comfortable sharing, and I can have a closer look.

Hi @DianaIonita I do have caching enabled in the root, and caching is working, it's just that the /admin/ resource is being cached when it shouldn't.

Here's my serverless.yml

service: pages2020

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage,'dev'}
  region: us-east-1
  environment:
    PAGE_TABLE: ${file(../global-variables.json):basePath.pages2020.table.${self:provider.stage}} 
    LAYOUTS_TABLE: layouts 
    WIDGETS_TABLE: widgets 
    STAGE: ${self:provider.stage}
functions:
  getPage:
    handler: handler.getPage
    events:
      - http:
          path: /
          method: get
          cors: true # <-- CORS!
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.querystring.country
              - name: request.querystring.siteCode
              - name: request.querystring.slug
              - name: request.path.id
      - http:
          path: /admin/{id}
          method: get
          cors: true # <-- CORS!
          authorizer: aws_iam
      - http:
          path: /{id}
          method: get
          cors: true # <-- CORS!
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.querystring.country
              - name: request.querystring.siteCode
              - name: request.querystring.slug
              - name: request.path.id
      - http:
          path: /admin/
          method: get
          cors: true # <-- CORS!
          authorizer: aws_iam
  postPage:
    handler: handler.postPage
    events:
      - http:
          path: /admin/
          method: post
          cors: true # <-- CORS!
          authorizer: aws_iam
  deletePage:
    handler: handler.deletePage
    events:
      - http:
          path: /admin/
          method: delete
          cors: true # <-- CORS!
          authorizer: aws_iam
plugins:
  - serverless-prune-plugin
  - serverless-domain-manager
  - serverless-plugin-typescript
  - serverless-api-gateway-caching

custom:
  customDomain:
    domainName: ${file(../global-variables.json):domainName}
    basePath: ${file(../global-variables.json):basePath.pages2020.${self:provider.stage}} # This will be prefixed to all routes
    stage: ${self:provider.stage}
    createRoute53Record: true
  prune:
    automatic: true
    number: 3
  cachingState:
    prod: true
    dev: false
  apiGatewayCaching:
    enabled: ${self:custom.cachingState.${self:provider.stage}}
# you can add CloudFormation resource templates here
resources:
  Conditions:
    IsDev:
      Fn::Equals:
        - ${opt:stage}
        - dev 
  Resources:
    PagesTable:
      Type: 'AWS::DynamoDB::Table'
      Condition: IsDev
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: site_code
            AttributeType: S
          -
            AttributeName: slug
            AttributeType: S
        KeySchema:
          -
            AttributeName: site_code
            KeyType: HASH
          -
            AttributeName: slug
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 10
          WriteCapacityUnits: 1
        TableName: odbx_pages-dev

Hi @bjclark13,

Thanks for sharing, it's indeed a bug.
The issue was that the path ended in forward slash, /admin/. It seems API Gateway's updateStage function that this plugin uses doesn't match the correct HTTP method and doesn't throw an error either.

I've created a new release which fixes this bug by removing the forward slash when calling API Gateway to update cache settings. If you update to serverless-api-gateway-caching@1.5.1, the problem should be fixed.
Please let me know if you're having any more issues.

Thanks @DianaIonita!