diff --git a/packages/aws-cdk-lib/aws-rds/lib/cluster.ts b/packages/aws-cdk-lib/aws-rds/lib/cluster.ts index 72b4a9cb72af2..41bcc57d35a6c 100644 --- a/packages/aws-cdk-lib/aws-rds/lib/cluster.ts +++ b/packages/aws-cdk-lib/aws-rds/lib/cluster.ts @@ -756,6 +756,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 8a21c2f744cb1..7563065fd0f10 100644 --- a/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts +++ b/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts @@ -528,6 +528,103 @@ describe('cluster new api', () => { }); }); + describe('instanceEndpoints', () => { + test('should contain writer and reader instance endpoints at DatabaseCluster', () => { + //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'] }, + ]], + }, + }]); + }); + + 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', () => { test('serverless reader in promotion tier 2 throws warning', () => { // GIVEN