1

There are multiple questions/answers for this but none of the solutions seem to work.

My current setup is:

  • Lambda (with python 2.7 runtime) in VPC
  • RDS instance (postgres) in same VPC
  • Lambda role has policy read/write to certain S3 buckets and AWSLambdaVPCAccessExecutionRole
  • Lambda subnet's route table has access to local IPs, and to a VPC endpoint for S3
  • RDS subnet's route table has access to local IPs
  • Lambda security group (SG) has inbound & outbound rules for Postgres & VPC endpoint
  • RDS SG has inbound & outbound rules for the Lambda SG id

I'm using psycopg2 to connect, which is in my requirements.txt file. I tried a simple SELECT query as below:

conn = psycopg2.connect(dbname=os.environ['DB_NAME'], user=os.environ['USERNAME'], password=os.environ['PASSWORD'])
cursor = conn.cursor()
query = "SELECT * from {};".format(os.environ['TABLE'])
cursor.execute(query)
records = cursor.fetchall()

Which gives me the error:

could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
: OperationalError
Traceback (most recent call last):
File "/var/task/mylambda.py", line 31, in call
conn = psycopg2.connect(dbname=os.environ['DB_NAME'], 
user=os.environ['USERNAME'], password=os.environ['PASSWORD'])
File "/tmp/sls-py-req/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

What am I missing? How can I successfully connect to RDS from my Lambda? Thanks!

Katherine
  • 2,086
  • 1
  • 14
  • 23
  • If you run this on an EC2 instance in the same subnet as the Lambda function and with the same SG, can you connect? – jarmod Apr 04 '18 at 14:47
  • 3
    Your code snippet doesn't show values for `host` or `port` in the `connect()` call. Did you simply not copy them into the question, or are they not present in your actual code? – kdgregory Apr 04 '18 at 18:02
  • @kdgregory oh gosh you're right! I'll add it in and see if that fixes it. – Katherine Apr 04 '18 at 20:05
  • @kdgregory that was it! I was looking at it for far too long and completely missed it. I even had host included in my environment variables but didn't think that it was missing in my code. Thanks! – Katherine Apr 04 '18 at 20:11
  • 2
    @Katherine, the giveaway here was in the error message... `Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?` ...this gives you two pieces of information that indicate your code was not trying to connect to a separate server: "Is the server running locally?" You weren't trying to connect to a local server, you were trying to connect to a remote one... and a *"unix domain socket"* never connects you to a different machine, only the local one. – Michael - sqlbot Apr 04 '18 at 22:35
  • 1
    That makes perfect sense @Michael-sqlbot, thank you for that explanation. – Katherine Apr 05 '18 at 08:50

0 Answers0