berenddeboer / cdk-rds-sql

A CDK construct that allows creating roles and databases an on Aurora Serverless Postgresql cluster.
Apache License 2.0
23 stars 11 forks source link

Database does not exist error when using alternate root username #4

Closed RedbackThomson closed 6 months ago

RedbackThomson commented 1 year ago

When creating an rds.DatabaseCluster and a Role with the option credentials: rds.Credentials.fromGeneratedSecret("my-username"),, the Lambda function returns the following error:

Received response status [FAILED] from custom resource. Message returned: database "my-username" does not exist

I believe this is happening because the database config when establishing a client uses event.ResourceProperties.Database. However, the Role custom resource doesn't define a Database property in its outputs to the handler. The default fallback for the pg.Client object is to use the username. For Aurora Postgres, the default database name is postgres and not the username.

To fix this, the custom resource should try to use the following for the value of ResourceProperties.Database:

The current implementation only works if credentials is not set on the rds.DatabaseCluster and it defaults to postgres, or if the generated username happens to also be postgres

berenddeboer commented 6 months ago

This was fixed in a recent release.