简体   繁体   English

如何使用 AWS Lambda 层使用 Python?

[英]How to use AWS Lambda layer using Python?

I have a simple Lambda function which is using the numpy library,我有一个简单的 Lambda function 使用numpy库,
I have set up a virtual environment in my local, and my code is able to fetch and use the library locally.我在本地设置了一个虚拟环境,我的代码能够在本地获取和使用该库。

I tried to use AWS Lambda's layer, and zipped the venv folder and uploaded to the layer,我尝试使用 AWS Lambda 的层,并将venv文件夹压缩并上传到层,
Then I attached the correct layer and version to my function,然后我将正确的层和版本附加到我的 function,
But the function is not able to fetch the library但是 function 无法获取库

Following is the code which works fine on local -以下是在本地运行良好的代码 -

import numpy as np

def main(event, context):
    a = np.array([1, 2, 3])

    print("Your numpy array:")
    print(a)

Following is the venv structure which I zipped and uploaded -以下是我压缩并上传的 venv 结构 -
在此处输入图像描述

I get the following error -我收到以下错误 -

{
  "errorMessage": "Unable to import module 'handler': No module named 'numpy'",
  "errorType": "Runtime.ImportModuleError"
}

My Lambda deployment looks like this -我的 Lambda 部署如下所示 -
在此处输入图像描述

I'm trying to refer this -我试图参考这个 -
https://towardsdatascience.com/introduction-to-amazon-lambda-layers-and-boto3-using-python3-39bd390add17 https://towardsdatascience.com/introduction-to-amazon-lambda-layers-and-boto3-using-python3-39bd390add17

I've seen that a few libraries like numpy and pandas don't work in Lambda when installed using pip .我已经看到一些库,如 numpy 和 pandas 在使用pip安装时在 Lambda 中不起作用。 I have had success using the .whl package files for these libraries to create the Lambda layer.我已经成功使用这些库的.whl package 文件来创建 Lambda 层。 Refer to the steps below:请参考以下步骤:

NOTE: These steps set up the libraries specific to the Python 3.7 runtime.注意:这些步骤设置特定于 Python 3.7 运行时的库。 If using any other version, you would need to download the .whl files corresponding to that Python version.如果使用任何其他版本,则需要下载与该 Python 版本对应的.whl文件。

  1. Create an EC2 instance using Amazon Linux AMI and SSH into this instance.使用 Amazon Linux AMI 和 SSH 在此实例中创建一个 EC2 实例。 We should create our layer in Amazon Linux AMI as the Lambda Python 3.7 runtime runs on this operating system (doc) .我们应该在 Amazon Linux AMI 中创建我们的层,因为 Lambda Python 3.7 运行时在此操作系统上运行(文档)

  2. Make sure this instance has Python3 and "pip" tool installed.确保此实例安装了 Python3 和“pip”工具。

  3. Download the numpy .whl file for the cp37 Python version and the manylinux1_x86_64 OS by executing the below command:通过执行以下命令,下载cp37 Python 版本和manylinux1_x86_64操作系统的numpy .whl文件:

$ wget https://files.pythonhosted.org/packages/d6/c6/58e517e8b1fb192725cfa23c01c2e60e4e6699314ee9684a1c5f5c9b27e1/numpy-1.18.5-cp37-cp37m-manylinux1_x86_64.whl
  1. Skip to the next step if you're not using pandas.如果您不使用 pandas,请跳至下一步。 Download the pandas .whl file for the cp37 Python version and the manylinux1_x86_64 OS by executing the below command:通过执行以下命令,下载cp37 Python 版本和manylinux1_x86_64操作系统的pandas .whl文件:
$ wget https://files.pythonhosted.org/packages/a4/5f/1b6e0efab4bfb738478919d40b0e3e1a06e3d9996da45eb62a77e9a090d9/pandas-1.0.4-cp37-cp37m-manylinux1_x86_64.whl
  1. Next, we will create a directory named "python" and unzip these files into that directory:接下来,我们将创建一个名为“python”的目录并将这些文件解压缩到该目录中:
        $ mkdir python
        $ unzip pandas-1.0.4-cp37-cp37m-manylinux1_x86_64.whl -d python/
        $ unzip numpy-1.18.5-cp37-cp37m-manylinux1_x86_64.whl -d python/
  1. We also need to download "pytz" library to successfully import numpy and pandas libraries:我们还需要下载“pytz”库才能成功导入 numpy 和 pandas 库:
        $ pip3 install -t python/ pytz
  1. Next, we would remove the “*.dist-info” files from our package directory to reduce the size of the resulting layer.接下来,我们将从 package 目录中删除“*.dist-info”文件以减小生成层的大小。
        $ cd python
        $ sudo rm -rf *.dist-info
  1. This will install all the required libraries that we need to run pandas and numpy.这将安装我们运行 pandas 和 numpy 所需的所有库。

  2. Zip the current "python" directory and upload it to your S3 bucket. Zip 当前“python”目录并将其上传到您的 S3 存储桶。 Ensure that the libraries are present in the hierarchy as given here .确保这些库存在于此处给出的层次结构中。

        $ cd ..
        $ zip -r lambda-layer.zip python/
        $ aws s3 cp lambda-layer.zip s3://YOURBUCKETNAME
  1. The "lambda-layer.zip" file can then be used to create a new layer from the Lambda console.然后可以使用“lambda-layer.zip”文件从 Lambda 控制台创建一个新层。

Base on aws lamda layer doc, https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html your zip package for the layer must have this structure. Base on aws lamda layer doc, https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html your zip package for the layer must have this structure.

my_layer.zip
  | python/numpy
  | python/numpy-***.dist-info

So what you have to do is create a folder python, and put the content of site-packages inside it, then zip up that python folder.所以你需要做的是创建一个文件夹 python,并将站点包的内容放入其中,然后将 zip 放在 python 文件夹上。 I tried this out with a simple package and it seem to work fine.我用一个简单的 package 进行了尝试,它似乎工作正常。

Also keep in mind, some package require c/c++ compilation, and for that to work you must install and package on a machine with similar architecture to lambda.还请记住,某些 package 需要 c/c++ 编译,为此,您必须在与 Z945F3FC449518A73B9F5F32868DB466ZC 架构相似的机器上安装 package。 Usually you would need to do this on an EC2 where you install and package where it have similar architecture to the lambda.通常你需要在你安装的 EC2 和 package 上执行此操作,它与 lambda 具有相似的架构。

That's bit of misleading question , because you at least did not mention you use serverless .这是一个误导性的问题,因为您至少没有提到您使用serverless I found it going through the snapshot of you project structure you provided.我通过您提供的项目结构的快照发现它。 That means you probably use serverless for deployment of your project within AWS provider.这意味着您可能使用serverless在 AWS 提供商中部署您的项目。

Actually, there are multiple ways you can arrange lambda layer .实际上,有多种方法可以排列lambda layer Let's have a look at each of them.让我们来看看他们中的每一个。

Native AWS

Once you will navigate to Add a layer , you will find 3 options: [AWS Layers, Custom Layers, Specify an ARN;] .导航到Add a layer后,您会发现 3 个选项: [AWS Layers, Custom Layers, Specify an ARN;]

Specify an ARN Guys, who did all work for you: KLayers Specify an ARN专家: KLayers

so, you need numpy , okay.所以,你需要numpy ,好吧。 Within lambda function navigate to the layers --> create a new layer --> out of 3 options, choose Specify an ARN and as the value put: arn:aws:lambda:eu-west-1:770693421928:layer:Klayers-python38-numpy:12 . Within lambda function navigate to the layers --> create a new layer --> out of 3 options, choose Specify an ARN and as the value put: arn:aws:lambda:eu-west-1:770693421928:layer:Klayers-python38-numpy:12 It will solve your problem and you will be able to work with numpy Namespace.它将解决您的问题,您将能够使用numpy命名空间。

Custom Layers

Choose a layer from a list of layers created by your AWS account or organization.从您的 AWS 账户或组织创建的层列表中选择一个层。

For custom layers the way of implementing can differ based on your requirements in terms of deployment.对于自定义层,实施方式可能会根据您在部署方面的要求而有所不同。 If are allowed to accomplish things manually, you should have a glimpse at following Medium article .如果允许手动完成事情,你应该看看下面的Medium 文章 I assume it will help you!我想它会帮助你!

AWS Layers

As for AWS pre-build layers, all is simple.至于 AWS 预构建层,一切都很简单。

Layers provided by AWS that are compatible with your function's runtime. AWS 提供的与您的函数运行时兼容的层。 Can differentiate between runtimes可以区分runtimes

For me I have list of: Perl5, SciPy, AppConfig Extension对我来说,我有以下列表: Perl5, SciPy, AppConfig Extension

Serverless

Within serverless things are much easier, because you can define you layers directly with lambda definition in serverless.yml file.serverless中,事情要容易得多,因为您可以直接使用serverless.yml文件中的 lambda 定义来定义层。 Afterwards, HOW to define them can differ as well.之后,如何定义它们也会有所不同。

Examples can be found at: How to publish and use AWS Lambda Layers with the Serverless Framework示例可在以下位置找到: 如何使用无服务器框架发布和使用 AWS Lambda 层

If you will have any questions, feel free to expand the discussion.如果您有任何问题,请随时扩展讨论。 Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM