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

TL;DR: You cannot compile the deployment package on your Mac or whatever pc you use. You have to do it using a specific OS/"setup", the same one that AWS Lambda uses to run your code. To do this, you have to use EC2.

I will provide here an answer on how to get Google Cloud Vision working on AWS Lambda for Python 2.7. This answer is potentially extendable for other other APIs and other programming languages on AWS Lambda.

So the my journey to a solution began with this initial posting on Github with others who have the same issue. One solution someone posted was

I had the same issue " cannot import name 'cygrpc' " while running the lambda. Solved it with pip install google-cloud-vision in the AMI amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2 instance and exported the lib/python3.6/site-packages to aws lambda Thank you @tseaver

This is partially correct, unless I read it wrong, but regardless it led me on the right path. You will have to use EC2. Here are the steps I took:

  1. Set up an EC2 instance by going to EC2 on Amazon. Do a quick read about AWS EC2 if you have not already. Set one up for amzn-ami-hvm-2018.03.0.20180811-x86_64-gp2 or something along those lines (i.e. the most updated one).
  2. Get your EC2 .pem file. Go to your Terminal. cd into your folder where your .pem file is. ssh into your instance using

    ssh -i "your-file-name-here.pem" [email protected]

  3. Create the following folders on your instance using mkdir: google-cloud-vision, protobuf, google-api-python-client, httplib2, uritemplate, google-auth-httplib2.

  4. On your EC2 instance, cd into google-cloud-vision. Run the command:

    pip install google-cloud-vision -t .

Note If you get "bash: pip: command not found", then enter "sudo easy_install pip" source.

  1. Repeat step 4 with the following packages, while cd'ing into the respective folder: protobuf, google-api-python-client, httplib2, uritemplate, google-auth-httplib2.

  2. Copy each folder on your computer. You can do this using the scp command. Again, in your Terminal, not your EC2 instance and not the Terminal window you used to access your EC2 instance, run the command (below is an example for your "google-cloud-vision" folder, but repeat this with every folder):

    sudo scp -r -i your-pem-file-name.pem [email protected]:~/google-cloud-vision ~/Documents/your-local-directory/

  3. Stop your EC2 instance from the AWS console so you don't get overcharged.

  4. For your deployment package, you will need a single folder containing all your modules and your Python scripts. To begin combining all of the modules, create an empty folder titled "modules." Copy and paste all of the contents of the "google-cloud-vision" folder into the "modules" folder. Now place only the folder titled "protobuf" from the "protobuf" (sic) main folder in the "Google" folder of the "modules" folder. Also from the "protobuf" main folder, paste the Protobuf .pth file and the -info folder in the Google folder.

  5. For each module after protobuf, copy and paste in the "modules" folder the folder titled with the module name, the .pth file, and the "-info" folder.

  6. You now have all of your modules properly combined (almost). To finish combination, remove these two files from your "modules" folder: googleapis_common_protos-1.5.3-nspkg.pth and google_cloud_vision-0.34.0-py3.6-nspkg.pth. Copy and paste everything in the "modules" folder into your deployment package folder. Also, if you're using GCP, paste in your .json file for your credentials as well.

  7. Finally, put your Python scripts in this folder, zip the contents (not the folder), upload to S3, and paste the link in your AWS Lambda function and get going!

If something here doesn't work as described, please forgive me and either message me or feel free to edit my answer. Hope this helps.


There's a fast solution that doesn't require much coding.

Cloud9 uses AMI so using pip on their virtual environment should make it work.

I created a Lambda from the Cloud9 UI and from the console activated the venv for the EC2 machine. I proceeded to install google-cloud-speech with pip.That was enough to fix the issue.