简体   繁体   English

AWS API 网关超时

[英]AWS API gateway timeouts

I have set up serverless environment on AWS using lambdas and api gateway.我已经使用 lambdas 和 api 网关在 AWS 上设置了无服务器环境。 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这是我的 sendEmail 课程

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?但是,由于超时限制为 aws 方面的五分钟,它一直挂起,我是否遗漏了超过五分钟的时间?

问题是 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您正在混合使用 lambda 的回调和异步语法
  • you are mixing promise & async/await syntax您正在混合使用 promise 和 async/await 语法

you can simply return the output, instead of of using callback.您可以简单地返回输出,而不是使用回调。 I have also changed the promise syntax to async/await.我还将承诺语法更改为 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,
      }),
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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