4

Overview:

I have some C++ code (myProgram) that I want to execute from a Lambda function. I have built the code on an Amazon Linux EC2 instance, and it runs there without problem. When I try to run it from a Python script in Lambda, I receive the following error message:

Command 'LD_LIBRARY_PATH=/var/task/lib; /var/task/myProgram -d' returned non-zero exit status 126.: CalledProcessError

I see from the Bash Reference Manual that 126 means the command is found but is not executable. It is executable before I upload it to Lambda, so I'm not sure how to resolve this.

Edit: Thanks to the comment below, I double checked the permissions on the Lambda instance. Indeed, the file is not marked as executable after it has been uploaded. When I try changing it with chmod, I get an error Read-only file system.

Details:

When I package this and upload it to my lambda function, the directory structure looks like this:

  • handler.py
  • myProgram
  • lib
    • (required libraries)

Here is the code for handler.py

import os
import subprocess

exepath = os.path.join(os.getcwd(), 'myProgram')
libdir = os.path.join(os.getcwd(), 'lib')

def handler(event, context):
    command = 'LD_LIBRARY_PATH={}; {} -d'.format(libdir, exepath)
    return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)

If I change shell=True to shell=False then I receive a different error

[Errno 2] No such file or directory: 'LD_LIBRARY_PATH=/var/task/lib; /var/task/myProgram -d': FileNotFoundError

Question:

How can I make myProgram executable? I don't see any mention of this process in the Lambda blog on executables

Community
  • 1
  • 1
andreamc
  • 646
  • 1
  • 9
  • 19
  • 2
    The `777` permissins (`rwxrwxrwx`), is that something you confirmed "locally" or on the Lambda server? If not on Lambda, can you convince Lambda to dump the permissions of `myProgram`? Can you dump the user and group of the executable and the process that is trying to execute it? (Theories: (a) "upload to Lambda" messed with permissions, and it isn't executable. (b) there is some security against running globally writable executables on the Lambda system, or executing outside group/user) – Yakk - Adam Nevraumont Jan 10 '18 at 20:05
  • You are right. I checked the permissions locally and assumed they would be preserved, but they aren't. On the lambda instance they are `rw-rw-r--`. Now... how to change that? – andreamc Jan 10 '18 at 20:21

1 Answers1

2

I figured out the solution to this thanks to the tip from Yakk and the answer to Can't run binary from within python aws lambda function.

I had to copy myProgram to /tmp and then chmod to be able to execute the it.

command = 'cp ./myProgram /tmp/myProgram; chmod 755 /tmp/myProgram; LD_LIBRARY_PATH={}; /tmp/myProgram -d '.format(libdir)
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
andreamc
  • 646
  • 1
  • 9
  • 19