I am connecting to Amazon RDS with Sqlalchemy using a Boto3 generated auth-token:
self.client = boto3.client("rds", region_name="eu-central-1")
self.token = self.client.generate_db_auth_token(
self.endpoint, self.port, self.user
)
connection_string = (
f"postgresql://{urlquote(self.user)}:"
f"{urlquote(self.token)}@{self.endpoint}:{self.port}/dummy"
)
self.engine = db.create_engine(connection_string)
The problem is that the provided token is only valid for 15 minutes (I want to use temporary credentials!) and i don't now how i can tell SQLAlchemy to automatically create a new authentication token when the old one expires.
I found the creator argument, but this function seems not to be called when the token gets invalid:
def get_new_connection(self):
self.token = self.client.generate_db_auth_token(
self.endpoint, self.port, self.user
)
conn = psycopg2.connect(
f"dbname='dummy' user='{self.user}' host='{self.endpoint}' password='{self.token}'"
)
return conn
self.engine = db.create_engine(
connection_string, creator=self.get_new_connection
)
Is there a build in function for this, or a more elegant way to solve this?