Using Native Python Libraries in Lambda

It is possible to use native libraries in Amazon Lambda. In this example, we will be building a native bcrypt library against libcrypt, using virtualenv to manage our libraries.

Create an Amazon EC2 Instance to build the native library

Amazon Lambda runs on the Amazon Linux distribution, so we will need to target our compiled libraries against this distribution. Unfortunately, there doesn’t appear to be any sort of debootstrap equivalent for Amazon Linux, so an instance using Amazon Linux will be needed for the build process.

  1. Create an instance using Amazon Linux

  2. Connect to the server

Set up the build environment on the server

We will need to install the build tools for the library we are building, as well as any build dependencies that are required.

<span>$ </span><span>sudo </span>yum update
<span>$ </span><span>sudo </span>yum <span>install</span> <span>-y</span> gcc44 gcc-c++ libgcc44 cmake
<span>$ </span><span>sudo </span>yum <span>install</span> <span>-y</span> python27-devel python27-pip gcc libjpeg-devel zlib-devel gcc-c++
<span>$ </span><span>sudo </span>yum <span>install </span>libffi-devel
<span>$ </span><span>sudo </span>yum update
<span>$ </span><span>sudo </span>yum <span>install</span> <span>-y</span> gcc44 gcc-c++ libgcc44 cmake
<span>$ </span><span>sudo </span>yum <span>install</span> <span>-y</span> python27-devel python27-pip gcc libjpeg-devel zlib-devel gcc-c++
<span>$ </span><span>sudo </span>yum <span>install </span>libffi-devel
$ sudo yum update $ sudo yum install -y gcc44 gcc-c++ libgcc44 cmake $ sudo yum install -y python27-devel python27-pip gcc libjpeg-devel zlib-devel gcc-c++ $ sudo yum install libffi-devel

Enter fullscreen mode Exit fullscreen mode

Update/upgrade pip and setuptools

Make sure you are using the latest version of pip to prevent any issues.

<span>$ </span>pip update
<span>$ </span><span>sudo </span>pip <span>install</span> <span>--upgrade</span> pip setuptools
<span>$ </span>pip update
<span>$ </span><span>sudo </span>pip <span>install</span> <span>--upgrade</span> pip setuptools
$ pip update $ sudo pip install --upgrade pip setuptools

Enter fullscreen mode Exit fullscreen mode

Set up Virtualenv for your project

We will need to package any libraries that the lambda requires along with the lambda. Virtualenv provides us with a convenient method to bundle the libraries along with our lambda function.

Create a directory for your project, and enter it

<span>$ </span><span>mkdir</span> <span>$WORK</span>
<span>$ </span><span>cd</span> <span>$WORK</span>
<span>$ </span>virtualenv venv
<span>$ </span><span>mkdir</span> <span>$WORK</span>
<span>$ </span><span>cd</span> <span>$WORK</span>
<span>$ </span>virtualenv venv
$ mkdir $WORK $ cd $WORK $ virtualenv venv

Enter fullscreen mode Exit fullscreen mode

Activate virtualenv and install libraries

Here we will build and install the libraries into the virtualenv directory, which will make them available for packaging later.

<span>$ </span><span>source </span>venv/bin/activate
<span>$ </span>pip <span>install</span> <span>--upgrade</span> pip
<span>$ </span>pip <span>install </span>bcrypt
<span>$ </span><span>source </span>venv/bin/activate
<span>$ </span>pip <span>install</span> <span>--upgrade</span> pip
<span>$ </span>pip <span>install </span>bcrypt
$ source venv/bin/activate $ pip install --upgrade pip $ pip install bcrypt

Enter fullscreen mode Exit fullscreen mode

Place your lambda function into the working directory.

<span>$ </span><span>cp</span> ~/lambda_function.py <span>$WORK</span>
<span>$ </span><span>cp</span> ~/lambda_function.py <span>$WORK</span>
$ cp ~/lambda_function.py $WORK

Enter fullscreen mode Exit fullscreen mode

Verify the script is working as expected.

Run your lambda and/or tests locally to verify all the libraries are working as expected.

Package the project, according to the lambda requirements.

Here’s the tricky part — Amazon Lambda requires all the libraries to be at the top level in the zip file that you upload. The following will create a zip file with the lambda function and all required libraries at the top level of the zip file. Since virtualenv contains all the libraries that we need for the project, we will copy all the libraries from the virtualenv environment to the zip file.

<span>$ </span>zip <span>-9</span> bcrypt_lambda.zip lambda_function.py
<span>$ </span><span>cd</span> <span>$VIRTUAL_ENV</span>/lib/python2.7/site-packages
<span>$ </span>zip <span>-r9</span> <span>$WORK</span>/bcrypt_lambda.zip <span>*</span>
<span>$ </span><span>cd</span> <span>$VIRTUAL_ENV</span>/lib64/python2.7/site-packages
<span>$ </span>zip <span>-r9</span> <span>$WORK</span>/bcrypt_lambda.zip <span>*</span>
<span>$ </span>zip <span>-9</span> bcrypt_lambda.zip lambda_function.py
<span>$ </span><span>cd</span> <span>$VIRTUAL_ENV</span>/lib/python2.7/site-packages
<span>$ </span>zip <span>-r9</span> <span>$WORK</span>/bcrypt_lambda.zip <span>*</span>
<span>$ </span><span>cd</span> <span>$VIRTUAL_ENV</span>/lib64/python2.7/site-packages
<span>$ </span>zip <span>-r9</span> <span>$WORK</span>/bcrypt_lambda.zip <span>*</span>
$ zip -9 bcrypt_lambda.zip lambda_function.py $ cd $VIRTUAL_ENV/lib/python2.7/site-packages $ zip -r9 $WORK/bcrypt_lambda.zip * $ cd $VIRTUAL_ENV/lib64/python2.7/site-packages $ zip -r9 $WORK/bcrypt_lambda.zip *

Enter fullscreen mode Exit fullscreen mode

Upload the zip to Amazon Lambda.

Now that you have the zip file, upload it to Amazon Lambda as you normally would. Remember to set up the test data for the lambda to verify that the function is working as expected.

原文链接:Using Native Python Libraries in Lambda

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
No matter how complicated your life is, you have to maintain your elegance.
不论生活如何复杂,总要保持自己的那一份优雅
评论 抢沙发

请登录后发表评论

    暂无评论内容