-1

I would like to execute a Lambda function through API Gateway and within this Lambda function I am invoking 3 other Lambda function and 8 scan queries. The combination of these process will complete my business logic to perform some action and find users to whom I need to send a notification using SNS. The issue is while I am invoking a Lambda function from first Lambda function it will not wait until the inner Lambda function proceed there code. Due to this I could not able to get the answer based on the inner Lambda function and always responding from callback as success.

Mark B
  • 183,023
  • 24
  • 297
  • 295
H Arif
  • 431
  • 4
  • 7
  • 1
    Can you post your code sample? I might have some ideas, but I think a code sample may help clarify what you're trying to do. – oreoluwa Jan 22 '18 at 13:38

1 Answers1

0

It would help to have some code samples, however, I'm guessing you might not be invoking your lambda function properly. Please refer to Invoke an AWS.Lambda function from within another lambda function .

In your case, I think you may be calling the context.succeed function outside the scope of the your lambda invocation.

What I think you might be doing:

exports.handler = function(event, context, callback) {
  const lambdaParams = ...
  const lambdaResponse = lambda.invoke(lambdaParams)

  context.succeed(...)
};

What I think you should be doing:

exports.handler = function(event, context, callback) {
  const lambdaParams = ...
  lambda.invoke(lambdaParams, function(err, data) {
    if (err) {
      context.fail(err); // AWS recommends using the callback instead of context
    } else {
      context.succeed('Lambda_B said '+ data.Payload);
    }
  })
};

UPDATE: Jan 23, 2018 based on a comment received regarding doing it the recommended way by AWS

exports.handler = function(event, context, callback) {
  const lambdaParams = ...
  lambda.invoke(lambdaParams, function(err, data) {
      // if using API gateway, you may want to serialize the data (i 
      // think the error response too), which requires an object
      // containing either/all statusCode, body, header e.g: 
      // {statusCode: 200, header: {}, body: JSON.stringify(data)}
    if (err) {
      callback(err, null)
    } else {
      callback(null, data) 
    }
  })
};
oreoluwa
  • 5,553
  • 2
  • 20
  • 27
  • Why are you not using what AWS recommends? – Noel Llevares Jan 23 '18 at 10:31
  • this was copied over from the answer referenced. I could infact use the recommended approach, but I think what matters from the OP's question is more conceptual, than actual implementation. BTW, I'm updating with the recommended approach too. – oreoluwa Jan 23 '18 at 16:22
  • That's much better. :-) Given that this will become reference for future readers, we should always use the right way. `context.succeed/context.fail` is already deprecated. It still works due to backwards compatibility. – Noel Llevares Jan 23 '18 at 20:21
  • I have tried with both ways but its not working for me.. I have used context.succeed and context.done as well – H Arif Jan 24 '18 at 04:56