From 16de83b33a615a931b622cf9755faaa6fbf952c6 Mon Sep 17 00:00:00 2001 From: Put Cheung Date: Mon, 2 Dec 2024 15:33:59 +0000 Subject: [PATCH] Updated README with DatabaseInstance.fromLookup --- packages/aws-cdk-lib/aws-rds/README.md | 17 +++++++++++++++++ .../aws-rds/test/instance.from-lookup.test.ts | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-rds/README.md b/packages/aws-cdk-lib/aws-rds/README.md index f57a0109287bd..105bb1137aa5d 100644 --- a/packages/aws-cdk-lib/aws-rds/README.md +++ b/packages/aws-cdk-lib/aws-rds/README.md @@ -1421,3 +1421,20 @@ new rds.DatabaseCluster(this, 'Cluster', { monitoringRole, }); ``` + +## Importing existing DatabaseInstance + +### Lookup DatabaseInstance by instanceIdentifier + +You can lookup an existing DatabaseInstance by its instanceIdentifier using `DatabaseInstance.fromLookup()`. This method returns an `IDatabaseInstance`. + +Here's how `DatabaseInstance.fromLookup()` can be used: + +```ts +const instance = rds.DatabaseInstance.fromLookup(stack, 'MyInstance', { + instanceIdentifier: 'instance-1', +}); + +// Add the new security group to the existing security groups of the RDS instance +instance.connections.addSecurityGroup(myNewSecurityGroup); +``` diff --git a/packages/aws-cdk-lib/aws-rds/test/instance.from-lookup.test.ts b/packages/aws-cdk-lib/aws-rds/test/instance.from-lookup.test.ts index c0a48b7c7a12c..ce3f4b9682f88 100644 --- a/packages/aws-cdk-lib/aws-rds/test/instance.from-lookup.test.ts +++ b/packages/aws-cdk-lib/aws-rds/test/instance.from-lookup.test.ts @@ -17,7 +17,7 @@ describe('DatabaseInstanceBase from lookup', () => { }); const stack = new Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } }); - const instance = rds.DatabaseInstance.fromLookup(stack, 'Key', { + const instance = rds.DatabaseInstance.fromLookup(stack, 'MyInstance', { instanceIdentifier: 'instance-1', }); @@ -42,7 +42,7 @@ describe('DatabaseInstanceBase from lookup', () => { }); const stack = new Stack(undefined, undefined, { env: { region: 'us-east-1', account: '123456789012' } }); - const instance = rds.DatabaseInstance.fromLookup(stack, 'Key', { + const instance = rds.DatabaseInstance.fromLookup(stack, 'MyInstance', { instanceIdentifier: 'instance-1', });