Ankush
Ankush

Reputation: 71

I want to call a python function from a nodes lambda function in AWS

I have used the spawn module to call a script1.py function from my nodejs export handler but it does not work. The index.js file is as follows-:

exports.handler =  async function(event, context) {
  process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT']
  //this was added as mentioned in the aws documentation
  let dataToSend
const python = spawn('python', ['script1.py']);
  python.stdout.on('data', function (data) {
     console.log('Pipe data from python script ...');
     dataToSend = data.toString();
    });
  console.log("EVENT: \n" + JSON.stringify(event, null, 2)+ dataToSend)
  return dataToSend
}

and the python script1.py is as follows-:

print('hello from python')

I tested it using postman and it gave an internal server error. I want to run a spacy module hence I need to integrate my python code with my nodejs code.

Upvotes: 0

Views: 874

Answers (1)

Jens
Jens

Reputation: 21530

This will not work, because there is no Python installed in the NodeJS runtime.

As far as I can tell, you have three options:

  1. Build a Docker image containing NodeJS and Python and use it with AWS Lambda.
  2. Port the code from the Python Lambda into your NodeJS Lambda.
  3. Create two Lambdas. One Python, one NodeJS and invoke one from the other (thanks manpreet from the comments)

Upvotes: 1

Related Questions