I have an ecs task which is meant to run a database schema migration on an aurora serverless v1 in aws. According to most of the examples I've seen, the connection is enabled by saving the database url with the username and password in the alembic configuration (https://ahmed-nafies.medium.com/fastapi-with-sqlalchemy-postgresql-and-alembic-and-of-course-docker-f2b7411ee396). My aurora databse has a password saved in aws secret manager. As an initial test, I hardcoded the username and password in the databse url and saved them in the .env file in the root directory of my application like so:
DATABASE_URL = postgresql+psycopg2://user:password@host_url:5432/test_db
At first I ran into an interpolation error because the credentials contained percentage sings, so I encoded the url in the env.py file of the alembic directory like this:
db_url_escaped = os.environ["DATABASE_URL"].replace('%', '%%')
config.set_main_option('sqlalchemy.url', db_url_escaped)
That got me past the interpolation error, but then I got the following error saying my password is invalid:
FATAL: password authentication failed for user "XXXXX"
I double checked that the same password as the one saved in secret manager is hardcoded into the url, and noticed that there are other special characters like \ / and =. I'm not sure if this is causing the issue.
Is there any way to error proof this interpolation, and if so does anyone have an example of retrieving a db password from secret manager and using it in a psycopg2 connection string?
Any help would be much appreciated.