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

Plugin fails on deployments that doesn't use API Gateway #14

Closed takeda closed 3 years ago

takeda commented 3 years ago

So this is kind of weird request, because why would I need this plugin if I'm not using API Gateway.

What I'm doing is actually using serverless-plugin-ifelse so I can deploy my project in two regions. In one of the regions I'm disabling the API gateway, and only have lambdas running based on events.

The problem is that your plugin tries to be helpful and also exposes ApiGatewayRestApi in the Outputs section. The problem is that on the region where I have API disabled the ApiGatewayRestApi doesn't exist and I'm getting following error:

The CloudFormation template is invalid: Unresolved resource dependencies [ApiGatewayRestApi] in the Outputs block of the template

Is there a way to disable this functionality?

takeda commented 3 years ago

So I figured out a workaround, but it would be nice if that was not needed, since it's ugly.

For anyone else who has this issue I did it by adding the following custom resource:

resources:
  Parameters:
    ApiGatewayRestApi:
      Type: String
_     Default: n/a

Then used serverless-plugin-ifelse to exclude resources.Parameters.ApiGatewayRestApi for normal situations when ApiGateway is available.

DianaIonita commented 3 years ago

Hi @takeda

Thanks for raising this issue. Would you mind providing some of your serverless.yml configuration please? I'm specifically curious how you define regions with the if-else plugin and how you configure throttling using this plugin.

takeda commented 3 years ago

so basically the relevant parts look like this:

[...]
resources:
  Parameters:
    ApiGatewayRestApi:
      Type: String
      Default: n/a

plugins:
- serverless-api-gateway-throttling
- serverless-plugin-ifelse
[...]

custom:
  apiGatewayThrottling:
    maxRequestsPerSecond: 10
    maxConcurrentRequests: 5
  serverlessIfElse:
  - If: '"${self:provider.region}" != "us-east-1"'
    Exclude:
    - functions.API
    - functions.ScanImages
    - functions.PrepareDatabase
    ElseExclude:
    - resources.Parameters.ApiGatewayRestApi

The API function is the one getting the REST requests. ScanImages and PrepareDatabase are just simple lambdas. By excluding these functions the lambdas are not created and in case of functions.API that includes the API Gateway.

DianaIonita commented 3 years ago

Hi @takeda,

Thanks for the extra info. The latest version of the plugin, 1.1.1, should now have fixed this issue. Please reopen if that's not the case.