-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
cluster.test.ts
4999 lines (4480 loc) · 160 KB
/
cluster.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Annotations, Match, Template } from '../../assertions';
import * as ec2 from '../../aws-ec2';
import { ManagedPolicy, Role, ServicePrincipal } from '../../aws-iam';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as logs from '../../aws-logs';
import * as s3 from '../../aws-s3';
import { Secret } from '../../aws-secretsmanager';
import * as cdk from '../../core';
import { RemovalPolicy, Stack, Annotations as CoreAnnotations } from '../../core';
import {
RDS_PREVENT_RENDERING_DEPRECATED_CREDENTIALS,
AURORA_CLUSTER_CHANGE_SCOPE_OF_INSTANCE_PARAMETER_GROUP_WITH_EACH_PARAMETERS,
} from '../../cx-api';
import {
AuroraEngineVersion, AuroraMysqlEngineVersion, AuroraPostgresEngineVersion, CfnDBCluster, Credentials, DatabaseCluster,
DatabaseClusterEngine, DatabaseClusterFromSnapshot, ParameterGroup, PerformanceInsightRetention, SubnetGroup, DatabaseSecret,
DatabaseInstanceEngine, SqlServerEngineVersion, SnapshotCredentials, InstanceUpdateBehaviour, NetworkType, ClusterInstance, CaCertificate,
IClusterEngine,
} from '../lib';
describe('cluster new api', () => {
describe('errors are thrown', () => {
test('when old and new props are provided', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
expect(() => {
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
},
writer: ClusterInstance.serverlessV2('writer'),
iamAuthentication: true,
});
// THEN
}).toThrow(/Cannot provide writer or readers if instances or instanceProps are provided/);
});
test('when no instances are provided', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
expect(() => {
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
iamAuthentication: true,
});
// THEN
}).toThrow(/writer must be provided/);
});
test('when vpc prop is not provided', () => {
// GIVEN
const stack = testStack();
expect(() => {
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
writer: ClusterInstance.serverlessV2('writer'),
iamAuthentication: true,
});
// THEN
}).toThrow(/Provide either vpc or instanceProps.vpc, but not both/);
});
test('when both vpc and instanceProps.vpc are provided', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
expect(() => {
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
},
vpc,
iamAuthentication: true,
});
// THEN
}).toThrow(/Provide either vpc or instanceProps.vpc, but not both/);
});
test('when both vpcSubnets and instanceProps.vpcSubnets are provided', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
expect(() => {
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpcSubnets: vpc.selectSubnets( { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS } ),
vpc,
},
vpcSubnets: vpc.selectSubnets( { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS } ),
iamAuthentication: true,
});
// THEN
}).toThrow(/Provide either vpcSubnets or instanceProps.vpcSubnets, but not both/);
});
test.each([
[0.5, 300, /serverlessV2MaxCapacity must be >= 0.5 & <= 256/],
[0.5, 0, /serverlessV2MaxCapacity must be >= 0.5 & <= 256/],
[0, 1, /serverlessV2MinCapacity must be >= 0.5 & <= 256/],
[300, 1, /serverlessV2MinCapacity must be >= 0.5 & <= 256/],
[0.5, 0.5, /If serverlessV2MinCapacity === 0.5 then serverlessV2MaxCapacity must be >=1/],
[10.1, 12, /serverlessV2MinCapacity & serverlessV2MaxCapacity must be in 0.5 step increments/],
[12, 12.1, /serverlessV2MinCapacity & serverlessV2MaxCapacity must be in 0.5 step increments/],
[5, 1, /serverlessV2MaxCapacity must be greater than serverlessV2MinCapacity/],
])('when serverless capacity is incorrect', (minCapacity, maxCapacity, errorMessage) => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
expect(() => {
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
vpcSubnets: vpc.selectSubnets( { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS } ),
serverlessV2MaxCapacity: maxCapacity,
serverlessV2MinCapacity: minCapacity,
iamAuthentication: true,
});
// THEN
}).toThrow(errorMessage);
});
});
describe('cluster options', () => {
test.each([true, false])('specify auto minor version upgrade', (autoMinorVersionUpgrade) => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
autoMinorVersionUpgrade,
writer: ClusterInstance.serverlessV2('writer'),
});
// THEN
const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::RDS::DBCluster', {
Engine: 'aurora-mysql',
AutoMinorVersionUpgrade: autoMinorVersionUpgrade,
});
});
test('with serverless instances', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.serverlessV2('writer'),
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
// serverless scaling config is set
template.hasResourceProperties('AWS::RDS::DBCluster', Match.objectLike({
ServerlessV2ScalingConfiguration: {
MinCapacity: 0.5,
MaxCapacity: 2,
},
}));
// subnets are set correctly
template.hasResourceProperties('AWS::RDS::DBSubnetGroup', {
DBSubnetGroupDescription: 'Subnets for Database database',
SubnetIds: [
{ Ref: 'VPCPrivateSubnet1Subnet8BCA10E0' },
{ Ref: 'VPCPrivateSubnet2SubnetCFCDAA7A' },
{ Ref: 'VPCPrivateSubnet3Subnet3EDCD457' },
],
});
});
test.each([
['MySQL', DatabaseClusterEngine.auroraMysql({ version: AuroraMysqlEngineVersion.VER_3_07_0 })],
['PostgreSQL', DatabaseClusterEngine.auroraPostgres({ version: AuroraPostgresEngineVersion.VER_16_4 })],
])('set enableLocalWriteForwarding for aurora %s', (type: string, engine: IClusterEngine) => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, type, {
engine,
vpc,
enableLocalWriteForwarding: true,
writer: ClusterInstance.serverlessV2('writer'),
});
// THEN
const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::RDS::DBCluster', {
EnableLocalWriteForwarding: true,
});
});
test('vpcSubnets can be provided', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
vpcSubnets: vpc.selectSubnets({ subnetType: ec2.SubnetType.PUBLIC }),
writer: ClusterInstance.serverlessV2('writer'),
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
// serverless scaling config is set
template.hasResourceProperties('AWS::RDS::DBCluster', Match.objectLike({
ServerlessV2ScalingConfiguration: {
MinCapacity: 0.5,
MaxCapacity: 2,
},
}));
// subnets are set correctly
template.hasResourceProperties('AWS::RDS::DBSubnetGroup', {
DBSubnetGroupDescription: 'Subnets for Database database',
SubnetIds: [
{ Ref: 'VPCPublicSubnet1SubnetB4246D30' },
{ Ref: 'VPCPublicSubnet2Subnet74179F39' },
{ Ref: 'VPCPublicSubnet3Subnet631C5E25' },
],
});
});
test('preferredMaintenanceWindow provided in InstanceProps', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
const PREFERRED_MAINTENANCE_WINDOW: string = 'Sun:12:00-Sun:13:00';
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
instanceProps: {
vpc: vpc,
preferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
},
});
// THEN
const template = Template.fromStack(stack);
// maintenance window is set
template.hasResourceProperties('AWS::RDS::DBInstance', Match.objectLike({
PreferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
}));
});
test('preferredMaintenanceWindow provided in writer', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
const PREFERRED_MAINTENANCE_WINDOW: string = 'Sun:12:00-Sun:13:00';
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc: vpc,
writer: ClusterInstance.provisioned('Instance1', {
preferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
}),
});
// THEN
const template = Template.fromStack(stack);
// maintenance window is set
template.hasResourceProperties('AWS::RDS::DBInstance', Match.objectLike({
PreferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
}));
});
test('preferredMaintenanceWindow provided in readers', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
const PREFERRED_MAINTENANCE_WINDOW: string = 'Sun:12:00-Sun:13:00';
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc: vpc,
writer: ClusterInstance.provisioned('Instance1', {
// No preferredMaintenanceWindow set
}),
readers: [
ClusterInstance.provisioned('Instance2', {
preferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
}),
],
});
// THEN
const template = Template.fromStack(stack);
// maintenance window is set
template.hasResourceProperties('AWS::RDS::DBInstance', Match.objectLike({
PreferredMaintenanceWindow: PREFERRED_MAINTENANCE_WINDOW,
}));
});
});
describe('migrate from instanceProps', () => {
test('template contains no changes (provisioned instances)', () => {
// GIVEN
const stack1 = testStack();
const stack2 = testStack();
function createCase(stack: Stack) {
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
const pg = new ParameterGroup(stack, 'pg', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
});
const sg = new ec2.SecurityGroup(stack, 'sg', {
vpc,
});
const instanceProps = {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc,
allowMajorVersionUpgrade: true,
autoMinorVersionUpgrade: true,
deleteAutomatedBackups: true,
enablePerformanceInsights: true,
parameterGroup: pg,
securityGroups: [sg],
};
return instanceProps;
}
const test1 = createCase(stack1);
const test2 = createCase(stack2);
new DatabaseCluster(stack1, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
instanceProps: test1,
iamAuthentication: true,
});
new DatabaseCluster(stack2, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc: test2.vpc,
securityGroups: test2.securityGroups,
writer: ClusterInstance.provisioned('Instance1', {
...test2,
isFromLegacyInstanceProps: true,
}),
readers: [
ClusterInstance.provisioned('Instance2', {
...test2,
isFromLegacyInstanceProps: true,
}),
],
iamAuthentication: true,
});
// THEN
const test1Template = Template.fromStack(stack1).toJSON();
// deleteAutomatedBackups is not needed on the instance, it is set on the cluster
delete test1Template.Resources.DatabaseInstance1844F58FD.Properties.DeleteAutomatedBackups;
delete test1Template.Resources.DatabaseInstance2AA380DEE.Properties.DeleteAutomatedBackups;
expect(
test1Template,
).toEqual(Template.fromStack(stack2).toJSON());
});
test('template contains no changes (serverless instances)', () => {
// GIVEN
const stack1 = testStack();
const stack2 = testStack();
function createCase(stack: Stack) {
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
const pg = new ParameterGroup(stack, 'pg', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
});
const sg = new ec2.SecurityGroup(stack, 'sg', {
vpc,
});
const instanceProps = {
instanceType: new ec2.InstanceType('serverless'),
vpc,
allowMajorVersionUpgrade: true,
autoMinorVersionUpgrade: true,
deleteAutomatedBackups: true,
enablePerformanceInsights: true,
parameterGroup: pg,
securityGroups: [sg],
};
return instanceProps;
}
const test1 = createCase(stack1);
const test2 = createCase(stack2);
// Create serverless cluster using workaround described here:
// https://github.com/aws/aws-cdk/issues/20197#issuecomment-1284485844
const workaroundCluster = new DatabaseCluster(stack1, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
instanceProps: test1,
iamAuthentication: true,
});
cdk.Aspects.of(workaroundCluster).add({
visit(node) {
if (node instanceof CfnDBCluster) {
node.serverlessV2ScalingConfiguration = {
minCapacity: 1,
maxCapacity: 12,
};
}
},
});
// Create serverless cluster using new/official approach.
// This should provide a non-breaking migration path from the workaround.
new DatabaseCluster(stack2, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc: test2.vpc,
securityGroups: test2.securityGroups,
writer: ClusterInstance.serverlessV2('Instance1', {
...test2,
isFromLegacyInstanceProps: true,
}),
readers: [
ClusterInstance.serverlessV2('Instance2', {
...test2,
scaleWithWriter: true,
isFromLegacyInstanceProps: true,
}),
],
iamAuthentication: true,
serverlessV2MinCapacity: 1,
serverlessV2MaxCapacity: 12,
});
// THEN
const test1Template = Template.fromStack(stack1).toJSON();
// deleteAutomatedBackups is not needed on the instance, it is set on the cluster
delete test1Template.Resources.DatabaseInstance1844F58FD.Properties.DeleteAutomatedBackups;
delete test1Template.Resources.DatabaseInstance2AA380DEE.Properties.DeleteAutomatedBackups;
expect(test1Template).toEqual(Template.fromStack(stack2).toJSON());
});
});
describe('creates a writer instance', () => {
test('serverlessV2 writer', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.serverlessV2('writer'),
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
// only the writer gets created
template.resourceCountIs('AWS::RDS::DBInstance', 1);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.serverless',
Engine: 'aurora-mysql',
PromotionTier: 0,
});
});
test('serverlessV2 writer with config', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
removalPolicy: RemovalPolicy.RETAIN,
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.serverlessV2('writer', {
allowMajorVersionUpgrade: true,
autoMinorVersionUpgrade: true,
enablePerformanceInsights: true,
parameterGroup: new ParameterGroup(stack, 'pg', { engine: DatabaseClusterEngine.AURORA_MYSQL }),
}),
});
// THEN
const template = Template.fromStack(stack);
// only the writer gets created
template.resourceCountIs('AWS::RDS::DBInstance', 1);
template.hasResource('AWS::RDS::DBInstance', {
Properties: {
AllowMajorVersionUpgrade: true,
AutoMinorVersionUpgrade: true,
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.serverless',
DBParameterGroupName: { Ref: 'pg749EE6ED' },
EnablePerformanceInsights: true,
Engine: 'aurora-mysql',
PerformanceInsightsRetentionPeriod: 7,
PromotionTier: 0,
},
UpdateReplacePolicy: 'Retain',
Type: 'AWS::RDS::DBInstance',
});
});
test('provisioned writer', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer'),
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
// only the writer gets created
template.resourceCountIs('AWS::RDS::DBInstance', 1);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.t3.medium',
PromotionTier: 0,
});
});
test('provisioned writer with config', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer', {
allowMajorVersionUpgrade: true,
autoMinorVersionUpgrade: true,
enablePerformanceInsights: true,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE ),
parameterGroup: new ParameterGroup(stack, 'pg', { engine: DatabaseClusterEngine.AURORA_MYSQL }),
}),
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
// only the writer gets created
template.resourceCountIs('AWS::RDS::DBInstance', 1);
template.hasResourceProperties('AWS::RDS::DBInstance', {
AllowMajorVersionUpgrade: true,
AutoMinorVersionUpgrade: true,
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.c4.large',
DBParameterGroupName: { Ref: 'pg749EE6ED' },
EnablePerformanceInsights: true,
Engine: 'aurora-mysql',
PerformanceInsightsRetentionPeriod: 7,
PromotionTier: 0,
});
});
test('readers always to be created after the writer', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
vpcSubnets: vpc.selectSubnets({ subnetType: ec2.SubnetType.PUBLIC }),
writer: ClusterInstance.serverlessV2('writer'),
readers: [
ClusterInstance.serverlessV2('reader1', { instanceIdentifier: 'reader1' }),
ClusterInstance.serverlessV2('reader2', { instanceIdentifier: 'reader2' }),
],
});
// THEN
const template = Template.fromStack(stack);
// reader1 should depend on the writer
template.hasResource('AWS::RDS::DBInstance', {
Properties: {
DBInstanceIdentifier: 'reader1',
},
DependsOn: Match.arrayWith(['Databasewriter2462CC03']),
});
// reader2 should depend on the writer
template.hasResource('AWS::RDS::DBInstance', {
Properties: {
DBInstanceIdentifier: 'reader2',
},
DependsOn: Match.arrayWith(['Databasewriter2462CC03']),
});
});
});
describe('instanceIdentifiers', () => {
test('should contain writer and reader instance IDs', () => {
//GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
//WHEN
const cluster = new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.serverlessV2('writer'),
readers: [ClusterInstance.serverlessV2('reader')],
iamAuthentication: true,
});
//THEN
expect(cluster.instanceIdentifiers).toHaveLength(2);
expect(stack.resolve(cluster.instanceIdentifiers[0])).toEqual({
Ref: 'Databasewriter2462CC03',
});
});
});
describe('instanceEndpoints', () => {
test('should contain writer and reader instance endpoints at DatabaseCluster', () => {
//GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
//WHEN
const cluster = new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.serverlessV2('writer'),
readers: [ClusterInstance.serverlessV2('reader')],
iamAuthentication: true,
});
//THEN
expect(cluster.instanceEndpoints).toHaveLength(2);
expect(stack.resolve(cluster.instanceEndpoints)).toEqual([{
hostname: {
'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'],
},
port: {
'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'],
},
socketAddress: {
'Fn::Join': ['', [
{ 'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'] },
':',
{ 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] },
]],
},
}, {
hostname: {
'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'],
},
port: {
'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'],
},
socketAddress: {
'Fn::Join': ['', [
{ 'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'] },
':',
{ 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] },
]],
},
}]);
});
test('should contain writer and reader instance endpoints at DatabaseClusterFromSnapshot', () => {
//GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
//WHEN
const cluster = new DatabaseClusterFromSnapshot(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
snapshotIdentifier: 'snapshot-identifier',
iamAuthentication: true,
writer: ClusterInstance.serverlessV2('writer'),
readers: [ClusterInstance.serverlessV2('reader')],
});
//THEN
expect(cluster.instanceEndpoints).toHaveLength(2);
expect(stack.resolve(cluster.instanceEndpoints)).toEqual([{
hostname: {
'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'],
},
port: {
'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'],
},
socketAddress: {
'Fn::Join': ['', [
{ 'Fn::GetAtt': ['Databasewriter2462CC03', 'Endpoint.Address'] },
':',
{ 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] },
]],
},
}, {
hostname: {
'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'],
},
port: {
'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'],
},
socketAddress: {
'Fn::Join': ['', [
{ 'Fn::GetAtt': ['Databasereader13B43287', 'Endpoint.Address'] },
':',
{ 'Fn::GetAtt': ['DatabaseB269D8BB', 'Endpoint.Port'] },
]],
},
}]);
});
});
describe('provisioned writer with serverless readers', () => {
test('serverless reader in promotion tier 2 throws warning', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
const cluster = new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer'),
readers: [ClusterInstance.serverlessV2('reader')],
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::RDS::DBInstance', 2);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.t3.medium',
PromotionTier: 0,
});
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.serverless',
PromotionTier: 2,
});
Annotations.fromStack(stack).hasWarning('*',
`Cluster ${cluster.node.id} only has serverless readers and no reader is in promotion tier 0-1.`+
'Serverless readers in promotion tiers >= 2 will NOT scale with the writer, which can lead to '+
'availability issues if a failover event occurs. It is recommended that at least one reader '+
'has `scaleWithWriter` set to true [ack: @aws-cdk/aws-rds:noFailoverServerlessReaders]',
);
});
test('serverless reader in promotion tier 2 does not throws', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
const cluster = new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer'),
readers: [ClusterInstance.serverlessV2('reader')],
iamAuthentication: true,
});
CoreAnnotations.of(stack).acknowledgeWarning('RDSNoFailoverServerlessReaders');
// THEN
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::RDS::DBInstance', 2);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.t3.medium',
PromotionTier: 0,
});
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.serverless',
PromotionTier: 2,
});
Annotations.fromStack(stack).hasNoWarning('*',
`Cluster ${cluster.node.id} only has serverless readers and no reader is in promotion tier 0-1.`+
'Serverless readers in promotion tiers >= 2 will NOT scale with the writer, which can lead to '+
'availability issues if a failover event occurs. It is recommended that at least one reader '+
'has `scaleWithWriter` set to true',
);
});
test('serverless reader in promotion tier 2 does not throws with root context', () => {
// GIVEN
const app = new cdk.App({
context: {
ACKNOWLEDGEMENTS_CONTEXT_KEY: {
RDSNoFailoverServerlessReaders: ['Default/Database'],
},
},
});
const stack = testStack(app);
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
const cluster = new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer'),
readers: [ClusterInstance.serverlessV2('reader')],
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::RDS::DBInstance', 2);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.t3.medium',
PromotionTier: 0,
});
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.serverless',
PromotionTier: 2,
});
Annotations.fromStack(stack).hasNoWarning('*',
`Cluster ${cluster.node.id} only has serverless readers and no reader is in promotion tier 0-1.`+
'Serverless readers in promotion tiers >= 2 will NOT scale with the writer, which can lead to '+
'availability issues if a failover event occurs. It is recommended that at least one reader '+
'has `scaleWithWriter` set to true',
);
});
test('serverless reader in promotion tier 1', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer'),
readers: [ClusterInstance.serverlessV2('reader', { scaleWithWriter: true })],
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::RDS::DBInstance', 2);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.t3.medium',
PromotionTier: 0,
});
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.serverless',
PromotionTier: 1,
});
Annotations.fromStack(stack).hasNoWarning('*', '*');
});
test.each([
[
ec2.InstanceType.of(ec2.InstanceClass.M5, ec2.InstanceSize.XLARGE24 ),
undefined,
],
[
ec2.InstanceType.of(ec2.InstanceClass.M5, ec2.InstanceSize.XLARGE ),
4,
],
])('serverless reader cannot scale with writer, throw warning', (instanceType: ec2.InstanceType, maxCapacity?: number) => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer', {
instanceType,
}),
serverlessV2MaxCapacity: maxCapacity,
readers: [ClusterInstance.serverlessV2('reader', { scaleWithWriter: true })],
iamAuthentication: true,
});
// THEN
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::RDS::DBInstance', 2);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: `db.${instanceType.toString()}`,
PromotionTier: 0,
});
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.serverless',
PromotionTier: 1,
});
Annotations.fromStack(stack).hasWarning('*',
'For high availability any serverless instances in promotion tiers 0-1 '+
'should be able to scale to match the provisioned instance capacity.\n'+
'Serverless instance reader is in promotion tier 1,\n'+
`But can not scale to match the provisioned writer instance (${instanceType.toString()}) [ack: @aws-cdk/aws-rds:serverlessInstanceCantScaleWithWriter]`,
);
});
});
describe('provisioned writer and readers', () => {
test('single reader', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');
// WHEN
new DatabaseCluster(stack, 'Database', {
engine: DatabaseClusterEngine.AURORA_MYSQL,
vpc,
writer: ClusterInstance.provisioned('writer', {
// instanceType: ec2.InstanceType.of(ec2.InstanceClass.M5, ec2.InstanceSize.XLARGE24 ),
}),
readers: [ClusterInstance.provisioned('reader')],
});
// THEN
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::RDS::DBInstance', 2);
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.t3.medium',
PromotionTier: 0,
});
template.hasResourceProperties('AWS::RDS::DBInstance', {
DBClusterIdentifier: { Ref: 'DatabaseB269D8BB' },
DBInstanceClass: 'db.t3.medium',
PromotionTier: 2,
});
Annotations.fromStack(stack).hasNoWarning('*', '*');
});
test('throws warning if instance types do not match', () => {
// GIVEN
const stack = testStack();
const vpc = new ec2.Vpc(stack, 'VPC');