Attach a DatabaseSecret to an DatabaseInstance created by another stack
A way to attach database secrets to an existing RDS instance, without having the RDS instance in the same stack.
Use Case
We have a micro-service architecture where each service uses CDK to setup the infrastructure needed by the specific service. There is also a common project used to setup common resources shared by all microservices (VPC, ALB, etc.). The common project also sets up an RDS instance that is used by each microservice that needs a database. Each microservice has it's own database, but they all share RDS instance.
Each microservice would like to use CDK to create a DatabaseSecret used to authenticate to the service-specific database in the RDS instance. At the moment I can't find a way to attach the DatabaseSecret to the RDS instance unless the RDS instance was created in the same stack.
Proposed Solution
Provide a way to create an ISecretAttachmentTarget pointing to the RDS instance by providing an RDS instance ARN. Sample code in java:
mySecret.attach(DatabaseInstanceSecretAttachmentTarget.fromArn("arn:aws:rds:eu-west-1:..."))
Other
- [ ] :wave: I may be able to implement this feature request
- [ ] :warning: This feature might incur a breaking change
This is a :rocket: Feature Request
Why would you want to attach a secret to an imported instance? Attachement is usually only required when creating both the instance and the secret in the same stack. If your instance already exists in a stack and you expose it as a property then you can consume it in your other stack:
new secretsmanager.Secret(stack, 'Secret', {
generateSecretString: {
secretStringTemplate: JSON.stringify({
username: 'username',
engine: 'mysql',
host: myInstance.instanceEndpoint.hostname, // will create an export in your instance stack
port: myInstance.instanceEndpoint.port, // idem
dbname: 'myDbName'
}),
generateStringKey: 'password'
}
});
From a CloudFormation perspective attaching just means adding DB connection informations in the secret. In your case those are known and using the example above CDK will automatically create the correct export in the instance stack and references in your consuming stack.
DatabaseSecret seemed like a nice high level abstraction on top of the generic Secret class. Using it you don't have to know the internal structure of the JSON created in secrets manager...
But you are right, using the Secret class you can accomplish what I was trying to use the DatabaseSecret for.
@jogold - While the user can always do this, don't you think there's value in providing either the current DatabaseSecret class or a similar one here? For example,
new DatabaseSecret(this, 'secret', {
dbInstance: instance,
username: 'username',
});
Curious why you're opposed to using the DatabaseSecret for this purpose?
@nija-at certainly not opposed to improve the user experience here.
We could indeed add an optional dbInstance prop to DatabaseSecretProps. What about clusters? There's currently no common interface between instance and cluster. If the use case is to create a "user" secret (as opposed to a master secret) that should be rotated then we would have to expose engine and dbName on the instance/cluster because those fields are required.
I think there's something that we can do better here. The DatabaseSecret might not be the correct place.
Keeping this as a feature request.
This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.