I am running Windows 10 Home with Docker Toolbox. I’m trying to deploy a lambda function that uses numpy. I have serverless.yml:
service: test
plugins:
- serverless-python-requirements
custom:
pythonRequirements:
zip: true
# dockerFile: Dockerfile
dockerizePip: non-linux
provider:
name: aws
runtime: python3.6
stage: dev
environment: ${file(./env.yml):${opt:stage, self:provider.stage}.env}
region: ${file(./env.yml):${opt:stage, self:provider.stage}.aws.region}
profile: ${file(./env.yml):${opt:stage, self:provider.stage}.aws.profile}
package:
individually: true
functions:
f1:
handler: index.handler_sf
# timeout: 3 # in seconds (overrides default of 6, can also be done at provider level above)
# memory: 256 # in MB (overrides default of 1024, can also be done at provider level above)
module: functions/f1
and I get:
Serverless: Docker Image: lambci/lambda:build-python3.6
Error --------------------------------------------------
Unable to find good bind path format
I’ve tried using the dockerFile: DockerFile
as suggested here but that fails in similar way.
Any luck with this?
If I use Linux containers I get a different error.
@moneerrifai - no solution as of yet. I have posts here, on the python-requirements github issues, and stack overflow. Hopefully something soon.
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:
- Open an EC2 instance or Ubuntu or Linux or whatever - This is needed so we can compile the runtime binaries correctly
- 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
- 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
- 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
- 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)