diff --git a/packages/@aws-cdk/aws-rds/lib/cluster.ts b/packages/@aws-cdk/aws-rds/lib/cluster.ts index b928e34a74962..faedab239f82f 100644 --- a/packages/@aws-cdk/aws-rds/lib/cluster.ts +++ b/packages/@aws-cdk/aws-rds/lib/cluster.ts @@ -298,8 +298,6 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase { * Never undefined. */ public readonly engine?: IClusterEngine; - public readonly instanceIdentifiers: string[] = []; - public readonly instanceEndpoints: Endpoint[] = []; protected readonly newCfnProps: CfnDBClusterProps; protected readonly securityGroups: ec2.ISecurityGroup[]; @@ -481,6 +479,8 @@ export class DatabaseCluster extends DatabaseClusterNew { public readonly clusterEndpoint: Endpoint; public readonly clusterReadEndpoint: Endpoint; public readonly connections: ec2.Connections; + public readonly instanceIdentifiers: string[]; + public readonly instanceEndpoints: Endpoint[]; /** * The secret attached to this cluster @@ -533,7 +533,9 @@ export class DatabaseCluster extends DatabaseClusterNew { } setLogRetention(this, props); - createInstances(this, props, this.subnetGroup); + const createdInstances = createInstances(this, props, this.subnetGroup); + this.instanceIdentifiers = createdInstances.instanceIdentifiers; + this.instanceEndpoints = createdInstances.instanceEndpoints; } /** @@ -602,6 +604,8 @@ export class DatabaseClusterFromSnapshot extends DatabaseClusterNew { public readonly clusterEndpoint: Endpoint; public readonly clusterReadEndpoint: Endpoint; public readonly connections: ec2.Connections; + public readonly instanceIdentifiers: string[]; + public readonly instanceEndpoints: Endpoint[]; constructor(scope: Construct, id: string, props: DatabaseClusterFromSnapshotProps) { super(scope, id, props); @@ -625,7 +629,9 @@ export class DatabaseClusterFromSnapshot extends DatabaseClusterNew { cluster.applyRemovalPolicy(props.removalPolicy ?? RemovalPolicy.SNAPSHOT); setLogRetention(this, props); - createInstances(this, props, this.subnetGroup); + const createdInstances = createInstances(this, props, this.subnetGroup); + this.instanceIdentifiers = createdInstances.instanceIdentifiers; + this.instanceEndpoints = createdInstances.instanceEndpoints; } } diff --git a/packages/@aws-cdk/aws-rds/test/cluster.test.ts b/packages/@aws-cdk/aws-rds/test/cluster.test.ts index 25400f0b17964..9c743b811c818 100644 --- a/packages/@aws-cdk/aws-rds/test/cluster.test.ts +++ b/packages/@aws-cdk/aws-rds/test/cluster.test.ts @@ -76,7 +76,7 @@ describe('cluster', () => { const vpc = new ec2.Vpc(stack, 'VPC'); // WHEN - new DatabaseCluster(stack, 'Database', { + const cluster = new DatabaseCluster(stack, 'Database', { engine: DatabaseClusterEngine.AURORA, instances: 1, credentials: { @@ -97,6 +97,28 @@ describe('cluster', () => { MasterUserPassword: 'tooshort', VpcSecurityGroupIds: [{ 'Fn::GetAtt': ['DatabaseSecurityGroup5C91FDCB', 'GroupId'] }], }); + + expect(cluster.instanceIdentifiers).toHaveLength(1); + expect(stack.resolve(cluster.instanceIdentifiers[0])).toEqual({ + Ref: 'DatabaseInstance1844F58FD', + }); + + expect(cluster.instanceEndpoints).toHaveLength(1); + expect(stack.resolve(cluster.instanceEndpoints[0])).toEqual({ + hostname: { + 'Fn::GetAtt': ['DatabaseInstance1844F58FD', 'Endpoint.Address'], + }, + port: { + 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'], + }, + socketAddress: { + 'Fn::Join': ['', [ + { 'Fn::GetAtt': ['DatabaseInstance1844F58FD', 'Endpoint.Address'] }, + ':', + { 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] }, + ]], + }, + }); }); test('can create a cluster with imported vpc and security group', () => { @@ -1635,7 +1657,7 @@ describe('cluster', () => { const vpc = new ec2.Vpc(stack, 'VPC'); // WHEN - new DatabaseClusterFromSnapshot(stack, 'Database', { + const cluster = new DatabaseClusterFromSnapshot(stack, 'Database', { engine: DatabaseClusterEngine.aurora({ version: AuroraEngineVersion.VER_1_22_2 }), instanceProps: { vpc, @@ -1659,6 +1681,28 @@ describe('cluster', () => { }, ResourcePart.CompleteDefinition); expect(stack).toCountResources('AWS::RDS::DBInstance', 2); + + expect(cluster.instanceIdentifiers).toHaveLength(2); + expect(stack.resolve(cluster.instanceIdentifiers[0])).toEqual({ + Ref: 'DatabaseInstance1844F58FD', + }); + + expect(cluster.instanceEndpoints).toHaveLength(2); + expect(stack.resolve(cluster.instanceEndpoints[0])).toEqual({ + hostname: { + 'Fn::GetAtt': ['DatabaseInstance1844F58FD', 'Endpoint.Address'], + }, + port: { + 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'], + }, + socketAddress: { + 'Fn::Join': ['', [ + { 'Fn::GetAtt': ['DatabaseInstance1844F58FD', 'Endpoint.Address'] }, + ':', + { 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] }, + ]], + }, + }); }); test('reuse an existing subnet group', () => {