Skip to content

Commit

Permalink
fix(rds): Cluster does not work with imported VPC (#7666)
Browse files Browse the repository at this point in the history
Specific subnet group names in an imported VPC may be missing
from the Dummy version of the imported VPC, leading to an exception
which aborts.

Turn the exception into a Construct error, which simply prevents deployment
of an incomplete construct instead of a complete abort.

Fixes #6115.
  • Loading branch information
rix0rrr authored Apr 30, 2020
1 parent 39eeb42 commit 95c66a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class DatabaseCluster extends DatabaseClusterBase {

// Cannot test whether the subnets are in different AZs, but at least we can test the amount.
if (subnetIds.length < 2) {
throw new Error(`Cluster requires at least 2 subnets, got ${subnetIds.length}`);
this.node.addError(`Cluster requires at least 2 subnets, got ${subnetIds.length}`);
}

const subnetGroup = new CfnDBSubnetGroup(this, 'Subnets', {
Expand Down
31 changes: 30 additions & 1 deletion packages/@aws-cdk/aws-rds/test/test.cluster.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import { expect, haveResource, ResourcePart, SynthUtils } from '@aws-cdk/assert';
import * as ec2 from '@aws-cdk/aws-ec2';
import { ManagedPolicy, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
Expand Down Expand Up @@ -1023,6 +1023,35 @@ export = {

test.done();
},

'does not throw (but adds a node error) if a (dummy) VPC does not have sufficient subnets'(test: Test) {
// GIVEN
const stack = testStack();
const vpc = ec2.Vpc.fromLookup(stack, 'VPC', { isDefault: true });

// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA,
instances: 1,
masterUser: {
username: 'admin',
},
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
vpcSubnets: {
subnetName: 'DefinitelyDoesNotExist',
},
},
});

// THEN
const art = SynthUtils.synthesize(stack);
const meta = art.findMetadataByType('aws:cdk:error');
test.equal(meta[0].data, 'Cluster requires at least 2 subnets, got 0');

test.done();
},
};

function testStack() {
Expand Down

0 comments on commit 95c66a7

Please sign in to comment.