Lamba Proxy not working on 1.26.1

Anyone have any issues getting lambda proxy to work? My function works fine when invoked locally and when running serverless-offline, but once I deploy, the function doesn’t return (however the log right before the callback logs out) and there aren’t any errors in Cloudwatch. It just say that the function timed out (at 15 seconds which is plenty long enough).

Any help would be greatly appreciated.

  console.log('EXP: ', exp);  // This logs in cloudwatch

  return callback(null, {
    statusCode: 200,
    headers: {
      'Set-Cookie': `x-jwt=${token}; expires=${exp}`,
      'Access-Control-Allow-Origin' : "*", // Required for CORS support to work
      'Access-Control-Allow-Credentials' : true // Required for cookies, authorization headers with HTTPS
    }
  });
  • serverless.yml
service: wl-login-test

plugins:
  - serverless-webpack
  - serverless-offline

custom:
  webpackIncludeModules: true
  serverless-offline:
    babelOptions:
      presets: ["env"]

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: us-east-1
  timeout: 15

functions:
  login:
    handler: handler.login
    environment:
      SECRET: secret
      TABLE_NAME: tableName
      AWS_PROFILE: profile
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pgPassword
      POSTGRES_DB: dbName
      POSTGRES_HOST: hostName
    events:
      - http:
          path: login
          method: post
          cors: true

Ok, so after playing around, I realized that if I don’t query my database, the function returns as expected. The following code works as expected

const login = async (event, context, callback) => {

  const body = JSON.parse(event.body);
  const email = body.email.toLowerCase();
  const { password } = body

  //const usr = await user.get(email);
  //console.log('USR: ', usr);
  //if (!usr) {
    //callback(null, {
      //statusCode: 401,
      //message: `No user found for email: ${email}`
    //});
  //}

  const response = {
    statusCode: 200,
    body: JSON.stringify({user: 'usr'})
  };
  callback(null, response);  // This returns as expected

however, if I query a user (the commented out code above) the user is found (ie console.log('USR: ', usr) returns), but the callback never fires and the function times out. I’m thinking that maybe the issue is the db connection isn’t being closed. Thoughts?

const login = async (event, context, callback) => {

  const body = JSON.parse(event.body);
  const email = body.email.toLowerCase();
  const { password } = body

  const usr = await user.get(email);
  console.log('USR: ', usr);  // This returns find
  if (!usr) {
    callback(null, {
      statusCode: 401,
      message: `No user found for email: ${email}`
    });
  }

  const response = {
    statusCode: 200,
    body: JSON.stringify({user: usr})
  };
  console.log('Response: ', response) // This logs as well
  callback(null, response);  // This returns as expected

Turns out using context.callbackWaitsForEmptyEventLoop = false saves the day.

2 Likes

Thanks that saved my day too!