Skip to content

Commit

Permalink
fix(rds): proxy for db cluster fails with model validation error (#8896)
Browse files Browse the repository at this point in the history
fixes #8885
follows #8476

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
civilizeddev committed Jul 6, 2020
1 parent 841060d commit 7d47cfb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
22 changes: 16 additions & 6 deletions packages/@aws-cdk/aws-rds/lib/proxy.ts
Expand Up @@ -408,20 +408,30 @@ export class DatabaseProxy extends cdk.Resource
this.endpoint = this.resource.attrEndpoint;

let dbInstanceIdentifiers: string[] | undefined;
if (bindResult.dbClusters) {
// support for only instances of a single cluster
dbInstanceIdentifiers = bindResult.dbClusters[0].instanceIdentifiers;
} else if (bindResult.dbInstances) {
if (bindResult.dbInstances) {
// support for only single instance
dbInstanceIdentifiers = [ bindResult.dbInstances[0].instanceIdentifier ];
}

new CfnDBProxyTargetGroup(this, 'ProxyTargetGroup', {
let dbClusterIdentifiers: string[] | undefined;
if (bindResult.dbClusters) {
dbClusterIdentifiers = bindResult.dbClusters.map((c) => c.clusterIdentifier);
}

if (!!dbInstanceIdentifiers && !!dbClusterIdentifiers) {
throw new Error('Cannot specify both dbInstanceIdentifiers and dbClusterIdentifiers');
}

const proxyTargetGroup = new CfnDBProxyTargetGroup(this, 'ProxyTargetGroup', {
dbProxyName: this.dbProxyName,
dbInstanceIdentifiers,
dbClusterIdentifiers: bindResult.dbClusters?.map((c) => c.clusterIdentifier),
dbClusterIdentifiers,
connectionPoolConfigurationInfo: toConnectionPoolConfigurationInfo(props),
});

// Currently(2020-07-04), this property must be set to default.
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbproxytargetgroup.html#TargetGroupName-fn::getatt
proxyTargetGroup.addOverride('Properties.TargetGroupName', 'default');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-rds/test/integ.proxy.expected.json
Expand Up @@ -554,7 +554,8 @@
{
"Ref": "dbInstance4076B1EC"
}
]
],
"TargetGroupName": "default"
}
}
}
Expand Down
44 changes: 35 additions & 9 deletions packages/@aws-cdk/aws-rds/test/test.proxy.ts
@@ -1,4 +1,4 @@
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import { ABSENT, expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
Expand Down Expand Up @@ -67,6 +67,7 @@ export = {
Ref: 'InstanceC1063A87',
},
],
TargetGroupName: 'default',
},
}, ResourcePart.CompleteDefinition));

Expand Down Expand Up @@ -140,17 +141,42 @@ export = {
Ref: 'DatabaseB269D8BB',
},
],
DBInstanceIdentifiers: [
{
Ref: 'DatabaseInstance1844F58FD',
},
{
Ref: 'DatabaseInstance2AA380DEE',
},
],
TargetGroupName: 'default',
},
}, ResourcePart.CompleteDefinition));

test.done();
},

'Cannot specify both dbInstanceIdentifiers and dbClusterIdentifiers'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new rds.DatabaseCluster(stack, 'Database', {
engine: rds.DatabaseClusterEngine.AURORA_POSTGRESQL,
engineVersion: '10.7',
masterUser: {
username: 'admin',
},
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
},
});

// WHEN
test.doesNotThrow(() => {
new rds.DatabaseProxy(stack, 'Proxy', {
proxyTarget: rds.ProxyTarget.fromCluster(cluster),
secret: cluster.secret!,
vpc,
});
}, /Cannot specify both dbInstanceIdentifiers and dbClusterIdentifiers/);

expect(stack).to(haveResource('AWS::RDS::DBProxyTargetGroup', {
DBInstanceIdentifiers: ABSENT,
}, ResourcePart.Properties));

test.done();
},
};

0 comments on commit 7d47cfb

Please sign in to comment.