AWS Lambda is a great micro-service to run code without managing servers with the scalable solution with few to thousands requests per second. Well, this great service comes with some limitations. One of the big limitation is the 5 Minutes execution time out window. Well, this limitation can be overcome by tweaks. Read more on how to increase lambda execution time

Well, the 5 minutes execution time limitation could be overcome by running the lambda asynchronously. But again it depends on the type of execution flow and what needs to achieve. I had a situation where I required to implement multiple implementation of lambda in parallel and implemented successfully. So, I thought to share.

If you have read my previous article then i have discussed about invoking recurrent lambdas. Well, we will use the same invoke code here. One can also invoke and run the same lambda function asynchronously or call the second lambda from one in parallel. Make sure you assign required permission to IAM role. We will use boto3 library here. Lets say, i want to run single lambda 5 times asynchronously.

invokeLam = boto3.client('lambda', region_name='us-east-1', config=config)

for i in range(5):
    try:
        #defining payload
        payload = {"count": count,
                   "again": True}
        #invoking lambda with payload
        resp = invokeLam.invoke(FunctionName = 'functionname', InvocationType = 'Event', Payload=json.dumps(payload))
    except Exception as ex:
        print(ex)

The above code will invoke lambda asynchronously. Now, the same lambda is invoked 5 times and will run asynchronously. To check whether the lambda is running in parallel, you need to check the AWS CloudWatch logs. It will generate 5 log files for 5 parallel execution of lambda. This is how you can invoke parallel lambdas. Hope, this helps.

Keep sharing. Stay tuned to Tech Tunes.

 

Follow me on Twitter