Skip to content

Commit

Permalink
fix(rds): add the dependency on proxy targets to ensure dbInstance av…
Browse files Browse the repository at this point in the history
…ailability while creating proxy

fixes: aws#11311
  • Loading branch information
Saud Khanzada committed Dec 26, 2020
1 parent 09d1f6c commit 673fc90
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/@aws-cdk/aws-rds/lib/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,16 @@ export class DatabaseProxy extends cdk.Resource
let dbInstanceIdentifiers: string[] | undefined;
if (bindResult.dbInstances) {
// support for only single instance
dbInstanceIdentifiers = [bindResult.dbInstances[0].instanceIdentifier];
const dbInstance = bindResult.dbInstances[0];
this.node.addDependency(dbInstance);
dbInstanceIdentifiers = [dbInstance.instanceIdentifier];
}

let dbClusterIdentifiers: string[] | undefined;
if (bindResult.dbClusters) {
dbClusterIdentifiers = bindResult.dbClusters.map((c) => c.clusterIdentifier);
const dbClusters = bindResult.dbClusters;
dbClusters.forEach((cluster) => this.node.addDependency(cluster));
dbClusterIdentifiers = dbClusters.map((c) => c.clusterIdentifier);
}

if (!!dbInstanceIdentifiers && !!dbClusterIdentifiers) {
Expand Down
12 changes: 8 additions & 4 deletions packages/@aws-cdk/aws-rds/test/integ.proxy.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@
],
"Version": "2012-10-17"
}
}
},
"DependsOn": ["dbInstance4076B1EC","dbInstanceSecretAttachment88CFBDAE","dbInstanceSecret032D3661","dbInstanceSecurityGroupA58A00A3","dbInstanceSubnetGroupD062EC9E"]
},
"dbProxyIAMRoleDefaultPolicy99AB98F3": {
"Type": "AWS::IAM::Policy",
Expand All @@ -506,7 +507,8 @@
"Ref": "dbProxyIAMRole662F3AB8"
}
]
}
},
"DependsOn": ["dbInstance4076B1EC","dbInstanceSecretAttachment88CFBDAE","dbInstanceSecret032D3661","dbInstanceSecurityGroupA58A00A3","dbInstanceSubnetGroupD062EC9E"]
},
"dbProxy3B89EAF2": {
"Type": "AWS::RDS::DBProxy",
Expand Down Expand Up @@ -537,7 +539,8 @@
"Ref": "vpcPrivateSubnet2Subnet7031C2BA"
}
]
}
},
"DependsOn": ["dbInstance4076B1EC","dbInstanceSecretAttachment88CFBDAE","dbInstanceSecret032D3661","dbInstanceSecurityGroupA58A00A3","dbInstanceSubnetGroupD062EC9E"]
},
"dbProxyProxyTargetGroup8DA26A77": {
"Type": "AWS::RDS::DBProxyTargetGroup",
Expand All @@ -555,7 +558,8 @@
}
],
"TargetGroupName": "default"
}
},
"DependsOn": ["dbInstance4076B1EC","dbInstanceSecretAttachment88CFBDAE","dbInstanceSecret032D3661","dbInstanceSecurityGroupA58A00A3","dbInstanceSubnetGroupD062EC9E"]
}
}
}
41 changes: 40 additions & 1 deletion packages/@aws-cdk/aws-rds/test/test.proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ABSENT, expect, haveResourceLike } from '@aws-cdk/assert';
import { ABSENT, expect, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import * as cdk from '@aws-cdk/core';
Expand Down Expand Up @@ -223,4 +223,43 @@ export = {

test.done();
},

'proxy should have dependency on target'(test: Test) {
// GIVEN
const cluster = new rds.DatabaseCluster(stack, 'cluster', {
engine: rds.DatabaseClusterEngine.AURORA,
instanceProps: {
vpc,
},
});

//When
new rds.DatabaseProxy(stack, 'proxy', {
proxyTarget: rds.ProxyTarget.fromCluster(cluster),
borrowTimeout: cdk.Duration.seconds(30),
secrets: [cluster.secret!],
maxConnectionsPercent: 50,
vpc,
});

// THEN
expect(stack).to(haveResourceLike('AWS::RDS::DBProxy', {
Type: 'AWS::RDS::DBProxy',
Properties: {
DBProxyName: 'proxy',
EngineFamily: 'MYSQL',
},
DependsOn: [
'clusterInstance183584D40',
'clusterInstance23D1AD8B2',
'cluster611F8AFF',
'clusterSecretAttachment69BFCEC4',
'clusterSecretE349B730',
'clusterSecurityGroupF441DCEA',
'clusterSubnets81E3593F',
],
}, ResourcePart.CompleteDefinition));

test.done();
},
};

0 comments on commit 673fc90

Please sign in to comment.