Using lambda functions asynchronously

We have a legacy app that goes through a number of database processing tasks, where each task must complete before the next one can start and these tasks can take an unknown amount of time. We’d like to create lambda functions for each of these steps and have them execute their SQL statements asynchronously. Basically, invoke the SQL statement and end as the function doesn’t need to sit around and wait for it to complete. We can’t figure out how to signal the next step since the calling function has ended. Is there a best practice around this? We looked at step functions but it didn’t seem that would do the job either.


Related discussions:

Help me understand a bit better. We issue the long-running SQL statement from lambda and then the function ends. The SQL statement will continue to run until it’s completed. The step function would make the lambda call but how does the step function know when the SQL statement has completed? Do we need to have the SQL statement signal completion somehow?

i think it mostly comes down to the database you are using. most databases will cancel the process if the connection breaks. there used to be asynchronous ways to do stuff, but it depends on the actual server software. in sybase you have events, in mssql you have jobs or something.

another way is to simply put the client program on the server box, in form of a shell script or python or anything you’d like. of course needs access to the box, and proper privileges, etc. i’d say it is a dirty solution, but nevertheless its there. this way, you can start the client tool, and then disconnect the session no problem.

I’m not really sure if it’s applicable to your usecase, but you’d wind up with a Lambda function that checks the progress of the SQL statement periodically (say, every 5 seconds). The step function then executes the wait Lambda over and over again until it finds the successful completion, and then goes to the Lambda to process the data.

Here’s an example: https://docs.aws.amazon.com/step-functions/latest/dg/sample-project-job-poller.html

In the SQL statements used to run the data processing, before you start the process, insert a record in a different table saying the process has started. When the sql is complete, mark it as complete in the same table. Have a lambda in step function check the table periodically to see if any statements are complete, then proceed with next step.

We kinda thought that this was probably the cleanest way to do it but wondered if there was something we didn’t know about lambdas or some other AWS technology that could detect completion.

Unless you exactly know the invocation time will always be less than 15 minutes i would use a t3a.nano instance instead which costs about 2 dollar per month if you buy it reserved. You are just invoking a SQL command, nothing special with using much CPU or RAM.

Then have a dynamo db table which stores: name of command, id of coammand, (sql command itself? depends on size, 4k max size in dynamodb), status( start, stop, interrupt, terminated, error, …).

Use SQS or other decoupling to trigger the ec instance to invoke that sql command. ec2 writes to dynamodb entries on start, on error, on termination or timeout, on finish. EC2 will only process next SQS once the last message was finished sucessfully.

Best choice - Lambda step functions

(Poor) Alternate bastardization choices:
FIFO SQS with Polling - at the successful completion of the Lambda, enqueue something and poll for it - this is “dirty IPC”

S3 event trigger - at the successful completion of the Lambda, write a file to S3 and leverage S3 event triggers to call the “next” Lambda.

I see the logic of the best choice but no one has been clear how w’d go about it. If we invoke the lambda function with the asynchronous SQL in it, the lambda function will end even though the SQL execution is still running. We don’t want the lambda to continue to run because the charges will continue as it waits for completion.

So when the SQL actually does complete, how does the step function know this so that it can continue on to the next step? The end of the lambda function is not the correct signal as it must wait for the SQL to finish. Is there some other signaling mechanism that we can leverage.

Another person commented that a t2.nano may be the best approach because the instance is simply launching the SQL code and isn’t really doing any other work.

If you’ve defined the Lambda to be finished on initiating the SQL process…then that’s “the end” of Lambda #1.
How you handle the state of SQL outcome pending is an architectural choice (e.g. an entry on a table, a temp table, key value pair in DynamoDB, Parameter Store) that the subsequent step function would query.

You have to decide (with whatever basis you elect: cost, efficiency, latency) and how to manage state. If you’ve decided you don’t want to persist Lambda 1 until completion, then you need to create a place to store and monitor a pending state.

I now see the difference between a SAP and a SAA :wink: Nice, i did not think of those architectures.