简体   繁体   English

使用同一库的多个AWS Lambda函数

[英]Multiple AWS Lambda functions using the same libraries

I am writing AWS Lambda functions for my android app backend. 我正在为我的android应用后端编写AWS Lambda函数。 I have multiple Lambda functions in python on AWS which requires the same libraries. 我在AWS上的python中有多个Lambda函数,它们需要相同的库。 For example, I need to access the database so I use pymysql library in all my lambda functions. 例如,我需要访问数据库,以便在所有lambda函数中使用pymysql库。 But I am not sure whether I am doing it right. 但是我不确定我是否做对了。

Do I have to include these libraries in every function package that I deploy or is there a better way by which I can reference the libraries I have used in the previous function? 我是否必须在部署的每个功能包中都包含这些库,还是可以通过更好的方式引用以前功能中使用的库?

I am following Tutorial: Accessing Amazon RDS in an Amazon VPC . 我正在学习教程:在Amazon VPC中访问Amazon RDS I have 2 functions. 我有2个功能。 I am uploading each function separately with its dependencies in a zip. 我分别在zip中上传每个函数及其相关性。 Zip contains the code and libraries. 邮政编码包含代码和库。 Libraries takes most of the space making zip size big. 图书馆占用了大部分空间,使拉链尺寸变大。 Now the second function also requires the same libraries so again making a zip with same libraries feels wrong. 现在,第二个功能也需要相同的库,因此再次使用相同的库制作zip感觉是错误的。

Also some links to where this is mentioned in docs is helpful. 此外,一些指向文档中提到的地方的链接也很有帮助。 I did not find it anywhere in documentation. 我在文档中的任何地方都找不到它。

You can share your code using AWS Lambda Layers . 您可以使用AWS Lambda Layers共享您的代码。 For example define them using AWS::Lambda::LayerVersion or AWS::Serverless::LayerVersion . 例如,使用AWS::Lambda::LayerVersionAWS::Serverless::LayerVersion定义它们。 You can then reference to them in your Python Lambda functions. 然后,您可以在Python Lambda函数中引用它们。 Here using AWS SAM : 在这里使用AWS SAM

  MyLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: function_code/
      Handler: app.lambda_handler
      Runtime: python3.6
      Layers:
        - !Ref MySharedLayer
  MySharedLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: SharedLayerName
      Description: Some shared code
      ContentUri: layer_code/
      CompatibleRuntimes:
        - python3.6
      RetentionPolicy: Retain

Each Lambda function will have the shared code available in /opt . 每个Lambda函数将在/opt提供共享代码。 It can be then used in the functions. 然后可以在功能中使用它。

Now that the Lambda Layers are released you can easily share libraries and code between your Lambda Functions. 现在, Lambda层已发布,您可以轻松地在Lambda函数之间共享库和代码。

You can create a zip file for the Layer pretty much the same way as you can do so for a Function. 您可以为图层创建zip文件,几乎与为功能创建zip文件的方法相同。
To share pymysql package you will need to create a Lambda Layer on base of the following function: 要共享pymysql包,您将需要基于以下函数创建Lambda层:

pymysql-bundle.zip/
  python/lib/python3.7/site-packages/pymysql

Then from your Lambda Function's code you can reference it like this: 然后从Lambda函数的代码中可以像这样引用它:

from pymysql import ...

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

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