isaura almar
isaura almar

Reputation: 466

Invoke a Lambda function from another lambda is not working

My goal is to Trigger a lambda from another lambda, and I am getting stuck because the 2nd lambda never gets started.

public async Task<bool> CallLambda(string functionName)
{
  var request = new InvokeRequest
  {
    FunctionName = functionName + ":" + LAMBDA_ENVIROMENT,
    InvocationType = InvocationType.RequestResponse,
    Payload = ""
  };

    LambdaLogger.Log("Trigger lambda " + request.FunctionName);
    var lambdaClient = new AmazonLambdaClient();
    await lambdaClient.InvokeAsync(request);
    LambdaLogger.Log("Trigger Done ");

    return true;
}

And here is the Lambda function that needs to be triggered

public const string NAME = LAMBDA_BASENAME + "DeleteHandler";

[Cloudformation4dotNET.Lambda.LambdaResourceProperties(TimeoutInSeconds = 900)]
public void DeleteHandler()
{
   Logger.Log(string.Format("Data from the model " + AnaplanIDs.modelId + "has been deleted"));
   ...
}

When executing the 1rst lambda, the output I am getting is: enter image description here

We can see that it calls the correct lambda name, and it never prints the LambdaLogger.Log("Trigger Done "); and the DeleteHandler never get started

Upvotes: 0

Views: 1036

Answers (1)

isaura almar
isaura almar

Reputation: 466

The problem was that I have not been waiting for the call to be completed. I had

var response = callLambda.CallLambdaAsync(lambdaPayload);

When calling the function that invokes the Lambda, I have solved it by adding it .Result at the end

var response = callLambda.CallLambdaAsync(lambdaPayload).Result;

I tried InvocationType.Event instead of InvocationType.RequestResponse but that was not enough. I have learned that the difference between them is:

  • InvocationType.RequestResponse the current Lambda waits until the invoke lambda is finalized.
  • InvocationType.Event the current Lambda invokes the new one and continue.

Personally, I consider .Event is a better solution in this case since it needs a new lambda to be triggered when the current one finalized—moreover, having a Lambda waiting for another Lambda are not elegant solutions.

Upvotes: 1

Related Questions