0

I have a Python app that uses Google Cloud Pubsub, like this:

from google.cloud import pubsub

It works fine locally, but on calling the AWS Lambda function I get:

Unable to import module 'handler': cannot import name 'pubsub'

and no other error or details.

My requirements.txt file:

requests
google-cloud-datastore==1.4.0
google-cloud-pubsub==0.29.0
sqlalchemy

I have a feeling it may be do to the psutil requirement of pubsub, as when I try to import psutil before pubsub, I get this error:

Unable to import module 'handler': cannot import name '_psutil_linux'
skunkwerk
  • 2,920
  • 2
  • 37
  • 55
  • this seems more like a question about how you are uploading the deployment package, since lambda does not provide those requirements for you. Can you give more detail about how you are uploading your code? – avigil Nov 19 '17 at 02:16
  • 1
    Possible duplicate of [How do I add python libraries to an AWS lambda function for Alexa?](https://stackoverflow.com/questions/38877058/how-do-i-add-python-libraries-to-an-aws-lambda-function-for-alexa) – Abdennour TOUMI Nov 19 '17 at 02:32

2 Answers2

1

If you are using the serverless library, the solution is to do the following:

delete the .requirements folder in your project directory

add this to your serverless.yml file (docs here):

custom:
  pythonRequirements:
    dockerizePip: true
skunkwerk
  • 2,920
  • 2
  • 37
  • 55
0

You will need to prepackage your lambda function in a zip file that includes your external module.

pip install module-name -t /path/to/project-dir

Then compress your code with the modules included and upload the zip file.

For Reference: http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html