3 Common Challenges using Javascript / Node.js in AWS Lambda

Mitoc Group
hello JS
Published in
3 min readJun 13, 2016

--

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume — there is no charge when your code is not running.

We, at Mitoc Group, have been working with this service since AWS officially launched it in January 2015. In this article, we’d like to emphasize several common challenges using Javascript / Node.js in AWS Lambda.

Callbacks Are Executed Asynchronously

If you are only getting started with AWS Lambda and you are trying to use Node.js as runtime, you might run into a problem as it often happens that we, as developers, forget or misunderstand the context of this cloud service. As Tim Wagner from AWS Lambda points out in his blogpost:

For those encountering nodejs for the first time in Lambda, a common error is forgetting that callbacks execute asynchronously and calling context.done() in the original handler when you really meant to wait for another callback (such as an S3.PUT operation) to complete, forcing the function to terminate with its work incomplete.

For example, the below code will definitely be forced to terminate before any callbacks are finished executing:

https://stackoverflow.com/questions/28449363/why-is-this-http-request-not-working-on-aws-lambda

Instead, context.done() must be moved inside the callbacks, and then you must wait for the response, as shown below:

https://stackoverflow.com/questions/28449363/why-is-this-http-request-not-working-on-aws-lambda

Functions Can Be Invoked Asynchronously (or Not)

Evolving on the same topic, functions can be invoked directly from other functions. This is very handy especially because AWS Lambda is constrained by design to 5 minutes execution time and 1.5G of memory, so breaking down functionality into smaller pieces and triggering them from each other is vital.

The privilege of running Node.js in AWS Lambda gives you the power of invoking asynchronous executions. For example:

https://stackoverflow.com/questions/31714788/can-an-aws-lambda-function-call-another

Note: AWS Lambda allows both synchronous and asynchronous invocations, as described in this walkthrough:

The preceding invoke command specifies RequestResponse as the invocation type, which returns a response immediately in response to the function execution. Alternatively, you can specify Event as the invocation type to invoke the function asynchronously.

Cloud-Natively Storing and Retrieving Confidential Information

AWS provides cloud-native capabilities to store and retrieve confidential information cost-free. Let’s say we need to use Stripe or PayPal keys in our code, so instead of baking them into our function, we’ll load them into AWS KMS and reuse them in AWS Lambda.

First, we’ll create a KMS key by following the below documentation:

Then, we’ll encrypt our keys and put the results into a file. This can be achieved by using AWS CLI, as shown below:

aws kms encrypt --key-id some_key_id --plaintext "This is the scret you want to encrypt" --query CiphertextBlob --output text | base64 -D > ./encrypted-keys.txt

The encrypted file will be gzipped together with Node.js function and uploaded into AWS Lambda. When triggered, the keys will be decrypted and used for further purposes:

https://stackoverflow.com/questions/29372278/aws-lambda-how-to-store-secret-to-external-api

Bonus: Manage AWS Lambda Functions at Scale

Managing hundreds or thousands of functions in AWS Lambda is another challenge that our team has been continuously working on. We have developed and open sourced DEEP Framework, a full-stack web framework for building cloud-native web applications. We would like to emphasize the Getting Started tutorial, Step 4. Deploy Microservice(s) to Production:

deepify deploy [path_to_micro_application]

This step empowers developers to compile, build and deploy “virtually-unlimited” amount of functions to AWS Lambda service. We use this tool to manage one single web application that implements over a hundred of functions so far. To learn more, please read this blogpost.

As a side note, Mitoc Group is featured by AWS Lambda team as Framework Partner on their website (see below):

https://aws.amazon.com/lambda/partners

Closing Thoughts

In this article we have discussed some of the challenges that developers face when getting started with Javascript / Node.js in AWS Lambda. We have emphasized that callbacks are executed asynchronously, functions can be invoked asynchronously (or not) and developers can store and retrieve cloud-natively confidential information. By no means we think this list of challenges is complete and comprehensive, so if you have been using AWS Lambda or will be using it in the near future, please share your experience in the comments section below.

--

--

technology company focusing on automation using cloud native services with proven track record helping clients migrate to the cloud, establish devops & dataops