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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With