简体   繁体   中英

AWS API gateway timeouts

I have set up serverless environment on AWS using lambdas and api gateway. I have a script what gets called whenever someone fills up information on contact form. The script itself looks like this:

const rp = require('request-promise')
const sendEmail = require('./sendEmail')

    module.exports.run = async (event, context, callback) => {
      const body = JSON.parse(event.body)
      const { name, email, budget, message, attachment } = body
      if (!name) {
        return callback(null, {
          statusCode: 400,
          body: JSON.stringify({ message: 'Name is required' }),
        })
      }

      if (!email) {
        return callback(null, {
          statusCode: 400,
          body: JSON.stringify({ message: 'Email address is required' }),
        })
      }

      if (!message) {
        return callback(null, {
          statusCode: 400,
          body: JSON.stringify({ message: 'Message is required' }),
        })
      }


      return Promise.all([
        sendEmail({
          to: 'Example <user@example.com>',
          subject: 'Received submission',
          data:
            'Hello'
        }),
        sendEmail({
          to: `${name} <${email}>`,
          subject: 'Subject',
          data:
            'Example text',
        }),

      ])
        .then(() => {
          return callback(null, {
            statusCode: 200,
            headers: {
              'Access-Control-Allow-Origin': '*',
              'Access-Control-Allow-Credentials': true,
            },
            body: JSON.stringify({ message: 'Great success' }),
          })
        })
        .catch(err => {
          return callback(null, {
            statusCode: 500,
            body: JSON.stringify({
              message: 'Oh no :( Message not delivered',
              error: err,
            }),
          })
        })
    }

And this is my sendEmail class

const AWS = require('aws-sdk')

const ses = new AWS.SES()

module.exports = function({ to, subject, data, replyTo }) {
  return ses
    .sendEmail({
      Destination: { ToAddresses: [to] },
      Message: {
        Body: {
          Text: { Charset: 'UTF-8', Data: data },
        },
        Subject: {
          Data: subject,
          Charset: 'UTF-8',
        },
      },
      Source: 'Example <user@example.com>',
      ReplyToAddresses: [replyTo],
    })
    .promise()
}

However it keeps hanging due to the timeout which are limited to five minutes on aws side, is there something i'm missing that it's taking longer than five minutes?

问题是 lambda 位于 eu-west-2,而 SES 设置在 eu-west-1,这意味着它无法联系 SES 中 eu-west-2 中的 api 端点,导致响应 500 .

I am seeing two issues here.

  • you are mixing callback and async syntax of lambda
  • you are mixing promise & async/await syntax

you can simply return the output, instead of of using callback. I have also changed the promise syntax to async/await.

const sendEmail = require('./sendEmail')

module.exports.run = async (event, context) => {
  const body = JSON.parse(event.body)
  const { name, email, budget, message, attachment } = body
  if (!name) {
    return {
      statusCode: 400,
      body: JSON.stringify({ message: 'Name is required' }),
    }
  }

  if (!email) {
    return {
      statusCode: 400,
      body: JSON.stringify({ message: 'Email address is required' }),
    }
  }

  if (!message) {
    return {
      statusCode: 400,
      body: JSON.stringify({ message: 'Message is required' }),
    }
  }

  try {
    await Promise.all([
      sendEmail({
        to: 'Example <user@example.com>',
        subject: 'Received submission',
        data:
          'Hello'
      }),
      sendEmail({
        to: `${name} <${email}>`,
        subject: 'Subject',
        data:
          'Example text',
      }),

    ]);

    return {
      statusCode: 200,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Credentials': true,
      },
      body: JSON.stringify({ message: 'Great success' }),
    }
  } catch (err) {
    return {
      statusCode: 500,
      body: JSON.stringify({
        message: 'Oh no :( Message not delivered',
        error: err,
      }),
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM