aws / aws/aws-cdk

(aws-rds): database connection string in generated secret (secret manager)

Open
#23,613 2 comments 44 reactions 0 assignees View on GitHub
@aws-cdk/aws-rds feature-request p3
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

It would be handy to have a "ready to use" [connection string](https://www.connectionstrings.com/).
Currently it's a hassle to transform the RDS secret from the secret manager into a connection string.
It involves `unsafeUnwrap` and generating a secret in the Systems Manager Parameter Store based on the values in the Secret Manager.

### Use Case

```ts
var database = new DatabaseInstance(stack, 'my-database', {
// rest omitted ...
});

new ApplicationLoadBalancedFargateService(stack, 'my-fargate', {
taskImageOptions: {
secrets: [{
'ConnectionStrings__MyDatabase': Secret.fromSecretsManager(database.secret, 'connectionString')
}]
},
// rest omitted ...
});
```

### Proposed Solution

A default connection string for each supported database type would nice.

```
// default SQL Server connection string
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
```
However connection strings are highly configurable. So it would be ideal to be able to alter it.
It's actually necessary as the `Database=...;` parameter is application specific and **required**.

```ts
new DatabaseInstance(stack, 'my-database', {
// rest omitted ...
credentials: Credentials.fromGeneratedSecret('admin', {
// {{key}} refers to keys in the generated json secret
// note `Database=MyDatabaseName` is hardcoded as it's not in the secret manager
connectionStringTemplate: 'Server={{host}},{{port}};Database=MyDatabaseName;User Id={{username}};Password={{password}};'
}),
});
```
This way it would be easy to alter the connection sting:
```jsonc
{
connectionStringTemplate: 'Server={{host}},{{port}};Database=MyDatabaseName;User Id={{username}};Password={{password}};MultipleActiveResultSets=True;'
}
```

A version of `Credentials.fromGeneratedSecret` that only accepts the options would also be welcome, because then the `connectionStringTemplate` can be configured without hardcoding a username.
```ts
Credentials.fromGeneratedSecret({
connectionStringTemplate: 'Server={{host}},{{port}};Database=MyDatabaseName;User Id={{username}};Password={{password}};'
})
```

### Other Information

Another solution could be to extract a configurable connection string from the secret manager:
```ts
Secret.connectionStringFromSecretsManager(database.secret, {
// {{key}} refers to keys in the generated json secret
// note `Database=MyDatabaseName` is hardcoded as it's not in the secret manager
connectionStringTemplate: 'Server={{host}},{{port}};Database=MyDatabaseName;User Id={{username}};Password={{password}};'
})
```

Found other people with the same issue:
- https://www.reddit.com/r/aws/comments/rzfskp/amazon_rds_amazon_secrets_manager_retrieving/
- https://catalog.us-east-1.prod.workshops.aws/workshops/5c59492e-fc61-4a7b-b08a-e0307b70f390/en-US/2-sql-server/sql-server
- https://stackoverflow.com/questions/68805472/concatenate-aws-secrets-in-aws-cdk-for-ecs-container

### Acknowledgements

- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

2.55.0

### Environment details (OS name and version, etc.)

Not relevant

Contributor guide

Open the contributing guide

Research direction

Start by reading the DatabaseInstance, Credentials.fromGeneratedSecret, and Secret.fromSecretsManager entry points mentioned in the issue. Compare the proposed template options and supported database types; done means users can obtain configurable, ready-to-use RDS connection strings without unsafeUnwrap or manual Parameter Store transformation.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.