无法从AWS Lambda层导入`yaml`模块

时间:2019-05-31 05:59:35

标签: python aws-lambda

我有一个非常简单的Python3.6 Lambda函数,唯一的复杂性是我想在其中包含pyyamlindex.py看起来像这样-

import yaml

def handler(event, context):
    return event

如果我将pyyaml捆绑在Lambda可部署程序中,则一切正常,

(aws_layer_demo) justin@justin-XPS-13-9360:~/work/aws_demos/aws_layer_demo$ jar -tvf  tmp/layer-demo-dev/2019-05-31-06-27-41.zip 
    59 Fri May 31 06:27:13 BST 2019 index.py
     0 Fri May 31 06:19:32 BST 2019 __init__.py
  4881 Fri May 31 06:15:37 BST 2019 yaml/composer.py
 25554 Fri May 31 06:15:37 BST 2019 yaml/constructor.py
  3294 Fri May 31 06:15:37 BST 2019 yaml/cyaml.py
                                    {... etc ...}

我可以ping Lambda函数并获得响应,没问题。

现在,我要测试在一个图层中是否包含pyyaml。所以我现在有两个可部署对象,一个用于应用程序-

(aws_layer_demo) justin@justin-XPS-13-9360:~/work/aws_demos/aws_layer_demo$ jar -tvf  tmp/layer-demo-dev/2019-05-31-06-43-42.zip 
    59 Fri May 31 06:27:13 BST 2019 index.py
     0 Fri May 31 06:19:32 BST 2019 __init__.py

和一层-

(aws_layer_demo) justin@justin-XPS-13-9360:~/work/aws_demos/aws_layer_demo$ jar -tvf tmp/layer-demo-layer-dev/2019-05-31-06-43-42.zip 
     0 Fri May 31 06:43:42 BST 2019 __init__.py
  4881 Fri May 31 06:15:37 BST 2019 yaml/composer.py
 25554 Fri May 31 06:15:37 BST 2019 yaml/constructor.py
  3294 Fri May 31 06:15:37 BST 2019 yaml/cyaml.py
                                    {... etc ...}

我已经使用Cloudformation堆栈(通过S3)进行了部署,并且看起来功能和层都已正确部署-

(aws_layer_demo) justin@justin-XPS-13-9360:~/work/aws_demos/aws_layer_demo$ ./scripts/show_function_config.sh dev
------------------------------------------------------------------------------------------------
|                                   GetFunctionConfiguration                                   |
+--------------+-------------------------------------------------------------------------------+
|  CodeSha256  |  MImn7X/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=                                 |
|  CodeSize    |  372                                                                          |
|  Description |                                                                               |
|  FunctionArn |  arn:aws:lambda:eu-west-1:XXXXXXXXXXXX:function:layer-demo-dev                |
|  FunctionName|  layer-demo-dev                                                               |
|  Handler     |  index.handler                                                                |
|  LastModified|  2019-05-31T05:44:14.089+0000                                                 |
|  MemorySize  |  512                                                                          |
|  RevisionId  |  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                         |
|  Role        |  arn:aws:iam::XXXXXXXXXXXX:role/layer-demo-dev-AppFunctionRole-XXXXXXXXXXXX   |
|  Runtime     |  python3.6                                                                    |
|  Timeout     |  5                                                                            |
|  Version     |  $LATEST                                                                      |
+--------------+-------------------------------------------------------------------------------+
||                                           Layers                                           ||
|+-------------+------------------------------------------------------------------------------+|
||  Arn        |  arn:aws:lambda:eu-west-1:XXXXXXXXXXXX:layer:layer-demo-dev:2                ||
||  CodeSize   |  41184                                                                       ||
|+-------------+------------------------------------------------------------------------------+|
||                                        TracingConfig                                       ||
|+-------------------------------+------------------------------------------------------------+|
||  Mode                         |  PassThrough                                               ||
|+-------------------------------+------------------------------------------------------------+|
||                                          VpcConfig                                         ||
|+---------------------------------------------------------------+----------------------------+|
||  VpcId                                                        |                            ||
|+---------------------------------------------------------------+----------------------------+|

在上面特别注意“层”,以及CodeSize> 0(即我没有意外部署大小为零的zipfile)这一事实

但是当我ping新分层的Lambda函数时,我现在得到-

Unable to import module 'index'

如果我深入研究Cloudwatch日志,我会得到-

Unable to import module 'index': No module named 'yaml'

所以。感觉好像我可能以某种方式错误地构造了我的图层包(在目录结构方面),以致Lambda在内部AWS yaml上看不到PYTHONPATH。或者其他的东西。谁能告诉我我哪里可能出问题了?

非常感谢。

1 个答案:

答案 0 :(得分:0)

事实证明,这是由于两个因素造成的。

首先,您必须非常小心如何构造图层包结构。仅压缩依赖项还不够-对于Python Lambda函数,必须将它们安装在zip文件中的python目录中。该目录会根据您使用的运行时发生变化。 Lambda文档中的信息不是特别清楚,但此处有更多详细信息-

https://medium.com/@adhorn/getting-started-with-aws-lambda-layers-for-python-6e10b1f9a5d

第二个是,在部署层时,您需要指定兼容的运行时-因此,如果您使用的是AWS CLI,则类似于-

aws lambda publish-layer-version --layer-name $appname --content S3Bucket=$s3bucketname,S3Key=$appname/layer.zip --compatible-runtimes python3.7

遵循这两个步骤,您的图层应该可以正常工作。