0

I am trying to call AWS Lambda function from another AWS Lambda function, but lambda function is not being called from another lambda function.

Please find 2 lambda functions below. First Lambda function is calling Another-lambda function but another lambda function is not getting called and not logging the console.log in another-lambda function.

console.log in first lambda function inside promise before lambda.invoke is getting logged but the callback function inside lambda.invoke is not logging any statement.

First Lambda function

var AWS = require('aws-sdk');
var lambda = new AWS.Lambda({ region: 'us-east-1' });

const callAnotherLambdaFunc = async () => {
  return await new Promise((res, rej) => {
    var params = {
      FunctionName: 'Another-Lambda',
      InvocationType: "RequestResponse",
      Payload: '{ "name" : "Alex" }',
      LogType: "Tail",
    };
    
    console.log("Promise") // This is getting logged
  
    try {
      lambda.invoke(params, (err, data) => {
        if (err) {
          console.log("Error ", err).  // This is not getting logged
          context.fail(err);
        } else {
          console.log("TKK Success ", data.payload)   // This is not getting logged
          context.succeed('Another Lambda said '+ data.Payload);
        }
      }); 
    } catch (e) {
      console.log("Exception", e)   // This is not getting logged
    }
  })
}

exports.handler = async (event, context) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Auto-Trading!'),
    };
    
    console.log(event.queryStringParameters.testKey12, " context ", context)    // This is getting logged
    
    callAnotherLambdaFunc();
    
    return response;  // Postman can see Hello from Auto-Trading! from response object.
};

Another-lambda

exports.handler = async (event, context) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Another Lambda!'),
    };
    
    console.log("Another Lambda Event ", event, " Context ", context)  // This is not being logged.
    
    return response;
};

Please let me know what am I missing. Thanks in advance.

2
  • 1
    Does the first Lambda function have Internet connectivity? (Either by not being connected to a VPC, or by using a NAT Gateway?) Commented Aug 2, 2022 at 2:00
  • Just tested First lambda with github.com/mihaerzen/lambda-internet-access-checker/blob/master/… and I am getting the content. So, yes first lambda has internet access. Commented Aug 2, 2022 at 7:41

1 Answer 1

1

Since the callAnotherLambdaFunc function is asynchronous and returns a promise, you should use await when calling it. ex

exports.handler = async (event, context) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Auto-Trading!'),
    };
    
    console.log(event.queryStringParameters.testKey12, " context ", context)    // This is getting logged
    
    await callAnotherLambdaFunc();
    
    return response;  // Postman can see Hello from Auto-Trading! from response object.
};

Also, another way to call the lambda functions that you can use and that I have used is:

var params = {
      FunctionName: 'Another-Lambda',
      InvocationType: "RequestResponse",
      Payload: '{ "name" : "Alex" }',
      LogType: "Tail",
    };
console.log("Promise") // This is getting logged
try {
  return await lambda.invoke(params, (err, data) => {
      if (err) {
        console.log("Error ", err).  // This is not getting logged
      } else {
        console.log("TKK Success ", data.payload)   // This is not getting logged
      }
  }).promise(); 
} catch (e) {
   console.log("Exception", e)   // This is not getting logged
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.