From d6843e6a1bae9825ed7a5007a77cf381a9521143 Mon Sep 17 00:00:00 2001 From: Kazuho CryerShinozuka Date: Sat, 2 Mar 2024 01:50:58 +0900 Subject: [PATCH 1/2] fix: add writer endpoint to DatabaseCluster.instanceEndpoints --- packages/aws-cdk-lib/aws-rds/lib/cluster.ts | 1 + .../aws-cdk-lib/aws-rds/test/cluster.test.ts | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/packages/aws-cdk-lib/aws-rds/lib/cluster.ts b/packages/aws-cdk-lib/aws-rds/lib/cluster.ts index b5d531f2dc7d9..68b705055cc5b 100644 --- a/packages/aws-cdk-lib/aws-rds/lib/cluster.ts +++ b/packages/aws-cdk-lib/aws-rds/lib/cluster.ts @@ -726,6 +726,7 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase { promotionTier: 0, // override the promotion tier so that writers are always 0 }); instanceIdentifiers.push(writer.instanceIdentifier); + instanceEndpoints.push(new Endpoint(writer.dbInstanceEndpointAddress, this.clusterEndpoint.port)); (props.readers ?? []).forEach(instance => { const clusterInstance = instance.bind(this, this, { diff --git a/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts b/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts index b368b79e6836c..292957a98edda 100644 --- a/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts +++ b/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts @@ -504,6 +504,55 @@ describe('cluster new api', () => { }); }); + describe('instanceEndpoints', () => { + test('should contain writer and reader instance endpoints', () => { + //GIVEN + const stack = testStack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + + //WHEN + const cluster = new DatabaseCluster(stack, 'Database', { + engine: DatabaseClusterEngine.AURORA, + vpc, + writer: ClusterInstance.serverlessV2('writer'), + readers: [ClusterInstance.serverlessV2('reader')], + iamAuthentication: true, + }); + + //THEN + expect(cluster.instanceEndpoints).toHaveLength(2); + expect(stack.resolve(cluster.instanceEndpoints)).toEqual([{ + hostname: { + 'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'], + }, + port: { + 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'], + }, + socketAddress: { + 'Fn::Join': ['', [ + { 'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'] }, + ':', + { 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] }, + ]], + }, + }, { + hostname: { + 'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'], + }, + port: { + 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'], + }, + socketAddress: { + 'Fn::Join': ['', [ + { 'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'] }, + ':', + { 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] }, + ]], + }, + }]); + }); + }); + describe('provisioned writer with serverless readers', () => { test('serverless reader in promotion tier 2 throws warning', () => { // GIVEN From f5bbc92f568a43982dd7e7c5f5614f666de6fab3 Mon Sep 17 00:00:00 2001 From: Kazuho CryerShinozuka Date: Sat, 2 Mar 2024 01:59:42 +0900 Subject: [PATCH 2/2] test: update unit test --- .../aws-cdk-lib/aws-rds/test/cluster.test.ts | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts b/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts index 292957a98edda..12b0f9ae6d4f7 100644 --- a/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts +++ b/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts @@ -505,7 +505,7 @@ describe('cluster new api', () => { }); describe('instanceEndpoints', () => { - test('should contain writer and reader instance endpoints', () => { + test('should contain writer and reader instance endpoints at DatabaseCluster', () => { //GIVEN const stack = testStack(); const vpc = new ec2.Vpc(stack, 'VPC'); @@ -551,6 +551,54 @@ describe('cluster new api', () => { }, }]); }); + + test('should contain writer and reader instance endpoints at DatabaseClusterFromSnapshot', () => { + //GIVEN + const stack = testStack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + + //WHEN + const cluster = new DatabaseClusterFromSnapshot(stack, 'Database', { + engine: DatabaseClusterEngine.AURORA, + vpc, + snapshotIdentifier: 'snapshot-identifier', + iamAuthentication: true, + writer: ClusterInstance.serverlessV2('writer'), + readers: [ClusterInstance.serverlessV2('reader')], + }); + + //THEN + expect(cluster.instanceEndpoints).toHaveLength(2); + expect(stack.resolve(cluster.instanceEndpoints)).toEqual([{ + hostname: { + 'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'], + }, + port: { + 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'], + }, + socketAddress: { + 'Fn::Join': ['', [ + { 'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'] }, + ':', + { 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] }, + ]], + }, + }, { + hostname: { + 'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'], + }, + port: { + 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'], + }, + socketAddress: { + 'Fn::Join': ['', [ + { 'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'] }, + ':', + { 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] }, + ]], + }, + }]); + }); }); describe('provisioned writer with serverless readers', () => {