juanjoDiaz / serverless-middleware

Serverless plugin to allow middleware handlers configured directly in serverless.yaml
MIT License
18 stars 11 forks source link

Serverless stops working after adding middleware (only in production) #34

Closed ThiagoMaia1 closed 2 years ago

ThiagoMaia1 commented 3 years ago

serverless.yml has this configuration:

    - serverless-middleware # Must stay before serverless-offline and serverless-webpack
    - serverless-webpack
    - serverless-offline
    - serverless-prune-plugin

custom:
    middleware: 
        pre: 
            - src/common/request_middleware/index.beforeRequest
        pos: 
            - then: src/common/request_middleware/index.afterRequest
            - catch: src/common/request_middleware/index.errorHandler

It works well when running locally, but when running in production, all api requests return a status code 500, Internal Server Error. What might be the cause?

juanjoDiaz commented 3 years ago

I have absolutely no idea with the given info 😅

What does the error say? What do the logs say?

ThiagoMaia1 commented 3 years ago

Sorry, on cloudwatch I get this for all functions:

ERROR   Uncaught Exception  
{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'flomatika-api-production-settings'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'flomatika-api-production-settings'",
        "Require stack:",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:999:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
        "    at Module.load (internal/modules/cjs/loader.js:863:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:702:16)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}

The deployment logs seem all right:

Serverless: Middleware: setting 4 middlewares for function flomatika-api-production-settings

Entrypoint .middleware/flomatika-api-production-settings = .middleware/flomatika-api-production-settings.js .middleware/flomatika-api-production-settings.js.map

ThiagoMaia1 commented 3 years ago

I created a test endpoint and applied middleware only to it. The rest didn't break, which means the plugin is not breaking everything, but the test endpoint had the same error:


    test-middleware:
        handler: 
            - src/common/request_middleware/index.beforeRequest
            - src/organization-settings/middleware_test.getEverything
            - then: src/common/request_middleware/index.afterRequest
            - catch: src/common/request_middleware/index.errorHandler
        memorySize: 1792
        events:
            - httpApi:
                  path: /middleware-test
                  method: GET
                  authorizer: ${file(./serverless/authorizer.js)}
{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'flomatika-api-production-test-middleware'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'flomatika-api-production-test-middleware'",
        "Require stack:",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:999:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
        "    at Module.load (internal/modules/cjs/loader.js:863:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:702:16)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}
juanjoDiaz commented 3 years ago

The error seems clear: the package flomatika-api-production-settings can't be found. This most likely has nothing to do with the plugin and you are just missing it in the package.json file.

ThiagoMaia1 commented 3 years ago

The error seems clear: the package flomatika-api-production-settings can't be found. This most likely has nothing to do with the plugin and you are just missing it in the package.json file.

Hi, @juanjoDiaz, thanks for trying to help.

So, the package flomatika-api-production-settings is just my lambda... It exists and works when I remove the middleware configuration, that seems to happen to all 118 lambdas on the api in prod. When I set up only one endpoint to have middleware, then only that endpoint gets this error, and only in prod.

I would guess it is related to the fact that with the plugin, we get a path .middleware/something. Seems to me that when the path for the function wrapped with middleware is set up, then the path to the actual function (which is used within the wrapped function) becomes unrecognized.

ThiagoMaia1 commented 3 years ago

https://github.com/juanjoDiaz/serverless-middleware/issues/33 This issue has a similar error, in his case it was caused by having the plugins in the wrong order.

ghost commented 3 years ago

I'm having a similar issue with this using sls offline start As suggested in this answer solved -parcially- the issue, but when deploying to production, same error occurs again ServerlessError: Configuration error at 'functions['get-samples-sample'].handler': should be string

juanjoDiaz commented 3 years ago

Which version of serverless are you using? That last issue seems to point towards a validation issue by the serverless framework. It expects the property handler to be a function. However, this plugin overrides it to be also array.

I've just released v0.0.15 which changes the plugin so all middlewares must be declared in the custom.middleware section. Can you please test?

ghost commented 2 years ago

Will do, I'll come back later

juanjoDiaz commented 2 years ago

Closing since there was no response. Feel free to reopen if you feel that it's needed.