简体   繁体   English

是否可以通过 serverless.yml 向 PATH 环境变量添加路径?

[英]Is it possible to add paths to the PATH environment variable through serverless.yml?

When I create an AWS Lambda Layer, all the contents / modules of my zip file go to /opt/ when the AWS Lambda executes.当我创建 AWS Lambda 层时,我的 zip 文件的所有内容/模块都会在 AWS Lambda 执行时转到/opt/ This easily becomes cumbersome and frustrating because I have to use absolute imports on all my lambdas.这很容易变得麻烦和令人沮丧,因为我必须对我所有的 lambda 使用绝对导入。 Example:例子:

import json
import os
import importlib.util
spec = importlib.util.spec_from_file_location("dynamodb_layer.customer", "/opt/dynamodb_layer/customer.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

def fetch(event, context):

    CustomerManager = module.CustomerManager
    customer_manager = CustomerManager()

    body = customer_manager.list_customers(event["queryStringParameters"]["acquirer"])

    response = {
        "statusCode": 200,
        "headers": {
            "Access-Control-Allow-Origin": "*"
        },
        "body": json.dumps(body)
    }

    return response

So I was wondering, is it possible to add these /opt/paths to the PATH environment variable by beforehand through serverless.yml?所以我想知道,是否可以通过 serverless.yml 预先将这些 /opt/paths 添加到 PATH 环境变量中? In that way, I could just from dynamodb_layer.customer import CustomerManager , instead of that freakish ugliness.这样,我就可以from dynamodb_layer.customer import CustomerManager ,而不是那种怪异的丑陋。

I've Lambda layer for Python3.6 runtime.我有用于 Python3.6 运行时的 Lambda 层。 My my_package.zip structure is:我的 my_package.zip 结构是:

my_package.zip
 - python
   - lib
     - python3.6
       - site-packages
         - customer

All dependencies are in build folder in project root: eg build/python/lib/python3.6/site-packages/customer所有依赖项都在项目根目录的build文件夹中:例如build/python/lib/python3.6/site-packages/customer

Relevant section of my serverless.yml我的 serverless.yml 的相关部分

layers:
  my_package:
    path: build             
    compatibleRuntimes:     
      - python3.6

In my Lambda I import my package like I would do any other package: import customer在我的 Lambda 中,我像导入任何其他包一样导入我的包: import customer

Have you tried setting your PYTHONPATH env var?您是否尝试过设置 PYTHONPATH 环境变量? https://stackoverflow.com/a/5944201/6529424 https://stackoverflow.com/a/5944201/6529424

Have you tried adding to sys.path?您是否尝试添加到 sys.path? https://stackoverflow.com/a/12257807/6529424 https://stackoverflow.com/a/12257807/6529424

In the zip archive, the module needs to be placed the in a python subdirectory so that when it is extracted as a layer in Lambda, it is located in /opt/python .在 zip 存档中,模块需要放在python子目录中,以便在 Lambda 中作为层提取时,它位于/opt/python That way you'll be able to directly import your module without the need for importlib .这样你就可以直接导入你的模块而无需importlib

It's documented here or see this detailed blogpost from an AWS dev evangelist for more.记录在此处或查看来自 AWS 开发布道者的这篇详细博客文章以了解更多信息。

Setting of PYTHONPATH variable is not required, as long as you place items correctly inside the zip file.不需要设置 PYTHONPATH 变量,只要您将项目正确放置在 zip 文件中即可。

Simple modules, and package directories, these should be placed inside a directory "python", and then the whole python/ placed into the zip file for uploading to AWS as a layer.简单的模块和包目录,这些应该放在一个目录“python”中,然后整个python/放在zip文件中作为一个层上传到AWS。 Don't forget to add the "compatible runtimes" (eg Python 3.6, 3.7, 3.8 ...) settings for the layers.不要忘记为图层添加“兼容运行时”(例如 Python 3.6、3.7、3.8 ...)设置。

So as an example:举个例子:

python/
- my_module.py
- my_package_dir
-- __init__.py
-- package_mod_1.py
-- package_mod_2.py

which then get included in the zip file.然后将其包含在 zip 文件中。

zip -r my_layer_zip.zip python/

The modules can then be imported without any more ado, when accessed as a layer:当作为层访问时,可以毫不费力地导入模块:

....
import my_module
from my_package.package_mod_2 import mod_2_function
....

You can see the package structure from within the lambda if you look at '/opt/python/' which will show my_module.py, my_package/ etc., this is easily tested using the AWS Lambda test function, assuming the layer is attached to the function (or else the code will error)如果您查看将显示 my_module.py、my_package/ 等的“/opt/python/”,您可以从 lambda 中看到包结构,这很容易使用 AWS Lambda 测试函数进行测试,假设该层附加到函数(否则代码会出错)

import json
import os

def lambda_handler(event, context):
    # TODO implement

    dir_list = os.listdir('/opt/python/')

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!'),
        'event': json.dumps(event),
        '/opt/python/': dir_list
    }

暂无
暂无

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

相关问题 serverless.yml 调用 python - serverless.yml to invoke python 是否可以将网络驱动器添加到%PATH%环境变量 - Is it possible to add a network drive to %PATH% environment variable 如何在 serverless.yml 中为 AWS SQS 设置 maximumBatchingWindowInSeconds? - How to set maximumBatchingWindowInSeconds for AWS SQS in serverless.yml? 如何使用 serverless.yml 创建 dynamodb 表并使用 python boto3 删除其中的项目? - How to create the dynamodb table using serverless.yml and delete the items of it using python boto3? 如何配置serverless.yml或AWS API网关或AWS Lambda处理程序以从POST请求读取request.header - How to configure serverless.yml or AWS API gateway or AWS lambda handler to read request.headers from POST request 是否可以在没有root访问权限的情况下将模块路径添加到linux中的python环境变量? - Is it possible to add the module path to the python environment variable in linux with out root access? 不能在环境变量路径中添加python - can not add python to the path of environment variable Linux Path环境变量—以正确的方式添加带空格的路径 - Linux Path environment variable — add path with spaces in the right way 是否可以在无服务器环境中使用 Whoosh 搜索? - Is it possible to use Whoosh search in a serverless environment? python virtualenv —是否可以增加$ PATH或添加其他环境变量? - python virtualenv — possible to augment $PATH or add other environment variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM