From 95c66a7500efd83ae4d840c97b0e7663689fe401 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Thu, 30 Apr 2020 07:19:53 +0200 Subject: [PATCH] fix(rds): Cluster does not work with imported VPC (#7666) 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. --- packages/@aws-cdk/aws-rds/lib/cluster.ts | 2 +- .../@aws-cdk/aws-rds/test/test.cluster.ts | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-rds/lib/cluster.ts b/packages/@aws-cdk/aws-rds/lib/cluster.ts index a43cf1c6b87f0..b3196514f46ce 100644 --- a/packages/@aws-cdk/aws-rds/lib/cluster.ts +++ b/packages/@aws-cdk/aws-rds/lib/cluster.ts @@ -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', { diff --git a/packages/@aws-cdk/aws-rds/test/test.cluster.ts b/packages/@aws-cdk/aws-rds/test/test.cluster.ts index 2bfeb3c7aed33..f2f420d72b415 100644 --- a/packages/@aws-cdk/aws-rds/test/test.cluster.ts +++ b/packages/@aws-cdk/aws-rds/test/test.cluster.ts @@ -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'; @@ -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() {