尽管返回值,AWS Lambda重试

I have found out that my and my colleagues lambda doesn't return data we anticipate. After what I have already found, we used deprecated invokeAsync which returnes only statusCode. I have upgraded aws-sdk to the newest version and change the invocation code to use invoke with

InvocationType: 'RequestResponse'

因为那应该给我们返回的值。

Lambdas代码本身看起来不错,它是一个不带参数的异步函数。 我在这里不使用回调(handler的第三个参数),因为我们正在做一些异步的事情,并且它需要一个小的重构才能不使用异步/等待,而且我已经读到只返回值也是可以的。 之前的lambda返回数组,但是经过一些调查和谷歌搜索,我将其更改为

return {
      statusCode: 200,
      body: JSON.stringify(result)
    }

where result is the mentioned array, as this was recurring pattern in search results.

总体结果是在我们进行调用的服务中,我们得到了超时,lambda日志表明它正在返回值,但是然后一次又一次地重试自身。

倾销代码

服务调用:

const lambda = new AWS.Lambda({ httpOptions: { timeout: 600000 } })
const results = await new Promise((resolve, reject) => {
    lambda.invoke(
      {
        FunctionName: `lambda-name`,
        InvocationType: 'RequestResponse',
        Payload: '""'
      },
      (err, data) => {
        if (err) {
          reject(err)
        } else {
          resolve(data)
        }
      }
    )
})

Lambda处理程序

export async function handle() {
  // setting db and some adapters

  try {
    // await for some stuff from db, calculate something, store in db and get some info we want to pass to service

    return {
      statusCode: 200,
      body: JSON.stringify(result)
    }
  } catch (error) {
    const errorMessage = 'failed to do task'
    const { message, name, stack } = error
    logger.error(errorMessage, { error: { message, name, stack } })

    return {
      statusCode: 500,
      body: JSON.stringify(new Error(errorMessage))
    }
  } finally {
    await db.close()
  }
}
评论