简体   繁体   中英

How to run AWS lambda functions sequentially

I have few lambda functions and I want to invoke these lambda functions one after other in C#. I created an AWS queue which contains the names of lambda functions in the message body. I used for loop for each message received form queue to execute the function. But my program quits as soon as the first lambda function is executed.

foreach (var message in response.Messages)
{
    Console.WriteLine("For message ID '" + message.MessageId + "':");
    Console.WriteLine("  Body: " + message.Body);

    if (message.Body == "Function1")
    {
        LambdaClient.InvokeAsync(new InvokeRequest
        {
            FunctionName = "Function1",
        }).Wait();
    }

    if (message.Body == "Function2")
    {
        LambdaClient.InvokeAsync(new InvokeRequest
        {
            FunctionName = "Function2",
        }).Wait();
    }
}

In this function, for loop iterates only once and stops executing. Any idea on how to invoke all the functions one after other.

Using SQS queue for invoking lambda functions sequentially is not a good idea. While I think that you might be able to make it work, it is definitely not the right way of doing it and there is a bunch of unnecessary stuff that you will need to handle on your own.

AWS provides you with a very clear way of handling this scenario using state machine which is called Step Functions. Using Step Functions, you can model your sequential flow of lambda executions without any additional operational overhead as well as it provides you with a nice graph where you can see how exactly your flow is being executed and where the error is if it fails.

Lambda functions are not made for this. If you want to invoke another lambda from one, you can use AWS step functions, which you can tune up for sequential invoke (with some workaround and state machines configuration). Pay attention at those Step Functions because they are not made for big data transfer (the maximum size of payload is 1MB if I'm not wrong) and if you need more size you have to use S3 (basically the storage service of AWS).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM