From 7d47cfb39ba40a223ccc511e5706f471b9225c52 Mon Sep 17 00:00:00 2001 From: Hyeonsoo David Lee Date: Tue, 7 Jul 2020 03:28:07 +0900 Subject: [PATCH] fix(rds): proxy for db cluster fails with model validation error (#8896) 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* --- packages/@aws-cdk/aws-rds/lib/proxy.ts | 22 +++++++--- .../aws-rds/test/integ.proxy.expected.json | 3 +- packages/@aws-cdk/aws-rds/test/test.proxy.ts | 44 +++++++++++++++---- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/packages/@aws-cdk/aws-rds/lib/proxy.ts b/packages/@aws-cdk/aws-rds/lib/proxy.ts index 4e8112ed9ffbc..9733056d8ed3b 100644 --- a/packages/@aws-cdk/aws-rds/lib/proxy.ts +++ b/packages/@aws-cdk/aws-rds/lib/proxy.ts @@ -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'); } /** diff --git a/packages/@aws-cdk/aws-rds/test/integ.proxy.expected.json b/packages/@aws-cdk/aws-rds/test/integ.proxy.expected.json index d02477bf1a86a..10e7b2afb02f8 100644 --- a/packages/@aws-cdk/aws-rds/test/integ.proxy.expected.json +++ b/packages/@aws-cdk/aws-rds/test/integ.proxy.expected.json @@ -554,7 +554,8 @@ { "Ref": "dbInstance4076B1EC" } - ] + ], + "TargetGroupName": "default" } } } diff --git a/packages/@aws-cdk/aws-rds/test/test.proxy.ts b/packages/@aws-cdk/aws-rds/test/test.proxy.ts index f0b6bfbf0c91f..2ec6d93f6eb0a 100644 --- a/packages/@aws-cdk/aws-rds/test/test.proxy.ts +++ b/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'; @@ -67,6 +67,7 @@ export = { Ref: 'InstanceC1063A87', }, ], + TargetGroupName: 'default', }, }, ResourcePart.CompleteDefinition)); @@ -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(); + }, };