'problem sending email using sendgrid with serverless-http

when trying to send email by sendgrid the lambda does not show log and ends up giving timeout. I've tried putting it inside a promise, but it didn't help. The initial handler has already been placed asyncronously. The same error also happens when I try to upload on S3. This problem only happens when it is deployed on aws, on serverless-offline it works correctly.

handler.js

import app from './';
import serverless from 'serverless-http';

const handler = serverless(app);

module.exports.handler = async (event, context) => {
  // you can do other things here
  const result = await new Promise(async (resolve, reject) => {
    const resProm = await handler(event, context);
    resolve(resProm);
  });
  // and here
  return result;
};

serveless.yml

functions:
  app:
    name: app-${env:STAGE}
    handler: src/handler.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

sendMail.js

'use strict';

import crypto from 'crypto';
import sgMail from '@sendgrid/mail';

const forgetPassword = async event => {
  try{
    const { email } = event.body;

    const token = crypto.randomBytes(3).toString('hex');
    
    const text  = `<p>Você esqueceu sua senha?</p>
    <p>Codigo de autorização para mudar senha: ${token}</p>
    <p>Se não pediu para recuperar a senha ignore esse e-amil.</p>`;
    
    const msg = {
      to: [<<EMAIL>>],
      from: '[email protected]',
      subject: 'Código para resetar senha',
      text,
      html: text
    }

    sgMail.setApiKey(process.env.SENDGRID_API_KEY);

    const sendMail = await sgMail.send(msg);

    console.log( sendMail);

    return {
      statusCode: 200,
      body: JSON.stringify(
        {
          message: 'Pedido de token enviado, verifique seu e-mail!',
          sendMail
        }
      ),
    }
  }catch(e){
      console.error(e, "forgetPassword EROOR");
  }
  return {
    statusCode: 400,
    error: true,
    body: JSON.stringify(
      {
        message: 'Falha na recuperação da senha!',
        input: event,
      }
    ),
  }
}

export default Auth;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source