1

Full screenshot

Runtime settings

I am going to connect RDS postgresql Database using Lambda function (python script) I attached screenhot. The error logs here. Unable to import module 'postgres_test': No module named 'psycopg2'

python version is 3.6

This issue causing due to not installed psycopg2 package. Then I don't know how can I install the package on lambda Pls guide me for it.

postgres_test.py:

`

import sys
import logging
import psycopg2

from db_util import make_conn, fetch_data
def lambda_handler(event, context):
query_cmd = "select count(*) from tablename"
# print query_cmd

# get a connection, if a connect cannot be made an exception will be raised here
conn = make_conn()

result = fetch_data(conn, query_cmd)
conn.close()

return result

db_util.py:

` ​

   ​import psycopg2

   ​db_host = "db_host" 
   ​db_port = 5432
   ​db_name = "db_name "
   ​db_user = "db_user "
   ​db_pass = "db_pass "
   ​db_table = "users"


   ​def make_conn():
       ​conn = None
       ​try:
           ​conn = psycopg2.connect("dbname='%s' user='%s' host='%s' 
   ​password='%s'" % (db_name, db_user, db_host, db_pass))
       ​except:
           ​print "I am unable to connect to the database"
       ​return conn


   ​def fetch_data(conn, query):
       ​result = []
       ​print "Now executing: %s" % (query)
       ​cursor = conn.cursor()
       ​cursor.execute(query)

       ​raw = cursor.fetchall()
       ​for line in raw:
           ​result.append(line)

       ​return result
3
  • 3
    please include the relevant code in the body of your question as text so it can be searched and copy and pasted to help us help you! Commented Oct 19, 2021 at 15:13
  • 1
    Are you sure psycopg2 is installed? Commented Oct 19, 2021 at 15:16
  • No, it isn't installed on lambda. I don't know how can I install psycopg2 on lambda. @kubatucka Commented Oct 19, 2021 at 15:21

1 Answer 1

1

To work with different libraries in lambda you have to install the library in the current project and upload it as zip file to lambda.

Specific to psycopg2 use this repo https://github.com/jkehler/awslambda-psycopg2

And to install some other library use the below command
For example requests library

pip install requests -t .

Your project will look something like below

.
├── lambda_function.py
├── psycopg2
├── <library2>

To upload a project to lambda using zip file method you can use the following links

https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html https://alexharv074.github.io/2018/08/18/creating-a-zip-file-for-an-aws-lambda-python-function.html

Sign up to request clarification or add additional context in comments.

7 Comments

nope in lambda venv folder doesnt really makes sense in the current project root level just install all the libraries
I have edited my answer
I have added a git repo in the answer specific to psycopg2 repo. can you check the readme of it and clone specific folder in your repo - github.com/jkehler/awslambda-psycopg2
If using python-3.6 then just copy psycopg2-3.6 from the repo and rename it to psycopg2
Thank you. the issue fixed once followed as you mentioned. I posted new issue continuous from here. stackoverflow.com/questions/69638493/… can you reply on there?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.