aws-lambdapython-ldap

AWS Lambda + Python-ldap


I am trying to use python-ldap with AWS Lambda. I downloaded the tarball from : https://pypi.python.org/pypi/python-ldap

and code to use lambda (lambda_function.py)

from ldap_dir.ldap_query.Lib import ldap

and uploaded the zip to Lambda.

where my directory structure is

ldap_dir -> ldap_query -> Lib -> ldap folder
ldap_dir -> lambda_function.py

Am I missing out something?


Solution

  • python-ldap is built on top of native OpenLDAP libraries. This article - even though unrelated to the python ldap module - describes how to bundle Python packages that have native dependencies.

    The outline of this is the following:

    import os
    import subprocess
    
    libdir = os.path.join(os.getcwd(), 'local', 'lib')
    
    def handler(event, context):
        command = 'LD_LIBRARY_PATH={} python ldap.py'.format(libdir)
        subprocess.call(command, shell=True)

    import ldap
    
    print ldap.PORT

    zip -9 ~/ldap.zip ldap.py
    zip -9 ~/ldap.zip lambda.py
    cd env/lib/python2.7/site-packages
    zip -r9 ~/ldap.zip *
    cd ../../../lib64/python2.7/site-packages
    zip -r9 ~/ldap.zip *

    I hope this helps.