Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using async keyword in AWS Lambda

I am playing with AWS Lambda using Node.js. After being tired of having to deal with callbacks I figured I could elegantly use async/await just like I am used to in C#.

exports.handler = async(event, context, callback) => {
    db = await MongoClient.connect(process.env['MONGODB_URI']);
}

Even though this seemingly works when testing offline using lambda-local, it fails miserably when uploaded to AWS. It appears as if async keyword is not recognized. I am using the latest Node.js 6.10 runtime on AWS while my local version is 8.5. Is there a way around or should I abandon this approach and go back to using callbacks?

like image 899
wpfwannabe Avatar asked Oct 12 '17 07:10

wpfwannabe


People also ask

How do you call async function in Lambda?

Use the Lambda console to configure error handling settings on a function, a version, or an alias. Open the Functions page of the Lambda console. Choose a function. Choose Configuration and then choose Asynchronous invocation.

Can Lambda function be async?

Lambda functions can be invoked either synchronously or asynchronously, depending upon the trigger. In synchronous invocations, the caller waits for the function to complete execution and the function can return a value.

Can Lambda call another Lambda asynchronously?

There are multiple ways in which we can do this. Async lambda invocation is the simplest and straightforward way to go about this problem. Async invocation means that the lambda that invokes the other lambda does not wait for the second lambda to finish and immediately returns.

What is the difference between synchronous and asynchronous Lambda invocations?

When you invoke a function, you can choose to invoke it synchronously or asynchronously. With synchronous invocation, you wait for the function to process the event and return a response. With asynchronous invocation, Lambda queues the event for processing and returns a response immediately.


1 Answers

The async/await feature was launched in Node.js v7.0 but was behind the --harmony flag as it was experimental. It was fully supported without the flag after Node.js v7.6.

Hence, you can't use async/await with Node.js v6.10.

Look here to know exactly which versions are supported.

like image 70
Jyotman Singh Avatar answered Sep 23 '22 02:09

Jyotman Singh