Terry Chon
Terry Chon

Reputation: 21

psycopg2 aws lambda timeout error

I have a python 3.6 script that needs to get executed on AWS Lambda. The script needs to establish a connection to RedShift - being done through psycopg2.

The deployment package has been created with the appropriate compatible psycopg2 version.

When establishing a connection - i.e., conn = psycopg2.connect(...) -, the script times out at thresholds from ranges as small as 5 seconds to ranges as large as 5 minutes (when executing the script locally, it takes less than 5 seconds).

If I remove the connection statement (i.e., conn = psycopg2.connect(...)), the script does not time out - the success prompt gets produced instantaneously.

If anyone experienced a similar issue, your input would be very much appreciated.

The wheels keep spinning

Upvotes: 2

Views: 4595

Answers (2)

Sunny Chaudhari
Sunny Chaudhari

Reputation: 430

I also faced the same issue while connecting to Redshift using psycopg2 using my lambda function.

Solution -

  1. psycopg2.connect(...) - the host parameter in this connection string should have only the host url without db name and port number, since the other parameters like dbname, port, user, password holds that information separately within the connection string.
  2. Under your network and security settings of Redshift cluster, you have to provide public access to your redshift cluster
  3. The security group attached to your redshift cluster should have access to redshift port which is enabled, which you can set by going to inbound rule.

After this you can try testing your lambda function your psycopg2.connect(...) will create successful connection using your lambda function.

Cheers!

Upvotes: 0

Mark
Mark

Reputation: 471

If your script hangs on the psycopg2.connect(...) call, than the security group associated with your lambda function, is not allowed access to the Postgres database instance.

Check the security group associated with the database it should have an Ingress which grants access to the security group associated with your lambda.

      "DatabaseSecurityGroupIngress": [
      {
        "IpProtocol": "tcp",
        "FromPort": "5432",
        "ToPort": "5432",
        "SourceSecurityGroupId": {
          "Ref": "LambdaSecurityGroup"
        }
      ]

Upvotes: 2

Related Questions