Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Redshift from AWS Lambda

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

like image 674
kab Avatar asked Oct 15 '25 21:10

kab


1 Answers

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

like image 136
John Rotenstein Avatar answered Oct 18 '25 09:10

John Rotenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!