问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/37018377/aws-lambda-python-ldap