DianaIonita / serverless-api-gateway-throttling

A plugin for the Serverless framework which configures throttling for API Gateway endpoints.
ISC License
71 stars 7 forks source link

Activated Cloudwatch Logs in Stage is overwritten in endpoint configuration #16

Open patrickwerz opened 3 years ago

patrickwerz commented 3 years ago

Hello, thx for this great plugin. I noticed the problem that when I activate execution logging on the stage level it gets falsely overwritten or not updated on the endpoint level.

custom: apiGatewayThrottling: maxConcurrentRequests: 1000 provider: logs: restApi: level: INFO accessLogging: true executionLogging: true roleManagedExternally: false

here the function

foo: handler: handlers.main.api_proxy.handle events:

  • http: path: v1/send-archive method: POST authorizer: arn: ${cf:${self:custom.baseapplication}-${sls:stage}.CustomAuthorizerArn} type: request throttling: maxRequestsPerSecond: 100 maxConcurrentRequests: 20 timeout: 30

Is this a known Problem? How can I fix it?

Thanks in advance, Patrick

DianaIonita commented 2 years ago

Hi Patrick,

Thanks for raising this. I couldn't replicate the issue. I tried this yaml configuration:

provider:
  logs:
    restApi:
      level: INFO
      accessLogging: true
      executionLogging: true

custom:
  apiGatewayThrottling:
    maxRequestsPerSecond: 10
    maxConcurrentRequests: 5

functions:
  the-endpoint:
    handler: handler.handle
    events:
      - http:
          path: /
          method: GET
          throttling:
            maxRequestsPerSecond: 20
            maxConcurrentRequests: 10

The result is that logging is enabled, and throttling is configured on both the stage and the HTTP endpoint:

image

image

image

Are you using the latest version of the Serverless framework?

patrickwerz commented 2 years ago

Thanks for your reply. I updated serverless framework and this plugin to the latest version. Today I created a new aws environment (serverless created a new stack) and everything worked fine as you described. It won't work for environments where you activate the exectionLogs after the creation (serverless update). Can you reproduce this?

DianaIonita commented 2 years ago

Hi @patrickwerz,

Thanks for bringing this to my attention. You're right about the behaviour of the plugin and I was able to reproduce it.

The issue seems to be around whether an endpoint inherits their settings from the stage, or whether it chooses to override stage settings. When the serverless framework sets logs configuration, it sets it on the stage, and by default, endpoints would inherit the settings for accessLogging and executionLogging. However, this plugin overrides throttling settings for each endpoint that has configured custom throttling settings. For some reason, when custom throttling settings are configured, API Gateway assumes custom settings for logging and metrics should also apply - therefore, they are no longer inherited from the stage. This happens in the UI as well, which gives you either this option: image

or this option: image

Since many other things can configure custom settings for an endpoint (the serverless framework itself, or some other plugins, and at different points in the deployment lifecycle), building a fix into the current workflow of the throttling plugin didn't seem like the right solution. Instead, I added a command in the latest version of this plugin which can reset endpoint custom settings so that they inherit from stage. It can be used like this: sls reset-all-endpoint-settings This will make it so that all endpoints in the project are in this state: image

From here, once you redeploy your project, both throttling and logs configurations should be properly set for each endpoint. Since it's just been built, I would advise you test it out in a lower environment first.