3

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?

2 Answers 2

4

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:

  • Create an Amazon EC2 instance with Amazon Linux
  • Install compiler packages as well as the OpenLDAP developer package. yum install -y gcc openldap-devel
  • Create a virtual environment: virtualenv env
  • Activate the virtual environment: env/bin/activate
  • Upgrade pip (I am not sure this is necessary, but I got a warning without this): pip install --upgrade pip
  • Install python-ldap: pip install python-ldap
  • Create a handler Python script, for example, lambda.py with the following code:

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)

  • Implement your LDAP function, in this example ldap.py:

import ldap

print ldap.PORT

  • Create a zip package, let's say ldap.zip:

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 *

  • Download the zip to your system (or put it into an S3 bucket). Now you can create your Lambda function using lambda.handler as the function name and use the zip file as the code.

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I followed everything step by step and strangely I am getting : Unable to import module 'lambda': No module named lambda. Am I missing something?
0

one more step/check to the solution above: still you might get No module named '_ldap', then check if the python version that you install on local/EC2 are the same as the Runtime on lambda

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.