Search code examples
pythonamazon-web-servicesaws-lambdasubprocesspython-3.8

subprocess not working when used inside the lambda layer - [Errno 2] No such file or directory: 'ld'


I'm having issues accessing my custom python module's subprocess. My use case is thus:

Lambda layer clamav.py has below code:

def current_library_search_path():
    ld_verbose = subprocess.check_output(["ld", "--verbose"]).decode("utf-8")
    rd_ld = re.compile(RE_SEARCH_DIR)
    return rd_ld.findall(ld_verbose)

when method called from the lambda function getting below error:

{
  "errorMessage": "[Errno 2] No such file or directory: 'ld'",
  "errorType": "FileNotFoundError",
  "stackTrace": [
    "  File \"/var/task/update.py\", line 33, in lambda_handler\n    clamav.update_defs_from_freshclam(AV_DEFINITION_PATH, CLAMAVLIB_PATH)\n",
    "  File \"/opt/python/clamav.py\", line 114, in update_defs_from_freshclam\n    \":\".join(current_library_search_path()),\n",
    "  File \"/opt/python/clamav.py\", line 44, in current_library_search_path\n    ld_verbose = subprocess.check_output([\"ld\", \"--verbose\"]).decode(\"utf-8\")\n",
    "  File \"/var/lang/lib/python3.8/subprocess.py\", line 415, in check_output\n    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,\n",
    "  File \"/var/lang/lib/python3.8/subprocess.py\", line 493, in run\n    with Popen(*popenargs, **kwargs) as process:\n",
    "  File \"/var/lang/lib/python3.8/subprocess.py\", line 858, in __init__\n    self._execute_child(args, executable, preexec_fn, close_fds,\n",
    "  File \"/var/lang/lib/python3.8/subprocess.py\", line 1704, in _execute_child\n    raise child_exception_type(errno_num, err_msg, err_filename)\n"
  ]
}

python runtime : v3.8 memory: 1024 mb timeout: 15 mins

How can I resolve this issue ? I have tried adding the PYTHONPATH in the env variables. that doesn't work.


Solution

  • This solution has worked for me:

    https://stackoverflow.com/a/77601836/23040740

    I've replaced this:

    fc_env["LD_LIBRARY_PATH"] = "%s:%s" % (":".join(current_library_search_path()), CLAMAVLIB_PATH)
    

    With:

    fc_env["LD_LIBRARY_PATH"] = "%s:%s" % (fc_env["LD_LIBRARY_PATH"], CLAMAVLIB_PATH)