Serverless - Numpy - Unable to find good bind path format

萝らか妹 提交于 2019-12-05 20:00:52

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:

Create a layer package:

  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
    
  3. 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
    
  4. 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
    
  5. 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)
    

when I got the same issue, I opened docker went to settings/shared drive opted to reset credentials and after applied my changes and this cleared the error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!