Elbert Bae
Elbert Bae

Reputation: 231

AWS Lambda direct invocation sends two immediate responses

I have a lambda function that is triggered via the AWS SDK using lambda.invoke which is sending duplicate requests with the same request ID every single time. Currently running NodeJS with the lambdas in AWS. - AWS SDK version 2.557.0 - NodeJS version 10

I have tried configuring the AWS client to have maxRetries to 0 and httpOptions timeout to 30000 with connectTimeout to 10000.

AWS.config.update({
  maxRetries: 0,
  httpOptions: {
    timeout: 30000,
    connectTimeout: 10000
  }
})

The lambda.invoke is getting called with the following:

const result = await lambda.invoke({
    FunctionName: process.env.INTAKES_FUNCTION_NAME,
    Payload: JSON.stringify(intakesEvent)
  }, function(err, data) {
    if (err) {
      console.log(`Lamda Error: ${JSON.stringify(err, null, 2)}`)
      throw err;
    } else {
      console.log('Lambda Result: ' +  data);
    }
}).promise();

I know for sure that the invocation takes place as I can see the logs in Cloudwatch. However, I see that the logs in the function that calls lambda.invoke only appear once whereas the logs in the lambda function that is invoked occur twice. Request ID that AWS SDK assigns is identical.

I have done some research and concluded that idempotency needs to be addressed, but I am lost as to why this is happening 100% of the time. Cloudwatch and Lambda logs show 0% of errors, nothing is timing out, but two duplicate requests are being triggered using lambda.invoke within a 20 - 50ms difference. Other posts I've come across indicate that they've experienced this with a 10 minute delay between duplicate events.

Has anyone experienced this before?

[Answer] As per Arun K's answer below:

const result = await lambda.invoke({
    FunctionName: process.env.INTAKES_FUNCTION_NAME,
    Payload: JSON.stringify(intakesEvent)
}).promise();

Issue was the callback and promise syntax being mixed causing a double trigger of sorts. Fixing this removed the double immediate retry issue.

Upvotes: 2

Views: 846

Answers (1)

Arun Kamalanathan
Arun Kamalanathan

Reputation: 8593

the only problem i can see is, you are mixing the promise and call back syntax. Once you fix that, you should be able to see the actual error from the lambda.

try {
  const result = await lambda.invoke({
    FunctionName: process.env.INTAKES_FUNCTION_NAME,
    Payload: JSON.stringify(intakesEvent)
  }).promise();
} catch (err) {
   console.log('error: ', err)
}

Upvotes: 3

Related Questions