Serverless-offline: Running serverless-offline among with serverless-middleware plugin doesn't work

Created on 20 Jan 2021  Â·  4Comments  Â·  Source: dherault/serverless-offline

Bug Report

I'm trying to run serverless-offline using serverless-middleware plugin and I'm getting error during execution.

Current Behavior

According to serverless-middleware documentation:

 Once serverless-middleware is installed you can set the function.handler property to an array. Each middleware handler can be a string (like a standard handler would be) or an object containing the properties then and/or catch.

So I defined the function.handler (getAllUsers.handler) in my case, as an array.

When I'm running sls deploy --stage dev command, everything works as expected. The deployment works great, and the middleware functionality works as expected.

But, When I'm running sls offline start -s dev command, I'm getting the following error:

> sls offline start -s dev

Serverless: Configuration warning at 'functions.getAllUsers.handler': should be string
Serverless:  
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:  

  Serverless Error ---------------------------------------

  No matching handler found for './middleware.middlewareFunc,./user' in 'C:\Users\avishay_a\WebstormProjects\middleware-test'. Check your service definition.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              12.13.0
     Framework Version:         2.7.0
     Plugin Version:            4.1.0
     SDK Version:               2.3.2
     Components Version:        3.2.1

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] serverless-offline-dev: `sls offline start -s dev`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] serverless-offline-dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\avishay_a\AppData\Roaming\npm-cache\_logs\2021-01-19T07_47_15_850Z-debug.log

Sample Code

All files also available at this GitHub Repo

  • file: serverless.yml
service: my-service

plugins:
  - serverless-webpack
  - serverless-offline
  - serverless-middleware

provider:
  runtime: nodejs12.x
  stage: dev

functions:
  getAllUsers:
    handler:
      - ./middleware.middlewareFunc
      - ./user.getFunc
    events:
      - http:
          method: GET
          path: users
  • file: middleware.ts
'import {APIGatewayProxyHandler} from 'aws-lambda';
import 'source-map-support/register';


export const middlewareFunc: APIGatewayProxyHandler = async (event, _context) => {
    try {
        console.log('middlewareFunc');
        return 'Message from middleware';

    } catch (e) {
        return e;
    }

};
  • file: user.ts
import {APIGatewayProxyHandler} from 'aws-lambda';
import 'source-map-support/register';


export const getFunc: APIGatewayProxyHandler = async (event, _context) => {
    try {
        console.log('user getFunc')
        return {
            statusCode: 200,
            body:JSON.stringify({
                message: _context.prev,
                data:[
                    {
                        name:'John',
                        gender:'male'
                    },
                    {
                        name:'Doe',
                        gender:'male'
                    }
                ]
            })
        };

    } catch (e) {
        return e;
    }

};

Expected behavior/code

The expected behavior should be the same as deployment without using serverless-offline plugin.

e.g - real deployment log:

> sls deploy --stage dev

Serverless: Configuration warning at 'functions.getAllUsers.handler': should be string
Serverless:  
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:  
Serverless: Middleware: setting 2 middlewares for function getAllUsers
Serverless: Bundling with Webpack...
Time: 722ms
Built at: 01/19/2021 09:45:36
                         Asset      Size  Chunks                   Chunk Names
    .middleware/getAllUsers.js  1.67 KiB       0  [emitted]        .middleware/getAllUsers
.middleware/getAllUsers.js.map  7.43 KiB       0  [emitted] [dev]  .middleware/getAllUsers
Entrypoint .middleware/getAllUsers = .middleware/getAllUsers.js .middleware/getAllUsers.js.map
[0] external "source-map-support/register" 42 bytes {0} [built]
[1] ./.middleware/getAllUsers.ts + 2 modules 1.42 KiB {0} [built]
    | ./.middleware/getAllUsers.ts 558 bytes [built]
    | ./middleware.ts 247 bytes [built]
    | ./user.ts 642 bytes [built]
Serverless: WARNING: Could not determine version of module source-map-support
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: source-map-support
Serverless: WARNING: Could not determine version of module source-map-support
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service middleware-test.zip file to S3 (290.43 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: middleware-test
stage: dev
region: us-east-1
stack: middleware-test-dev
resources: 11
api keys:
  None
endpoints:
  GET - https://nfd5plc9p4.execute-api.us-east-1.amazonaws.com/dev/users
functions:
  getAllUsers: middleware-test-dev-getAllUsers
layers:
  None

Environment

  • serverless version: v2.7.0
  • serverless-offline version: v6.8.0
  • node.js version: v12.13.0

  • OS: Windows 10

Most helpful comment

something that made it work is using serverless offline start instead of simply serverless offline. Im assuming start is a legacy execution.

All 4 comments

+1

something that made it work is using serverless offline start instead of simply serverless offline. Im assuming start is a legacy execution.

something that made it work is using serverless offline start instead of simply serverless offline. Im assuming start is a legacy execution.

I think the command used in the original question is with “offline”...

didn’t seems to do the trick.
In what version did it help?

We are also looking for a solution

something that made it work is using serverless offline start instead of simply serverless offline. Im assuming start is a legacy execution.

Worked for me, thanks!

serverless versions:

"serverless": "^2.28.0"
"serverless-middleware": "0.0.14",
"serverless-offline": "^6.8.0"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

FranzSkuffka picture FranzSkuffka  Â·  3Comments

adambiggs picture adambiggs  Â·  4Comments

stonebraker picture stonebraker  Â·  3Comments

jormaechea picture jormaechea  Â·  4Comments

ghost picture ghost  Â·  4Comments