如何使用Python使用AWS Lambda层? [英] How to use AWS Lambda layer using Python?

查看:73
本文介绍了如何使用Python使用AWS Lambda层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Lambda函数,它使用 numpy 库,
我已经在本地设置了虚拟环境,并且我的代码能够在本地获取和使用该库.

I have a simple Lambda function which is using the numpy library,
I have set up a virtual environment in my local, and my code is able to fetch and use the library locally.

我尝试使用AWS Lambda的图层,并压缩了 venv 文件夹并上传到该图层,
然后,我将正确的图层和版本附加到我的函数中,
但是该函数无法获取库

I tried to use AWS Lambda's layer, and zipped the venv folder and uploaded to the layer,
Then I attached the correct layer and version to my function,
But the function is not able to fetch the library

以下是适用于本地的代码-

import numpy as np

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

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

以下是我压缩并上传的venv结构-

我收到以下错误-

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

我的Lambda部署如下所示-

我正在尝试引用此内容-
https://towardsdatascience.com/introduction-to-amazon-lambda-layers-and-boto3-using-python3-39bd390add17

推荐答案

我已经看到,当使用 pip 安装时,诸如numpy和pandas之类的一些库在Lambda中不起作用.我已经成功使用这些库的 .whl 包文件创建了Lambda层.请参考以下步骤:

I've seen that a few libraries like numpy and pandas don't work in Lambda when installed using pip. I have had success using the .whl package files for these libraries to create the Lambda layer. Refer to the steps below:

注意:这些步骤设置了特定于Python 3.7运行时的库.如果使用任何其他版本,则需要下载与该Python版本相对应的 .whl 文件.

  1. 使用Amazon Linux AMI和SSH在该实例中创建EC2实例.当Lambda Python 3.7运行时在此操作系统上运行时,我们应该在Amazon Linux AMI中创建层.(文档).

确保此实例已安装Python3和"pip"工具.

Make sure this instance has Python3 and "pip" tool installed.

下载 numpy .whl 文件通过执行以下命令,获取 cp37 Python版本和 manylinux1_x86_64 操作系统:

Download the numpy .whl file for the cp37 Python version and the manylinux1_x86_64 OS by executing the below command:

$ wget https://files.pythonhosted.org/packages/d6/c6/58e517e8b1fb192725cfa23c01c2e60e4e6699314ee9684a1c5f5c9b27e1/numpy-1.18.5-cp37-cp37m-manylinux1_x86_64.whl

  1. 如果您不使用熊猫,请跳至下一步.为 cp37下载熊猫 .whl 文件. Python版本和 manylinux1_x86_64 操作系统,方法是执行以下命令:
  1. Skip to the next step if you're not using pandas. Download the pandas .whl file for the cp37 Python version and the manylinux1_x86_64 OS by executing the below command:

$ wget https://files.pythonhosted.org/packages/a4/5f/1b6e0efab4bfb738478919d40b0e3e1a06e3d9996da45eb62a77e9a090d9/pandas-1.0.4-cp37-cp37m-manylinux1_x86_64.whl

  1. 接下来,我们将创建一个名为"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. 我们还需要下载"pytz"库才能成功导入numpy和pandas库:

        $ pip3 install -t python/ pytz

  1. 接下来,我们将从包目录中删除"* .dist-info"文件,以减小生成的图层的大小.

        $ cd python
        $ sudo rm -rf *.dist-info

  1. 这将安装运行pandas和numpy所需的所有必需库.

  1. This will install all the required libraries that we need to run pandas and numpy.

压缩当前的"python"目录并将其上传到您的S3存储桶.确保库按照给定的

Zip the current "python" directory and upload it to your S3 bucket. 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. 然后可以使用"lambda-layer.zip"文件从Lambda控制台中创建新图层.

这篇关于如何使用Python使用AWS Lambda层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆