1

I've been successfully using Google's or-tools on AWS EC2 instances but have recently been looking into including them in AWS Lambda functions but can't get it to run.

Function debug.py

Below is just a basic function importing the pywrapcp from ortools which should succeed if everything is set up correctly.

from ortools.constraint_solver import pywrapcp

def handler(event, context):
    print(pywrapcp)

if __name__ == '__main__':
    handler(None, None)

Failing Module Import

I created a package.sh script that copies all dependencies to the project following Amazon's instructions before creating a zip archive. Running the deployed code results in this:

Unable to import module 'debug': No module named ortools.constraint_solver

Contents of package.sh

#!/bin/bash

DEST_DIR=$(dirname $(realpath -s $0));

echo "Copy all native libraries...";
mkdir -p ./lib && find $directory -type f -name "*.so" | xargs cp -t ./lib;

echo "Create package...";
zip -r dist.zip debug.py lib;
rm -r ./lib;

echo "Add dependencies from $VIRTUAL_ENV to $DEST_DIR/dist.zip";

cd $VIRTUAL_ENV/lib/python2.7/site-packages;
zip -ur $DEST_DIR/dist.zip ./** -x;

When I copy the ortools folder from ortools-4.4.3842-py2.7-linux-x86_64.egg directly into the project root it finds ortools but then fails to import pywrapcp which may be related to a failure loading the native libraries but I'm not sure since the logs don't show much detail.

Unable to import module 'debug': cannot import name pywrapcp

Any ideas?

4

2 回答 2

2

Googleor-tools上的讨论之后,我整理了一个打包脚本,它解决了以适用于 AWS Lambda 的方式安装依赖项的问题。

其中的关键部分是,必须将 egg 包的内容手动复制到 Lambda 项目文件夹并获得正确的权限,以便在运行时访问它们。

#!/bin/sh

easy_install3 py3-ortools

find "/opt/python3/lib/python3.6/site-packages" -path "*.egg/*" -not -name "EGG-INFO" -maxdepth 2 -exec cp -r {} ./dist \;

chmod -R 755 ./dist

您可以使用 Docker 在本地创建可部署的包,而不是创建和配置 EC2 实例,详情请参阅or-tools-lambda

于 2017-07-10T10:21:21.907 回答
0

首先,底层 AWS Lambda 执行环境是 Amazon Linux,而根据https://github.com/google/or-tools,or-tools未在以下环境之外进行测试

  • Ubuntu 14.04 和 16.04 以上(64 位)。
  • 带有 Xcode 7.x(64 位)的 Mac OS X El Capitan。
  • 带有 Visual Studio 2013 和 2015(64 位)的 Microsoft Windows

通过启动一个带有 aws lambda 在此处列表中使用的 ami 的实例来测试您的代码 ( http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html )

如果可行,请使用 pip 在项目目录的根级别安装依赖项/库,然后 zip。不要手动将库复制到您的项目目录

于 2016-10-19T17:03:43.890 回答