How to offload cpu heavy code to lambda with Nodejs

Nodejs-2-562x309@2x-op

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.

Nodejs

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

NPM

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:

new_lambda_function_button

You can choose the “Blank template” blueprint.

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

function_name_and_description

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) => {
 context.succeed(
 {
 "lambda_function_name":"convert_stuff",
 "original_data":event.data_to_transform,
 "transformed_data":compute_heavy_task(event.data_to_transform)
 });
};

Create a role from a policy template and name it:

function_handler_and_role

review your settings:

review

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:

{
        "data_to_transform":500
}

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

step3

step4

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

git clone https://github.com/yodeah/offload_to_lambda
cd offload_to_lambda
npm install

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

node app.js

 

Leave a Reply

Your email address will not be published. Required fields are marked *