1

I'm trying to call one Lambda function from another one that I have. I set up my permissions so that is not problem.

My problem is that the function doesn't wait for the Invoke function to complete and return NULL all the time.

Here is the code I'm using:

const AWS = require('aws-sdk');

exports.handler = async (event, context, callback) => {

    var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});

    var params = {
        FunctionName: 'testFunction',
        InvocationType: 'RequestResponse'
    }
    lambda.invoke(params, function(err, data){
        console.log(err);
        console.log('here');
    }).promise().then(data=> { callback(null, {message:'done'}); });

};

The {message:'done'} its never shown. I was recommended to use invokeAsync but that function is deprecated by AWS.

I know the problem is that the function is running lambda.invoke as synchronously because if I add callback(null, {message:'done'}); outside of the lambda.invoke function then I can see the console.logs working.

Any help?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Arian LC
  • 23
  • 5

1 Answers1

0

TL;DR - Remove "async" in line 3, and it should work.

Your issue seems to be caused by the async keyword here. I have recreated this and deployed it to Lambda to run on Node v8.10 (but pointing it to invoke one of my own lambda functions of course).

Why are you using "async" here anyway? The async keyword declaration defines an asynchronous function and returns an AsyncFunction object. AWS Lambda is expected a function, not an AsyncFunction, and your "null" result is probably just Lambda immediately giving up because it can't find a regular function. Also, async is almost exclusively used with await (at least is was in 99% of the cases I've seen), and since your code isn't using await at all I don't see any reason to use async either.

Jim
  • 3,821
  • 1
  • 28
  • 60