How to generate a database URI from an AWS Secret

A quick note about how to generate a database URI (or any other derived string) from an AWS SecretsManager SecretTargetAttachment (such as what’s provided via a RDS DatabaseInstance’s secret property).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
db = rds.DatabaseInstance(
    # ...
)
db_val = lambda field: db.secret.secret_value_from_json(field).to_string()
task_definition.add_container(
    environment=dict(
        # ...
        PGRST_DB_URI=f"postgres://{db_val('username')}:{db_val('password')}@{db_val('host')}:{db_val('port')}/",
    ),
    # ...
)