0

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.

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

3rd option: Convert the entire Lambda function to Python. And honestly if the source code in the question is the entire Lambda function, that would be trivial to convert.
No, this is just the sample code I used to check if my python script will run with nodes, I need the lambda function to be in nodejs.
Another option is to keep both as separate functions with different runtimes - Node and Python. Invoke one function from another using lambda api or step functions
@manpreet That's also a good idea. Although I think questions like this come from developers with limited technical expertise and they got a Python script (not Lambda) that someone else wrote which they just want to use without porting it etc. So I am not sure that they would be able to port the Python script to a Python Lambda. But in general, this is another option how to solve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.