且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无服务器-脾气暴躁-无法找到良好的绑定路径格式

更新时间:2022-06-15 03:21:29

我无法使插件正常工作,但是无论如何我都找到了更好的解决方案-Lambda Layers.这是一个好处,因为它减小了lambda的大小并允许代码/文件重用.有一个用于numpy和scipy的预先构建的lambda层,您可以使用,但是我建立了自己的lambda层,向我展示了它们如何工作.这是我的工作方式:

I was unable to make the plugin work but I found a better solution anyhow - Lambda Layers. This is a bonus because it reduces the size of the lambda and allows code/file reuse. There is a pre-built lambda layer for numpy and scipy that you can use, but I built my own to show myself how it all works. Here's how I made it work:

创建图层包:

  1. 打开EC2实例,Ubuntu或Linux或任何其他版本-这是必需的,以便我们可以正确地编译运行时二进制文件
  2. 制作一个依赖包zip-必须在运行时使用目录结构python/lib/python3.6/site-packages进行python查找

  1. Open an EC2 instance or Ubuntu or Linux or whatever - This is needed so we can compile the runtime binaries correctly
  2. Make a dependencies package zip - Must use the directory structure python/lib/python3.6/site-packages for python to find during runtime

mkdir -p tmpdir/python/lib/python3.6/site-packages 
pip install -r requirements.txt --no-deps -t tmpdir/python/lib/python3.6/site-packages 
cd tmpdir zip -r ../py_dependencies.zip . 
cd .. 
rm -r tmpdir

  • 将层zip推送到AWS-需要最新的awscli

  • Push layer zip to AWS - requires latest awscli

    sudo pip install awscli --upgrade --user
    sudo aws lambda publish-layer-version \
    --layer-name py_dependencies \
    --description "Python 3.6 dependencies [numpy=0.15.4]" \
    --license-info "MIT" \
    --compatible-runtimes python3.6 \
    --zip-file fileb://py_dependencies.zip \
    --profile python_dev_serverless
    

  • 要在需要numpy的任何功能中使用,只需使用控制台中或上面的上传过程中显示的arn

  • To use in any function that requires numpy, just use the arn that is shown in the console or during the upload above

    f1:
      handler: index.handler_f_use_numpy
      include:
        - functions/f_use_numpy.py
      layers:
        - arn:aws:lambda:us-west-2:XXXXX:layer:py_dependencies:1
    

  • 作为一项额外的好处,您还可以将诸如常量之类的常见文件推送到一层.这是我在Windows和Lambda上测试使用的方式:

  • As an added bonus, you can push common files like constants to a layer as well. Here's how I did it for testing use in windows and on the lambda:

    import platform
    
    \# Set common path
    COMMON_PATH = "../../layers/common/"
    if platform.system() == "Linux": COMMON_PATH = "/opt/common/"
    
    def handler_common(event, context):
        # Read from a constants.json file
        with open(COMMON_PATH + 'constants.json') as f:
            return text = json.load(f)