2

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

2
  • 1
    Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Use the "edit" link to improve your question - do not add more information via comments. Thanks! Commented Aug 31, 2017 at 12:14
  • Duplicate I think : stackoverflow.com/questions/31809890/… Commented Aug 31, 2017 at 12:15

2 Answers 2

2

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"
        }
      ]
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

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.