3
votes

I am able to call my python from nodejs on AWS Lambda using the below function. However, because I need specific python libraries, I created a virutalenv in the env directory. I zipped everything up and pushed to Lambda. But when I try and call python from the virtual directory I get a Permission Denied error.

I attempted to modify the chmod permissions on Lambda before calling python but got Operation Not Permitted. How can I get this to run?

console.log('Loading event');

var exec = require('child_process').exec;

exports.handler = function(event, context) {

    exec('env/bin/python district.py \'' + JSON.stringify(event) +   '\'',  function(error, stdout) {
        var obj = stdout.toString();
        context.done(error, obj);
    });
};

Here's the error:

{
  "errorMessage": "Command failed: /bin/sh: env/bin/python:    Permission denied\n",
  "errorType": "Error",
  "stackTrace": [
    "",
    "ChildProcess.exithandler (child_process.js:658:15)",
    "ChildProcess.emit (events.js:98:17)",
    "maybeClose (child_process.js:766:16)",
    "Process.ChildProcess._handle.onexit (child_process.js:833:5)"
  ]
}
2

2 Answers

0
votes

Try this out:

exec('python district.py "'+ JSON.stringify(event) +'"', function(error, stdout) {
    console.log('Python returned: ' + stdout + '.');
    context.done(error, stdout);
});

Amazon has a tutorial on using Python in Lambda here

0
votes

The error most likely signals that python.exe does not have the executable bit set. Note, however, that even if you set the x bit, it won't work: .exe files are Windows executables, and they won't work.

Note, this virtual env was created in windows. I also attempted from Linux in the i.e. env/bin/python district.py with no help.

env/bin/python is the correct command. If you still get the Permission Denied error, the it means that the file python is missing the executable bit.

In the AWS Lamba runtime environment, you are not allowed to change permissions of files, nor to change user, therefore you must set the executable bit (or any other permission bit you need) when creating the .zip archive.

To sum up:

  1. On Linux machines, use Linux executables.
  2. Set the executable bit of the executables before creating archive.