juanjoDiaz/serverless-middleware

Plugin not working in AWS

Closed this issue · 8 comments

Hi guys, i am experimenting issues in AWS that i dont have in local (running serverless offline). When i hit an endpoint with middleware in local, everything works as expected, not the case in AWS, that i got 502 Bad Gateway and :

{ "message": "Internal server error" }
Looking at the cloudwatch service in the lambda function i see this:

Bad handler .middleware/remoteIndex.handler
This is my lambda function, basically all of them return a promise:

remoteIndex:
  handler: 
    - api/controllers/AuthController.authorize
    - api/controllers/advanced/SiteOptionsController.remoteIndex
    - catch: api/controllers/ErrorController.unauthorize
  events:
    - http:
        path: api/siteOptions/

And the generated remoteIndex.js

const api_controllers_AuthController = require('../api/controllers/AuthController');
const api_controllers_advanced_SiteOptionsController = require('../api/controllers/advanced/SiteOptionsController');
const api_controllers_ErrorController = require('../api/controllers/ErrorController');

module.exports.handler = async (event, context) => {
  let end = false;
  context.end = () => end = true;

  const wrappedHandler = handler => prev => {
    if (end) return prev;
    context.prev = prev;
    return handler(event, context);
  };

  return Promise.resolve()
    .then(wrappedHandler(api_controllers_AuthController.authorize.bind(api_controllers_AuthController)))
    .then(wrappedHandler(api_controllers_advanced_SiteOptionsController.remoteIndex.bind(api_controllers_advanced_SiteOptionsController)))
    .catch(wrappedHandler(api_controllers_ErrorController.unauthorize.bind(api_controllers_ErrorController)));};

If you guys could take a look to this issue.

Hi @sebastianDejoy ,

The error seems to indicate that .middleware/remoteIndex.handler is not valid. But it's being generated properly based on what you copied above.
Are you using some include/exclude rules that might be excluding the .middleware folder when packaging?

Hi @juanjoDiaz, yes, it's confirmed because i exported the Lambda function and .middleware is in there. these are my include/exclude rules:

**update: ** update the name of remoteIndex into remoteGetAllSiteOptions all over the code but the issue persists

package:
  exclude:
    - ./**
  include:
    - ./.middleware/remoteGetAllSiteOptions.js
    - ./api/**
    - ./lib/**
    - ./node_modules/**

Solved!, According to the documentation for renaming .middleware folder, i set the following:

custom:
  middleware:
    folderName: middleware  # defaults to '.middleware'
    cleanFolder: false            # defaults to 'true'

And include all of the files inside the middleware folder (instead of .middleware)

package:
 exclude:
   - ./**
 include:
   - ./middleware/**
   - ./api/**
   - ./lib/**
   - ./node_modules/**

Many thanks for the support!

Good that you solved it. Although it's weird that it works with middleware and no with .middleware.

Maybe replacing - ./.middleware/remoteGetAllSiteOptions.js by - ./.middleware/** would have also worked?

Hi ! i tried with ./.middleware/** but did not work. The only way i get to make it work was renaming the middleware folder into /middleware/ (no dots)

Weird...

Are you using the latest version of the plugin? In previous versions, it used to be _middleware.
Although that shouldn't be because you said that the .middleware folder is in your exported zip.

It would be good to find out what's happening here...

In my package.json i have serverless middleware with version number 0.0.6.

¯_(ツ)_/¯ No idea here man...

If I ever manage to reproduce it I'll fix it 🙂