connecting to Postgres from Chalice
Hi,
I am running into an issue while using Chalice. I have created an RDS Postgres instance. I can connect to it using a jdbc client (DBeaver) but when trying to connect with Chalice, the connection times out. I think the issue is an IAM issue, but I am still trying to understand Lambda/Chalice so I am certain this is an operator error.
The relevant code:
import psycopg2
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
from chalice import Chalice
app = Chalice(app_name='postgres')
db_host = "postgresforlambdatest2.cy3q1kav1wnd.us-east-1.rds.amazonaws.com"
db_port = 5432
db_name = "PostgresLambdaTest2"
db_user = "admin"
db_pass = "xxxxxxxxxx"
db_table = "employee"
@app.route('/')
def index():
logger.info('hello...')
return {'hello': 'world'}
@app.route('/fetch')
def fetch_data():
logger.info('Making a connection...')
conn = make_conn() # this is were we are timing out...
logger.info('Connection made...')
# query = "select * from employee"
# result = []
# print ("Now executing: %s" % (query))
# cursor = conn.cursor()
# cursor.execute(query)
# raw = cursor.fetchall()
# for line in raw:
# result.append(line)
return {'result': 'ok'}
def make_conn():
conn = None
try:
logger.info('starting connection...')
conn = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (db_name, db_user, db_host, db_pass))
logger.info('made connection...')
except:
logger.error('I am unable to connect to the database')
return conn
I have also confirmed that I can run this code outside of chalice just using Python and the connection works.
Is the RDS instance deployed into a VPC, and/or do you have the security groups set to allow ingress from specific ip addresses only?
HI Kadrach, it is deployed in a VPC but as far as I know, it's completely open. I can connect to it from DBeaver from my machine and running the code above as a regular Python program creates the connection. I can share the login and password if this would help. I am not sure that I want to post it in an open forum though. I can be reached at [email protected]
@alberto33 That shouldn't be necessary. This being possible from your workstation (and not in Lambda) is an issue with the network configuration (VPCs, route tables, security groups) somewhere. Is your setup a vanilla VPC, or are you connecting to your database through corporate networks that may use e.g. DirectConnect?
Are you deploying the Lambda function into a VPC as well?
My setup for the database is completely vanilla. I just chose the default VPC. The account I have is not within a corporate VPC. I am not using DirectConnect.
Lambda is not deployed into a VPC. It has no VPC assigned.
Hi Kadrach,
I was able to get this issue resolved by doing three things:
- Giving the role associated with the lambda function access to AmazonVPCReadOnlyAccess and AWSLambdaVPCAccessExecutionRole
- configuring the lambda to belong to the same subnets and security groups as the RDS.
- change the Postgres security groups to be able to connect to the default security group.