wpfwannabe
wpfwannabe

Reputation: 14897

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?

Upvotes: 5

Views: 3221

Answers (4)

Khashayar
Khashayar

Reputation: 1351

Node.js v8.10 runtime is available in AWS Lambda as of April 2nd, 2018. Follow the link below for more information:

https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/

Upvotes: 3

Noel Llevares
Noel Llevares

Reputation: 16087

You can also write your handler in Typescript which can compile your code to ES5.

You can use async/await with Typescript.

Upvotes: 2

Gabriel Bleu
Gabriel Bleu

Reputation: 10224

You can bundle your lambda with webpack and babel to write node 8 code and deploy node 6 compatible code.

The easiest way to do this is to use the serverless framework with plugins like :

Upvotes: 5

Jyotman Singh
Jyotman Singh

Reputation: 11350

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.

Upvotes: 6

Related Questions