-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathaurora-cluster-instance.ts
545 lines (498 loc) · 18 KB
/
aurora-cluster-instance.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
import { Construct } from 'constructs';
import { CaCertificate } from './ca-certificate';
import { DatabaseCluster } from './cluster';
import { IDatabaseCluster } from './cluster-ref';
import { IParameterGroup, ParameterGroup } from './parameter-group';
import { helperRemovalPolicy } from './private/util';
import { PerformanceInsightRetention } from './props';
import { CfnDBInstance } from './rds.generated';
import { ISubnetGroup } from './subnet-group';
import * as ec2 from '../../aws-ec2';
import { IRole } from '../../aws-iam';
import * as kms from '../../aws-kms';
import { IResource, Resource, Duration, RemovalPolicy, ArnFormat, FeatureFlags } from '../../core';
import { AURORA_CLUSTER_CHANGE_SCOPE_OF_INSTANCE_PARAMETER_GROUP_WITH_EACH_PARAMETERS } from '../../cx-api';
/**
* Options for binding the instance to the cluster
*/
export interface ClusterInstanceBindOptions {
/**
* The interval, in seconds, between points when Amazon RDS collects enhanced
* monitoring metrics for the DB instances.
*
* @default no enhanced monitoring
*/
readonly monitoringInterval?: Duration;
/**
* Role that will be used to manage DB instances monitoring.
*
* @default - A role is automatically created for you
*/
readonly monitoringRole?: IRole;
/**
* The removal policy on the cluster
*
* @default - RemovalPolicy.DESTROY (cluster snapshot can restore)
*/
readonly removalPolicy?: RemovalPolicy;
/**
* The promotion tier of the cluster instance
*
* This matters more for serverlessV2 instances. If a serverless
* instance is in tier 0-1 then it will scale with the writer.
*
* For provisioned instances this just determines the failover priority.
* If multiple instances have the same priority then one will be picked at random
*
* @default 2
*/
readonly promotionTier?: number;
/**
* Existing subnet group for the cluster.
* This is only needed when using the isFromLegacyInstanceProps
*
* @default - cluster subnet group is used
*/
readonly subnetGroup?: ISubnetGroup;
}
/**
* The type of Aurora Cluster Instance. Can be either serverless v2
* or provisioned
*/
export class ClusterInstanceType {
/**
* Aurora Serverless V2 instance type
* @see https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2.html
*/
public static serverlessV2(): ClusterInstanceType {
return new ClusterInstanceType('db.serverless', InstanceType.SERVERLESS_V2);
}
/**
* Aurora Provisioned instance type
*/
public static provisioned(instanceType?: ec2.InstanceType): ClusterInstanceType {
return new ClusterInstanceType(
(instanceType ?? ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MEDIUM)).toString(),
InstanceType.PROVISIONED,
);
}
constructor(
private readonly instanceType: string,
public readonly type: InstanceType,
) { }
/**
* String representation of the instance type that can be used in the CloudFormation resource
*/
public toString(): string {
return this.instanceType;
}
}
/**
* Represents an Aurora cluster instance
* This can be either a provisioned instance or a serverless v2 instance
*/
export interface IClusterInstance {
/**
* Create the database instance within the provided cluster
*/
bind(scope: Construct, cluster: IDatabaseCluster, options: ClusterInstanceBindOptions): IAuroraClusterInstance;
}
/**
* Options for creating a provisioned instance
*/
export interface ProvisionedClusterInstanceProps extends ClusterInstanceOptions {
/**
* The cluster instance type
*
* @default db.t3.medium
*/
readonly instanceType?: ec2.InstanceType;
/**
* The promotion tier of the cluster instance
*
* Can be between 0-15
*
* For provisioned instances this just determines the failover priority.
* If multiple instances have the same priority then one will be picked at random
*
* @default 2
*/
readonly promotionTier?: number;
}
/**
* Options for creating a serverless v2 instance
*/
export interface ServerlessV2ClusterInstanceProps extends ClusterInstanceOptions {
/**
* Only applicable to reader instances.
*
* If this is true then the instance will be placed in promotion tier 1, otherwise
* it will be placed in promotion tier 2.
*
* For serverless v2 instances this means:
* - true: The serverless v2 reader will scale to match the writer instance (provisioned or serverless)
* - false: The serverless v2 reader will scale with the read workfload on the instance
*
* @default false
*/
readonly scaleWithWriter?: boolean;
}
/**
* Common options for creating cluster instances (both serverless and provisioned)
*/
export interface ClusterInstanceProps extends ClusterInstanceOptions {
/**
* The type of cluster instance to create. Can be either
* provisioned or serverless v2
*/
readonly instanceType: ClusterInstanceType;
/**
* The promotion tier of the cluster instance
*
* This matters more for serverlessV2 instances. If a serverless
* instance is in tier 0-1 then it will scale with the writer.
*
* For provisioned instances this just determines the failover priority.
* If multiple instances have the same priority then one will be picked at random
*
* @default 2
*/
readonly promotionTier?: number;
}
/**
* Common options for creating a cluster instance
*/
export interface ClusterInstanceOptions {
/**
* The identifier for the database instance
*
* @default - CloudFormation generated identifier
*/
readonly instanceIdentifier?: string;
/**
* Whether to enable automatic upgrade of minor version for the DB instance.
*
* @default - true
*/
readonly autoMinorVersionUpgrade?: boolean;
/**
* Whether to enable Performance Insights for the DB instance.
*
* @default - false, unless ``performanceInsightRentention`` or ``performanceInsightEncryptionKey`` is set.
*/
readonly enablePerformanceInsights?: boolean;
/**
* The amount of time, in days, to retain Performance Insights data.
*
* @default 7
*/
readonly performanceInsightRetention?: PerformanceInsightRetention;
/**
* The AWS KMS key for encryption of Performance Insights data.
*
* @default - default master key
*/
readonly performanceInsightEncryptionKey?: kms.IKey;
/**
* Indicates whether the DB instance is an internet-facing instance.
*
* @default - true if the instance is placed in a public subnet
*/
readonly publiclyAccessible?: boolean;
/**
* The parameters in the DBParameterGroup to create automatically
*
* You can only specify parameterGroup or parameters but not both.
* You need to use a versioned engine to auto-generate a DBParameterGroup.
*
* @default - None
*/
readonly parameters?: { [key: string]: string };
/**
* Whether to allow upgrade of major version for the DB instance.
*
* @default - false
*/
readonly allowMajorVersionUpgrade?: boolean;
/**
* The DB parameter group to associate with the instance.
* This is only needed if you need to configure different parameter
* groups for each individual instance, otherwise you should not
* provide this and just use the cluster parameter group
*
* @default the cluster parameter group is used
*/
readonly parameterGroup?: IParameterGroup;
/**
* Only used for migrating existing clusters from using `instanceProps` to `writer` and `readers`
*
* @example
* // existing cluster
* declare const vpc: ec2.Vpc;
* const cluster = new rds.DatabaseCluster(this, 'Database', {
* engine: rds.DatabaseClusterEngine.auroraMysql({
* version: rds.AuroraMysqlEngineVersion.VER_3_03_0,
* }),
* instances: 2,
* instanceProps: {
* instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.SMALL),
* vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
* vpc,
* },
* });
*
* // migration
*
* const instanceProps = {
* instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.SMALL),
* isFromLegacyInstanceProps: true,
* };
*
* const myCluster = new rds.DatabaseCluster(this, 'Database', {
* engine: rds.DatabaseClusterEngine.auroraMysql({
* version: rds.AuroraMysqlEngineVersion.VER_3_03_0,
* }),
* vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
* vpc,
* writer: rds.ClusterInstance.provisioned('Instance1', {
* instanceType: instanceProps.instanceType,
* isFromLegacyInstanceProps: instanceProps.isFromLegacyInstanceProps,
* }),
* readers: [
* rds.ClusterInstance.provisioned('Instance2', {
* instanceType: instanceProps.instanceType,
* isFromLegacyInstanceProps: instanceProps.isFromLegacyInstanceProps,
* }),
* ],
* });
*
* @default false
*/
readonly isFromLegacyInstanceProps?: boolean;
/**
* The identifier of the CA certificate for this DB cluster's instances.
*
* Specifying or updating this property triggers a reboot.
*
* For RDS DB engines:
* @see https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL-certificate-rotation.html
* For Aurora DB engines:
* @see https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.SSL-certificate-rotation.html
*
* @default - RDS will choose a certificate authority
*/
readonly caCertificate?: CaCertificate;
}
/**
* Create an RDS Aurora Cluster Instance. You can create either provisioned or
* serverless v2 instances.
*
* @example
*
* declare const vpc: ec2.Vpc;
* const myCluster = new rds.DatabaseCluster(this, 'Database', {
* engine: rds.DatabaseClusterEngine.auroraMysql({ version: rds.AuroraMysqlEngineVersion.VER_2_08_1 }),
* writer: rds.ClusterInstance.provisioned('writer', {
* instanceType: ec2.InstanceType.of(ec2.InstanceClass.R6G, ec2.InstanceSize.XLARGE4),
* }),
* serverlessV2MinCapacity: 6.5,
* serverlessV2MaxCapacity: 64,
* readers: [
* // will be put in promotion tier 1 and will scale with the writer
* rds.ClusterInstance.serverlessV2('reader1', { scaleWithWriter: true }),
* // will be put in promotion tier 2 and will not scale with the writer
* rds.ClusterInstance.serverlessV2('reader2'),
* ],
* vpc,
* });
*/
export class ClusterInstance implements IClusterInstance {
/**
* Add a provisioned instance to the cluster
*
* @example
* rds.ClusterInstance.provisioned('ClusterInstance', {
* instanceType: ec2.InstanceType.of(ec2.InstanceClass.R6G, ec2.InstanceSize.XLARGE4),
* });
*/
public static provisioned(id: string, props: ProvisionedClusterInstanceProps = {}): IClusterInstance {
return new ClusterInstance(id, {
...props,
instanceType: ClusterInstanceType.provisioned(props.instanceType),
});
}
/**
* Add a serverless v2 instance to the cluster
*
* @example
* rds.ClusterInstance.serverlessV2('ClusterInstance', {
* scaleWithWriter: true,
* });
*/
public static serverlessV2(id: string, props: ServerlessV2ClusterInstanceProps = {}): IClusterInstance {
return new ClusterInstance(id, {
...props,
promotionTier: props.scaleWithWriter ? 1 : 2,
instanceType: ClusterInstanceType.serverlessV2(),
});
}
private constructor(private id: string, private readonly props: ClusterInstanceProps) { }
/**
* Add the ClusterInstance to the cluster
*/
public bind(scope: Construct, cluster: IDatabaseCluster, props: ClusterInstanceBindOptions): IAuroraClusterInstance {
return new AuroraClusterInstance(scope, this.id, {
cluster,
...this.props,
...props,
});
}
}
interface AuroraClusterInstanceProps extends ClusterInstanceProps, ClusterInstanceBindOptions {
readonly cluster: IDatabaseCluster;
}
export enum InstanceType {
PROVISIONED = 'PROVISIONED',
SERVERLESS_V2 = 'SERVERLESS_V2',
}
/**
* An Aurora Cluster Instance
*/
export interface IAuroraClusterInstance extends IResource {
/**
* The instance ARN
*/
readonly dbInstanceArn: string;
/**
* The instance resource ID
*/
readonly dbiResourceId: string;
/**
* The instance endpoint address
*/
readonly dbInstanceEndpointAddress: string;
/**
* The instance identifier
*/
readonly instanceIdentifier: string;
/**
* The instance type (provisioned vs serverless v2)
*/
readonly type: InstanceType;
/**
* The instance size if the instance is a provisioned type
*/
readonly instanceSize?: string;
/**
* Te promotion tier the instance was created in
*/
readonly tier: number;
}
class AuroraClusterInstance extends Resource implements IAuroraClusterInstance {
public readonly dbInstanceArn: string;
public readonly dbiResourceId: string;
public readonly dbInstanceEndpointAddress: string;
public readonly instanceIdentifier: string;
public readonly type: InstanceType;
public readonly tier: number;
public readonly instanceSize?: string;
constructor(scope: Construct, id: string, props: AuroraClusterInstanceProps) {
super(
scope,
props.isFromLegacyInstanceProps ? `${id}Wrapper` : id,
{
physicalName: props.instanceIdentifier,
});
this.tier = props.promotionTier ?? 2;
if (this.tier > 15) {
throw new Error('promotionTier must be between 0-15');
}
const isOwnedResource = Resource.isOwnedResource(props.cluster);
let internetConnected;
let publiclyAccessible = props.publiclyAccessible;
if (isOwnedResource) {
const ownedCluster = props.cluster as DatabaseCluster;
internetConnected = ownedCluster.vpc.selectSubnets(ownedCluster.vpcSubnets).internetConnectivityEstablished;
publiclyAccessible = ownedCluster.vpcSubnets && ownedCluster.vpcSubnets.subnetType === ec2.SubnetType.PUBLIC;
}
// Get the actual subnet objects so we can depend on internet connectivity.
const instanceType = (props.instanceType ?? ClusterInstanceType.serverlessV2());
this.type = instanceType.type;
this.instanceSize = this.type === InstanceType.PROVISIONED ? props.instanceType?.toString() : undefined;
// engine is never undefined on a managed resource, i.e. DatabaseCluster
const engine = props.cluster.engine!;
const enablePerformanceInsights = props.enablePerformanceInsights
|| props.performanceInsightRetention !== undefined || props.performanceInsightEncryptionKey !== undefined;
if (enablePerformanceInsights && props.enablePerformanceInsights === false) {
throw new Error('`enablePerformanceInsights` disabled, but `performanceInsightRetention` or `performanceInsightEncryptionKey` was set');
}
const instanceParameterGroup = props.parameterGroup ?? (
props.parameters
? FeatureFlags.of(this).isEnabled(AURORA_CLUSTER_CHANGE_SCOPE_OF_INSTANCE_PARAMETER_GROUP_WITH_EACH_PARAMETERS)
? new ParameterGroup(this, 'InstanceParameterGroup', {
engine: engine,
parameters: props.parameters,
})
: new ParameterGroup(props.cluster, 'InstanceParameterGroup', {
engine: engine,
parameters: props.parameters,
})
: undefined
);
const instanceParameterGroupConfig = instanceParameterGroup?.bindToInstance({});
const instance = new CfnDBInstance(
props.isFromLegacyInstanceProps ? scope : this,
props.isFromLegacyInstanceProps ? id : 'Resource',
{
// Link to cluster
engine: engine.engineType,
dbClusterIdentifier: props.cluster.clusterIdentifier,
promotionTier: props.isFromLegacyInstanceProps ? undefined : this.tier,
dbInstanceIdentifier: this.physicalName,
// Instance properties
dbInstanceClass: props.instanceType ? databaseInstanceType(instanceType) : undefined,
publiclyAccessible,
enablePerformanceInsights: enablePerformanceInsights || props.enablePerformanceInsights, // fall back to undefined if not set
performanceInsightsKmsKeyId: props.performanceInsightEncryptionKey?.keyArn,
performanceInsightsRetentionPeriod: enablePerformanceInsights
? (props.performanceInsightRetention || PerformanceInsightRetention.DEFAULT)
: undefined,
// only need to supply this when migrating from legacy method.
// this is not applicable for aurora instances, but if you do provide it and then
// change it it will cause an instance replacement
dbSubnetGroupName: props.isFromLegacyInstanceProps ? props.subnetGroup?.subnetGroupName : undefined,
dbParameterGroupName: instanceParameterGroupConfig?.parameterGroupName,
monitoringInterval: props.monitoringInterval && props.monitoringInterval.toSeconds(),
monitoringRoleArn: props.monitoringRole && props.monitoringRole.roleArn,
autoMinorVersionUpgrade: props.autoMinorVersionUpgrade,
allowMajorVersionUpgrade: props.allowMajorVersionUpgrade,
caCertificateIdentifier: props.caCertificate && props.caCertificate.toString(),
});
// For instances that are part of a cluster:
//
// Cluster DESTROY or SNAPSHOT -> DESTROY (snapshot is good enough to recreate)
// Cluster RETAIN -> RETAIN (otherwise cluster state will disappear)
instance.applyRemovalPolicy(helperRemovalPolicy(props.removalPolicy));
// We must have a dependency on the NAT gateway provider here to create
// things in the right order.
if (internetConnected) {
instance.node.addDependency(internetConnected);
}
this.dbInstanceArn = this.getResourceArnAttribute(instance.attrDbInstanceArn, {
resource: 'db',
service: 'rds',
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
resourceName: this.physicalName,
});
this.instanceIdentifier = this.getResourceNameAttribute(instance.ref);
this.dbiResourceId = instance.attrDbiResourceId;
this.dbInstanceEndpointAddress = instance.attrEndpointAddress;
}
}
/**
* Turn a regular instance type into a database instance type
*/
function databaseInstanceType(instanceType: ClusterInstanceType) {
const type = instanceType.toString();
return instanceType.type === InstanceType.SERVERLESS_V2 ? type : 'db.' + type;
}