From 23f69a56fe3842e8d8e3c811e6635cd9f8255eb0 Mon Sep 17 00:00:00 2001 From: BLasan Date: Thu, 25 Mar 2021 14:35:03 +0530 Subject: [PATCH] fix(rds): fail with a descriptive error if Clusters instance count is a deploy-time value fixes #13558 --- packages/@aws-cdk/aws-rds/lib/cluster.ts | 3 +++ .../@aws-cdk/aws-rds/test/cluster.test.ts | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/@aws-cdk/aws-rds/lib/cluster.ts b/packages/@aws-cdk/aws-rds/lib/cluster.ts index a3f06555afdb9..24bb5944b8a85 100644 --- a/packages/@aws-cdk/aws-rds/lib/cluster.ts +++ b/packages/@aws-cdk/aws-rds/lib/cluster.ts @@ -651,6 +651,9 @@ interface InstanceConfig { */ function createInstances(cluster: DatabaseClusterNew, props: DatabaseClusterBaseProps, subnetGroup: ISubnetGroup): InstanceConfig { const instanceCount = props.instances != null ? props.instances : 2; + if (Token.isUnresolved(instanceCount)) { + throw new Error('The number of instances a RDS Cluster consists of cannot be provided as a deploy-time only value!'); + } if (instanceCount < 1) { throw new Error('At least one instance is required'); } diff --git a/packages/@aws-cdk/aws-rds/test/cluster.test.ts b/packages/@aws-cdk/aws-rds/test/cluster.test.ts index 74f538be5d998..80c2603973a25 100644 --- a/packages/@aws-cdk/aws-rds/test/cluster.test.ts +++ b/packages/@aws-cdk/aws-rds/test/cluster.test.ts @@ -54,6 +54,15 @@ describe('cluster', () => { }); + test('check instanceCount is a token', () => { + const stack = testStack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + expect(() => { + createRDSCluster(stack, vpc); + }).toThrow('The number of instances a RDS Cluster consists of cannot be provided as a deploy-time only value!'); + + }); + test('can create a cluster with a single instance', () => { // GIVEN const stack = testStack(); @@ -1959,3 +1968,14 @@ function testStack(app?: cdk.App) { stack.node.setContext('availability-zones:12345:us-test-1', ['us-test-1a', 'us-test-1b']); return stack; } + +function createRDSCluster(stack: any, vpc: any) { + new DatabaseCluster(stack, 'Database', { + instances: cdk.Token.asNumber('1'), + engine: DatabaseClusterEngine.AURORA, + instanceProps: { + instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL), + vpc, + }, + }); +}