How to run a Python script from a Node.js AWS Lambda Function

Viewed 3562

Running non Node.js executables from a Node.js Lambda Function is possible as described here.

Having a Node.js Lambda Function I want to run a Python script using Node.js' child_process command.

I followed this AWS tutorial and included the following at the start of my .js function:

process.env[‘PATH’] = process.env[‘PATH’] + ‘:’ + process.env[‘LAMBDA_TASK_ROOT’];

And the main Python script call starts here:

const { spawn } = require('child_process');

function calculateSomething(next) {
  var script = spawn('python', ['my_python_script.py', args]);

  script.stdout.on('data', (data) => { doSomething(data); });
  script.on('error', (error) => { console.error(error); });
  script.on('exit', () => { next(); });
}

The Python script runs using an external library (e.g., numpy), so I have to install it using pip.

Now:

  • What do I have to do for installing pip libraries inside a Node.js Lambda?
  • Is it enough for linking my Python script with Node.js Function to add this process.env[‘PATH’] = process.env[‘PATH’] + ‘:’ + process.env[‘LAMBDA_TASK_ROOT’]; at the start of my .js function?

It's similar to this other question.

1 Answers
Related