AWS Lambda Node.js 8 support: what it changes for serverless developers

Apr 2, 2018

Node 8 support for AWS Lambda is here!

If you're a serverless developer on Lambda, read on for what you need to know about Node 8. Namely: speed, Async/Await, object rest and spread, and NPX.

Speed

Node 8 is faster. (YES.)

Specifically: about 7% reduction in runtime and 23% reduction in render time (if you're doing server-side rendering in your Lambda function).

You can see the full performance benchmarks in this great post by David Gilbertson.

(Though we must say, if you want maximum speed, Go is still faster.)

Async/Await

With Node 6, if you wanted to execute asynchronous code, you were probably using promises. Honestly, we kind of like promises; they're just straightforward and readable.

Except when you hit a situation where you need to nest 15 rows of them together.

With Async/Await, those nested promises can be no more. Just use keyword await to await a function:

async function wow(x) {
  const a = await meFirst();
  const b = await meSecond();
  const c = await meThird();
}
Simplify your build

If you are using webpack to build your functions to polyfill async/await, you can simply use the native functionality now and simplify your build.

Simplicity for the win!

It's native in Node 8:

export const hello = async (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: `Go Serverless v1.0! ${(await message({ time: 1, copy: 'Your function executed successfully!'}))}`,
      input: event,
    }),
  };

  callback(null, response);
};

(Code snippet from the serverless-nodejs-starter example.)

Big shoutout to the serverless webpack plugin for supporting this long before lambda runtime.

Object rest and spread

You can now spread in parameters into your function, and combine objects together more easily.

Spread can be used instead of Object.assign and/or lodash assign/extend. Both rest and spread help to create a more readable codebase.

Object spread example:

const defaultOptions = {
  foo: true,
  bar: 10,
  zaz: 'hi'
};

const userLandOptions = {
  foo: false,
  bar: 200
};


const combinedOptionsObject = {
  ...defaultOptions,
  ...userLandOptions,
  yolo: true
};

console.log(combinedOptionsObject); // => { foo: false, bar: 200, zaz: 'hi', yolo: true }

👆 Remember yolo === true. Live it up!

For more on Object rest and spread, checkout this post

NPX

If you're running node 8 locally, it comes shipped with NPM version 5.2, which includes NPX:

Npx allows you to run serverless without installing it globally:

npx serverless deploy

If this is your package.json:

{
  "name": "my-serverless-app",
  "version": "0.1.0",
  "description": "blah blah",
  "main": "handler.js",
  "scripts": {
    "deploy": "npx serverless deploy"
  }
}

You can simply run npm run deploy to deploy the function to Lambda; no need to have anything installed globally on your machine.

Check this video for a full rundown.

Further reading:

Subscribe to our newsletter to get the latest product updates, tips, and best practices!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.