How to offload cpu heavy code to lambda with Nodejs


The title, says it all, but basically if you have a small service (in this case written with nodejs) running on a server with limited capabilities, you might run into problems if you want to do processor/memory heavy computations. For this can be a solution to offload the work to lambda, which can scale automatically, and you only have to pay for computation, so basically if you rarely need the computation, instead of renting a server you can do this and save heaps of money.

Talk is cheap, lets build it.

First of all if your dont already have you need the following:

Amazon AWS account

We will create the lambda function with this, and our extension of the application will run here.


Node.js is an open-source, cross-platform JavaScript runtime environment for developing a diverse variety of tools and applications.

You can get it here: download nodejs


Its for managing javascript packages, if you install nodejs, you will have this.


You have to log in to AWS and create a new lambda function:


You can choose the “Blank template” blueprint.

Name your function give it a description, a choose the Nodejs environment:


Insert the following code, it gives back a json when it recieves a request, if the json has an attribute named “data_to_transform” it gives back its value squared.

compute_heavy_task = function(data){
 return data*data;

exports.handler = (event, context, callback) => {

Create a role from a policy template and name it:


review your settings:


Now you can test it, with the test settings and you’re supposed to get back a json like:

 lambda_function_name: 'convert_stuff',
 original_data: 500,
 transformed_data: 250000 

if your test request looks like:


So in the second part we are going to create a nodejs app which has AWS auth credentials and can call the previously made function.

But first we have to create the credentials. Go to AWS console / IAM -> Users -> Add user.

welcome_to_iam   step1 step2



Save the credentials as a csv file. After clone the folowoing git repository npm install & run it with nodejs.

git clone
cd offload_to_lambda
npm install

!! edit the conf file, with the neccesary data !!

node app.js