Lambda调用未触发第二个Lambda

时间:2020-03-31 21:55:26

标签: node.js aws-lambda invoke

我已经通过类似的线程来解决此问题,但是我没有运气。两个lambda可以彼此独立地触发,我可以通过命令行调用第二个Lambda,但是我的代码不起作用。

'use strict'
/* eslint max-statements: ['error', 100, { 'ignoreTopLevelFunctions': true }] */
const RespHelper = require('../../lib/response')
const { uuid } = require('uuidv4')
const AWS = require('aws-sdk')
const DB = require('./dynamo')
const respHelper = new RespHelper()
const Dynamo = new DB()
const lambda = new AWS.Lambda({
  region: 'us-west-2'
})

const secondLambda = async (lambdaData) => {
  var params = {
    LogType: 'Tail',
    FunctionName: 'second_lambda_name',
    InvocationType: 'RequestResponse',
    Payload: JSON.stringify(lambdaData)
  }
  lambda.invoke(params, function (err, data) {
    if (err) {
      console.log(err)
    } else {
      console.log(`Success: ${data.Payload}`)
    }
  })
}

exports.handler = async event => {
  const id = uuid()
  let bodyData = {
    uuid: id,
    user: 'owner@email.com',
    processingStatus: 'IN_PROGRESS'
  }
  let payloadData = {
    uuid: id,
    user: 'owner@email.com',
    processingStatus: 'COMPLETE'
  }
  try {
    await Dynamo.writeRecordToDB(bodyData)
    await secondLambda(payloadData)
    return respHelper.sendResponse(200, { message: bodyData })
  } catch (err) {
    console.log(`Failure: ${err}`)
    return respHelper.sendResponse(400, { message: 'ERROR' })
  }
}

我已经仔细检查过lambda角色,它对所有资源都具有“调用Lambda”和“调用异步调用”权限。控制台输出未提供任何指示为何无效。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

当您需要等待callback时正在等待promise

const secondLambda = async lambdaData =>
    lambda
        .invoke({
            LogType: 'Tail',
            FunctionName: 'second_lambda_name',
            InvocationType: 'RequestResponse',
            Payload: JSON.stringify(lambdaData),
        })
        .promise()
相关问题