Skip to content

Commit

Permalink
fix(rds): DatabaseCluster.instanceEndpoints doesn't include writer …
Browse files Browse the repository at this point in the history
…endpoint (#29337)

### Issue # (if applicable)

Closes #29279.

### Reason for this change

`DatabaseCluster.instanceEndpoints` should include writer's endpoint but doesn't.

### Description of changes

Add writer's endpoint to `DatabaseCluster.instanceEndpoints`

### Description of how you validated changes

Add unit tests

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
badmintoncryer authored Mar 8, 2024
1 parent 3bb2944 commit ca59616
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/aws-cdk-lib/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
97 changes: 97 additions & 0 deletions packages/aws-cdk-lib/aws-rds/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca59616

Please sign in to comment.