3
votes

I'm trying to connect to my Redshift database from my AWS Lambda function:

con = psycopg2.connect(
    dbname="my_dbname",
    host="my_url",
    port= 5439,
    user="username",
    password="my_password")

cur = con.cursor()

But I can't access my database, my function raises the following error:

OperationalError: could not connect to server: Connection timed out
    Is the server running on host "my_url" (54.217.83.88) and accepting
    TCP/IP connections on port 5439?

Can i get some help with this please ? (and if possible I'd like to have a very detailed answer, because I'm new to AWS )

PS: I know that I have to configure the VPC, but I don't know what to do exactly

Thanks you in advance

1

1 Answers

3
votes

Your goal is to have the AWS Lambda function communicate with the Amazon Redshift cluster within the same VPC, via private IP address. This keeps the traffic within the VPC.

  • The AWS Lambda function will need to be configured to connect to a private subnet in the same VPC as the Amazon Redshift cluster.

See: Configuring a Lambda Function to Access Resources in an Amazon VPC

  • Either connect to the Private IP address of the cluster or (preferably) follow the directions on Managing Clusters in an Amazon Virtual Private Cloud (VPC) to enable DNS Hostnames and DNS Resolution on the VPC so that the host name will automatically resolve to the Private IP address.

  • The Security Group associated with the Amazon Redshift cluster will need to permit inbound traffic on port 5439 from the CIDR range of the the VPC (or as appropriate).

See: Amazon Redshift Cluster Security Groups