5

I am testing my api gateway to call lambda function. i was successful in the test.

i was then trying to make a connection to postgresql through the same lambda

   import json
   import psycopg2
    
    db_host = "hostname"
    db_port = 5432
    db_name ="db name"
    db_user ="user"
    db_pass ="password"
    
    def connect():
        conn = None
        try :
            conn = psycopg2.connect("dbname={} user={} host={} password={}".format(db_name,db_user,db_host,db_pass))
    except :
        print("connetion error")
    return conn


print("Loading function")
def lambda_handler(event, context):
    # paring query from the string 
    name = event['queryStringParameters']['name']
    action = event['queryStringParameters']['action']

print('name = '+name )
print('action = '+action)

# body of the response object 

transactionResponse = {}
transactionResponse['name'] = name
transactionResponse['action'] = action
transactionResponse['message'] = 'Lambda called from api_gateway'

# construting Http response

responseObject = {}
responseObject['statusCode'] = 200
responseObject['headers'] {}
responseObject['headers']['Content-Type'] = 'application/json'
responseObject['body'] = json.dumps(transactionResponse)

#  return the response object 

return responseObject

when i tried to trigger it through the API endpoint i got

 Unable to import module 'lambda_function': No module named 'psycopg2'

then i went ahead and build my lambda function by downloading the required package and then uploaded a zip file .

when i try to call try the same to trigger the lambda i am getting

 Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'lambda_function'

don't know what lamda_function is .

Could any one suggest me out of this slump ? or provide me a way to connect to RDS through lambda from API gateway trigger

This is my build Package enter image description here

sumanth shetty
  • 1,851
  • 5
  • 24
  • 57

3 Answers3

2

the issue is no longer there. Get the psycopg2 build library from https://github.com/jkehler/awslambda-psycopg2 was built for python 3.6 and make sure you change the name to psycopg2 while uploading your code to AWS lambda, select Python Runtime environment as 3.6, and it should work.

sumanth shetty
  • 1,851
  • 5
  • 24
  • 57
0

You should check the lambda handler name from the console. This is likely to be caused because the handler name is referring to lambda_function.foobar but the filename of the Lambda within the zip would be not be named lambda_function.py.

Ensure the name is in the format filename.function_name.

In this example if the file was named lambda_function then the handler value should be lambda_function.lambda_handler.

The directory structure does not currently include the psycopg2 module so this will still not be able to be loaded.

To solve this the following solutions are applicable:

  • Add the dependency via pip install, then zip up again deploy
  • Add a Lambda layer with this dependency already installed
Chris Williams
  • 32,215
  • 4
  • 30
  • 68
0

Can you check the lambda_handler settings and ensure they are correctly set to represent your function:

enter image description here

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 1
    it worked but now i am getting Runtime.ImportModuleError: Unable to import module 'lambda_api': No module named 'psycopg2' – sumanth shetty Jul 08 '20 at 11:37
  • @sumanthshetty Glad to hear. Have you bundled the `psycopg2` with your deployment package? – Marcin Jul 08 '20 at 11:45
  • have added the image in the question – sumanth shetty Jul 08 '20 at 11:48
  • @sumanthshetty I don't see `psycopg2` in your screenshot. You mean need to add it, for example as [layer](https://stackoverflow.com/a/60818205/248823). – Marcin Jul 08 '20 at 11:52
  • i have added it now . now when I run I get the error Runtime.ImportModuleError: Unable to import module 'lambda_api': No module named 'psycopg2._psycopg' – sumanth shetty Jul 08 '20 at 12:07
  • @sumanthshetty Maybe it got incorrectly installed. There is also [aws-psycopg2](https://pypi.org/project/aws-psycopg2/). Maybe you can try that. – Marcin Jul 08 '20 at 12:22
  • 1
    @sumanthshetty You can also check this [thread](https://stackoverflow.com/questions/36607952/using-psycopg2-with-lambda-to-update-redshift-python). Lots of useful info on how to use psycopg2 with lambda. – Marcin Jul 08 '20 at 12:25
  • i tried installing the aws-psycopg2 but the zip is exceeding 10mb – sumanth shetty Jul 08 '20 at 12:31
  • @sumanthshetty Max allowed size of the zip is 50 MB, so I don't see why 10 MB would be a problem? – Marcin Jul 08 '20 at 21:02