How to set TLS for a RDS proxy with SQLAlchemy

Viewed 3315

I have an AWS Lambda function (Python3.8) in which I am trying to connect to a RDS proxy using SQLAlchemy. I've confirmed that the function configuration will allow for this by also connecting to the proxy directly using PyMySQL. When I run the function, I get an error message of "(pymysql.err.InternalError) (3159, 'This RDS Proxy requires TLS connections')\n(Background on this error at: http://sqlalche.me/e/2j85)". The "background" for that error says nothing about TLS. I understand what I need to do (tell SQLAlchemy to connect using SSL/TLS), but I cannot figure out the syntax to do so. Below is my current code.

import pymysql
import sqlalchemy

from database_info import make_connection_str

print('connecting to database')
CONN_STR = make_connection_str()
ENGINE = sqlalchemy.create_engine(CONN_STR)
METADATA = sqlalchemy.MetaData(ENGINE)
TABLE = sqlalchemy.Table('active_prospect', METADATA, autoload=True) #error comes with this line

Things I've tried have been related to the following

Based on those two things, I've tried every combination of (ssl-mode, ssl) = ('true', 'required') in two different ways.

  • CONN_STR += '?ssl-mode=REQUIRED'
  • create_engine(CONN_STR, connect_args={'ssl': 'True'})

Results have varied. In some cases the error above has been replaced by AttributeError: 'str' object has no attribute 'get', still being raised from the same line.

1 Answers

I faced the same issue when i was trying to connect to RDS using RDS proxy from lambda. Got fixed by

  1. In lambda configuration, you will see the section 'Database proxies'. Go to 'Add database proxy' and add the proxy here. Basically, it will modify the IAM Role of the lambda to connect to proxy.
  2. In lambda code, I just replaced the host to the proxy endpoint (it worked without SSL argument)
Related