5

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?

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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 :

Comments

3

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/

Comments

2

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

You can use async/await with Typescript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.