Google Cloud Platform API for Python and AWS Lambda Incompatibility: Cannot import name 'cygrpc'

后端 未结 3 1028
情深已故
情深已故 2020-12-31 20:30

I am trying to use Google Cloud Platform (specifically, the Vision API) for Python with AWS Lambda. Thus, I have to create a deployment package for my dependencies. However,

3条回答
  •  臣服心动
    2020-12-31 20:38

    Building off the answer from @Josh Wolff (thanks a lot, btw!), this can be streamlined a bit by using a Docker image for Lambdas that Amazon makes available.

    You can either bundle the libraries with your project source or, as I did below in a Makefile script, upload it as an AWS layer.

    layer:
        set -e ;\
        docker run -v "$(PWD)/src":/var/task "lambci/lambda:build-python3.6" /bin/sh -c "rm -R python; pip install -r requirements.txt -t python/lib/python3.6/site-packages/; exit" ;\
        pushd src ;\
        zip -r my_lambda_layer.zip python > /dev/null ;\
        rm -R python ;\
        aws lambda publish-layer-version --layer-name my_lambda_layer --description "Lambda layer" --zip-file fileb://my_lambda_layer.zip --compatible-runtimes "python3.6" ;\
        rm my_lambda_layer.zip ;\
        popd ;
    

    The above script will:

    1. Pull the Docker image if you don't have it yet (above uses Python 3.6)
    2. Delete the python directory (only useful for running a second time)
    3. Install all requirements to the python directory, created in your projects /src directory
    4. ZIP the python directory
    5. Upload the AWS layer
    6. Delete the python directory and zip file

    Make sure your requirements.txt file includes the modules listed above by Josh: google-cloud-vision, protobuf, google-api-python-client, httplib2, uritemplate, google-auth-httplib2

提交回复
热议问题