簡體   English   中英

如何將模塊從自定義包正確導入 AWS Lambda 函數?

[英]How do I properly import modules from custom packages into an AWS Lambda function?

我是AWS 的新手,需要一些建議。 我有一個包含多個 AWS Lambda 函數的項目。 就我而言,有四個,它們位於functions文件夾中。 它們都需要使用數據庫連接。 所以我決定將與數據庫相關的代碼放在一個名為databases的單獨包中,並在 AWS Lambda Layers 中使用它。 如您所見, databases包有兩個模塊。 正如我從文檔中了解到的,我需要存檔我的包並將其放入 AWS Lambda 層中。 但是,這個包的地址發生了變化。 因為 AWS 默認將它放在/opt目錄中。 我有點困惑。 如何將模塊從自定義包正確導入 AWS Lambda 函數,以便它在本地和生產中工作?

我的項目結構如下所示:

src
   functions
      create_user_information
         __init__.py
         lambda_function.py
      update_user_information
         __init__.py
         lambda_function.py
      get_user_information
         __init__.py
         lambda_function.py
      delete_user_information
         __init__.py
         lambda_function.py
   layers
      databases
         __init__.py
         cassandra.py
         postgresql.py
         requirements.txt
template.yaml
venv
   bin
      ...
   include
   lib
      ...

lambda_function.py:

from src.layers.databases import cassandra


cassandra_db_session = None
cassandra_db_username = 'your-username'
cassandra_db_password = 'your-password'
cassandra_db_endpoints = ['your-endpoint']
cassandra_db_port = 9142


def lambda_handler(event, context):
    global cassandra_db_session
    if not cassandra_db_session:
        cassandra_db_session = cassandra.create_session(
            cassandra_db_username,
            cassandra_db_password,
            cassandra_db_endpoints,
            cassandra_db_port
        )
    # Some business logic.
    # ...
    return "AWS Lambda function finished."

模板.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: User Information Service
Globals:
    Function:
        Timeout: 10
Resources:
    Databases:
        Type: AWS::Serverless::LayerVersion
        Properties:
            LayerName: Databases
            Description:
            ContentUri:
            CompatibleRuntimes:
                - python3.8
            LicenseInfo: MIT
            RetentionPolicy: Retain
    GetUserInformation:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: GetUserInformation
            Description:
            CodeUri: src/functions/get_user_information
            Handler: lambda_function.lambda_handler
            Runtime: python3.8
            Layers:
                - !Ref Databases
    CreateUserInformation:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: CreateUserInformation
            Description:
            CodeUri: src/functions/create_user_information
            Handler: lambda_function.lambda_handler
            Runtime: python3.8
            Layers:
                - !Ref Databases
    UpdateUserInformation:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: UpdateUserInformation
            Description:
            CodeUri: src/functions/update_user_information
            Handler: lambda_function.lambda_handler
            Runtime: python3.8
            Layers:
                - !Ref Databases
    DeleteUserInformation:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: DeleteUserInformation
            Description:
            CodeUri: src/functions/delete_user_information
            Handler: lambda_function.lambda_handler
            Runtime: python3.8
            Layers:
                - !Ref Databases
Outputs:
    DatabasesARN:
        Value: !Ref Databases
        Description: Databases ARN
        Export:
            Name: databases-arn

假設您已經打包了databases並將其正確安裝在層中,那么您的導入將非常簡單:

# assume 'databases' is the installation name of your pkg
from databases import cassandra  
  1. 你的包必須是“可安裝的”才能讓上面的代碼工作,而它看不到。 網上有很多關於如何正確操作的參考資料。

重要的是要注意您不需要遵循這種方法(除了被推薦)。 您可以打包整個src文件夾並上傳為您的 lambda 函數,因此您可以按如下方式使用:

# don't use relative imports in lambda because the change to opt/
from src.layers.databases import cassandra  

當前的問題不是由任何類型的“目錄更改”引起的,而是由不正確的包裝引起的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM