With the state of serverless growing at an exponential rate during the last year, it’s no surprise developers across the globe have flocked to use AWS Lambda as their main serverless compute engine. AWS has a steady track record of covering all the main needs of developers, DevOps engineers and even C-level tech executives. Hell, I’ve been using it for a couple of years, and still haven’t come close to check out even half of their offering.

Disappointment turned to joy

So, after all that, tell me, why haven’t we had the joy of using the newest version of Node.js on AWS Lambda? Strange indeed. The de facto go-to compute service for serverless has limited Node.js developers to use outdated versions of their favorite language sad face. All that changes now, with the public release of a new supported version of Node.js. To be more precise, it’s version **8.10**. We now finally have beloved features at our disposal running natively in the AWS Lambda runtime Praise all things holy!

Out with the old, in with the new

Remember that clunky callback parameter every lambda function has? Here’s some pseudo code showing a generic login function.

Note: Examples are written in Node.js using the Serverless Framework.

// Node.js v.6.10

	module.exports.login = (event, context, callback) => {
	  connectToDatabase() // promise
	    .then(() =>
	      login(JSON.parse(event.body)) // promise
	    )
	    .then(session => callback(null, { // stupid callback :(
	      statusCode: 200,
	      body: JSON.stringify(session)
	    }))
	    .catch(err => callback(null, { // stupid callback :(
	      statusCode: 500,
	      body: JSON.stringify(err)
	    }));
	};

6.10-generic-login.js hosted with ❤ by GitHub

Yeah, well now you don’t need callbacks anymore! With this new Node.js version the Gods of AWS have given us the possibility to leverage Promise chains for lambda handlers. Remember those wrappers you used to write to handle promises? Yes, those… Well, you can throw them out and just return a Promise chain now. Don’t believe me? Check it!

// Node.js v.8.10

	module.exports.login = (event, context) => {
	  return connectToDatabase() // returning promise! :)
	    .then(() =>
	      login(JSON.parse(event.body)) // promise
	    )
	    .then(session => ({ // resolving the promise to an object
	      statusCode: 200,
	      body: JSON.stringify(session)
	    }))
	    .catch(err => ({ // or rejecting with an error
	      statusCode: 500,
	      body: JSON.stringify(err)
	    }));
	};

How amazing is this!? No more fiddling with annoying callbacks. Using Promises natively and chaining them in the same way you’re already used to in your day-to-day life is just a breath of fresh air for serverless developers worldwide.

My life has just become 40% more joyful, just by starting to use Promises in my lambda functions. If you want to know why exactly 40%, hit me with a comment below. I may be joking, just a bit.

#lambda #aws #node.js

AWS Lambda Now Supports Node.js Version 8.10!
1.10 GEEK