2

Here's my issue:

START RequestId: 3ef6bbb9-62da-11e8-82ba-81e0afb0b224 Version: $LATEST
Unable to import module 'lambda_insertEmailAddress': No module named psycopg2

END RequestId: 3ef6bbb9-62da-11e8-82ba-81e0afb0b224
REPORT RequestId: 3ef6bbb9-62da-11e8-82ba-81e0afb0b224  Duration: 0.44 ms   Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 19 MB  

My zip file has the following structure file name: lambdaInsertEmail.zip:

 total 98784
drwxrwxrwx  20 chauncey  staff   680B May 27 13:22 psycopg2
drwxrwxrwx  22 chauncey  staff   748B May 27 12:55 postgresql-9.4.3
-rwxrwxrwx   1 chauncey  staff   1.8K Apr 30 15:41 lambda_insertEmailAddress.py
-rw-r--r--   1 chauncey  staff    48M May 30 12:09 lambdaInsertEmail.zip

In case you want to know my setup.cfg file has the following changes:

pg_config=/Users/chauncey/Desktop/portfolio/aws_lambda_files/lambda_insertEmailAddress/postgresql-9.4.3/src/bin/pg_config/pg_config

static_libpq=1

I'm trying to get this lambda function working.

halfer
  • 19,824
  • 17
  • 99
  • 186
SonOfSeuss
  • 400
  • 5
  • 18
  • What OS did you use to package the function? - note: Lambda will only be able to load psycopg2 if packaged in Linux. Also What version of python are you using? – KiteCoder May 31 '18 at 15:29
  • @Jeff I used OSX 11.6. I used the Terminal application. The version of Python I used is 2.7.9. Any help you can give is greatly appreciated. – SonOfSeuss Jun 01 '18 at 01:19
  • Can you try using the Amazon Linux AMI to compile the libraries? - reference https://stackoverflow.com/a/36608956/5787099 – KiteCoder Jun 01 '18 at 02:33
  • I'm trying to do that. I'm getting this error: `make[1]: *** [install-include-recurse] Error 2 make[1]: Leaving directory `/home/ec2-user/lambdaFunctions/postgresql-9.4.3/src' make: *** [install-src-recurse] Error 2` on the make install. How can I get this to work, using I guess the root password or sudo. I tried `sudo make install` but it's not working. I guess I need to switch to root but I don't know the password. – SonOfSeuss Jun 05 '18 at 02:13
  • I actually made this happen by using `sudo su` and `sudo sh` and it didn't work. Any advice? – SonOfSeuss Jun 05 '18 at 02:19

2 Answers2

2

The problem is being cause because psycopg2 needs to be build an compiled with statically linked libraries for Linux. Please reference Using psycopg2 with Lambda to Update Redshift (Python) for more details on this issue. Another reference of problems of compiling psycopg2 on OSX.

One Solution is to compile the library using an Amazon Linux AMI. Once you have connected to the AMI with SSH:

  1. Set up aws credentials using aws configure
  2. sudo su -
  3. pip install psycopg2 -t /path/to/project-dir
  4. Zip directory zip project-dir.zip project-dir
  5. Upload to Lambda using CLI

Hopefully this helps you understand the issue and a possible solution solution. There are additional solutions in the above references, such as using boto3 or this repo https://github.com/jkehler/awslambda-psycopg2, but this is the solution that worked for me.

KiteCoder
  • 2,364
  • 1
  • 13
  • 29
2

pip install psycopg2-binary

fixed it for me

Neil
  • 7,482
  • 6
  • 50
  • 56