-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcluster.ts
571 lines (501 loc) · 15.7 KB
/
cluster.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
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as s3 from '@aws-cdk/aws-s3';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { Duration, IResource, RemovalPolicy, Resource, SecretValue, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { DatabaseSecret } from './database-secret';
import { Endpoint } from './endpoint';
import { IClusterParameterGroup } from './parameter-group';
import { CfnCluster } from './redshift.generated';
import { ClusterSubnetGroup, IClusterSubnetGroup } from './subnet-group';
/**
* Possible Node Types to use in the cluster
* used for defining {@link ClusterProps.nodeType}.
*/
export enum NodeType {
/**
* ds2.xlarge
*/
DS2_XLARGE = 'ds2.xlarge',
/**
* ds2.8xlarge
*/
DS2_8XLARGE = 'ds2.8xlarge',
/**
* dc1.large
*/
DC1_LARGE = 'dc1.large',
/**
* dc1.8xlarge
*/
DC1_8XLARGE = 'dc1.8xlarge',
/**
* dc2.large
*/
DC2_LARGE = 'dc2.large',
/**
* dc2.8xlarge
*/
DC2_8XLARGE = 'dc2.8xlarge',
/**
* ra3.xlplus
*/
RA3_XLPLUS = 'ra3.xlplus',
/**
* ra3.4xlarge
*/
RA3_4XLARGE = 'ra3.4xlarge',
/**
* ra3.16xlarge
*/
RA3_16XLARGE = 'ra3.16xlarge',
}
/**
* What cluster type to use.
* Used by {@link ClusterProps.clusterType}
*/
export enum ClusterType {
/**
* single-node cluster, the {@link ClusterProps.numberOfNodes} parameter is not required
*/
SINGLE_NODE = 'single-node',
/**
* multi-node cluster, set the amount of nodes using {@link ClusterProps.numberOfNodes} parameter
*/
MULTI_NODE = 'multi-node',
}
/**
* Username and password combination
*/
export interface Login {
/**
* Username
*/
readonly masterUsername: string;
/**
* Password
*
* Do not put passwords in your CDK code directly.
*
* @default a Secrets Manager generated password
*/
readonly masterPassword?: SecretValue;
/**
* KMS encryption key to encrypt the generated secret.
*
* @default default master key
*/
readonly encryptionKey?: kms.IKey;
}
/**
* Options to add the multi user rotation
*/
export interface RotationMultiUserOptions {
/**
* The secret to rotate. It must be a JSON string with the following format:
* ```
* {
* "engine": <required: database engine>,
* "host": <required: instance host name>,
* "username": <required: username>,
* "password": <required: password>,
* "dbname": <optional: database name>,
* "port": <optional: if not specified, default port will be used>,
* "masterarn": <required: the arn of the master secret which will be used to create users/change passwords>
* }
* ```
*/
readonly secret: secretsmanager.ISecret;
/**
* Specifies the number of days after the previous rotation before
* Secrets Manager triggers the next automatic rotation.
*
* @default Duration.days(30)
*/
readonly automaticallyAfter?: Duration;
}
/**
* Create a Redshift Cluster with a given number of nodes.
* Implemented by {@link Cluster} via {@link ClusterBase}.
*/
export interface ICluster extends IResource, ec2.IConnectable, secretsmanager.ISecretAttachmentTarget {
/**
* Name of the cluster
*
* @attribute ClusterName
*/
readonly clusterName: string;
/**
* The endpoint to use for read/write operations
*
* @attribute EndpointAddress,EndpointPort
*/
readonly clusterEndpoint: Endpoint;
}
/**
* Properties that describe an existing cluster instance
*/
export interface ClusterAttributes {
/**
* The security groups of the redshift cluster
*
* @default no security groups will be attached to the import
*/
readonly securityGroups?: ec2.ISecurityGroup[];
/**
* Identifier for the cluster
*/
readonly clusterName: string;
/**
* Cluster endpoint address
*/
readonly clusterEndpointAddress: string;
/**
* Cluster endpoint port
*/
readonly clusterEndpointPort: number;
}
/**
* Properties for a new database cluster
*/
export interface ClusterProps {
/**
* An optional identifier for the cluster
*
* @default - A name is automatically generated.
*/
readonly clusterName?: string;
/**
* Additional parameters to pass to the database engine
* https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-parameter-groups.html
*
* @default - No parameter group.
*/
readonly parameterGroup?: IClusterParameterGroup;
/**
* Number of compute nodes in the cluster. Only specify this property for multi-node clusters.
*
* Value must be at least 2 and no more than 100.
*
* @default - 2 if `clusterType` is ClusterType.MULTI_NODE, undefined otherwise
*/
readonly numberOfNodes?: number;
/**
* The node type to be provisioned for the cluster.
*
* @default {@link NodeType.DC2_LARGE}
*/
readonly nodeType?: NodeType;
/**
* Settings for the individual instances that are launched
*
* @default {@link ClusterType.MULTI_NODE}
*/
readonly clusterType?: ClusterType;
/**
* What port to listen on
*
* @default - The default for the engine is used.
*/
readonly port?: number;
/**
* Whether to enable encryption of data at rest in the cluster.
*
* @default true
*/
readonly encrypted?: boolean
/**
* The KMS key to use for encryption of data at rest.
*
* @default - AWS-managed key, if encryption at rest is enabled
*/
readonly encryptionKey?: kms.IKey;
/**
* A preferred maintenance window day/time range. Should be specified as a range ddd:hh24:mi-ddd:hh24:mi (24H Clock UTC).
*
* Example: 'Sun:23:45-Mon:00:15'
*
* @default - 30-minute window selected at random from an 8-hour block of time for
* each AWS Region, occurring on a random day of the week.
* @see https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_UpgradeDBInstance.Maintenance.html#Concepts.DBMaintenance
*/
readonly preferredMaintenanceWindow?: string;
/**
* The VPC to place the cluster in.
*/
readonly vpc: ec2.IVpc;
/**
* Where to place the instances within the VPC
*
* @default - private subnets
*/
readonly vpcSubnets?: ec2.SubnetSelection;
/**
* Security group.
*
* @default - a new security group is created.
*/
readonly securityGroups?: ec2.ISecurityGroup[];
/**
* A cluster subnet group to use with this cluster.
*
* @default - a new subnet group will be created.
*/
readonly subnetGroup?: IClusterSubnetGroup;
/**
* Username and password for the administrative user
*/
readonly masterUser: Login;
/**
* A list of AWS Identity and Access Management (IAM) role that can be used by the cluster to access other AWS services.
* Specify a maximum of 10 roles.
*
* @default - No role is attached to the cluster.
*/
readonly roles?: iam.IRole[];
/**
* Name of a database which is automatically created inside the cluster
*
* @default - default_db
*/
readonly defaultDatabaseName?: string;
/**
* Bucket to send logs to.
* Logging information includes queries and connection attempts, for the specified Amazon Redshift cluster.
*
* @default - No Logs
*/
readonly loggingBucket?: s3.IBucket
/**
* Prefix used for logging
*
* @default - no prefix
*/
readonly loggingKeyPrefix?: string
/**
* The removal policy to apply when the cluster and its instances are removed
* from the stack or replaced during an update.
*
* @default RemovalPolicy.RETAIN
*/
readonly removalPolicy?: RemovalPolicy
/**
* Whether to make cluster publicly accessible.
*
* @default false
*/
readonly publiclyAccessible?: boolean
}
/**
* A new or imported clustered database.
*/
abstract class ClusterBase extends Resource implements ICluster {
/**
* Name of the cluster
*/
public abstract readonly clusterName: string;
/**
* The endpoint to use for read/write operations
*/
public abstract readonly clusterEndpoint: Endpoint;
/**
* Access to the network connections
*/
public abstract readonly connections: ec2.Connections;
/**
* Renders the secret attachment target specifications.
*/
public asSecretAttachmentTarget(): secretsmanager.SecretAttachmentTargetProps {
return {
targetId: this.clusterName,
targetType: secretsmanager.AttachmentTargetType.REDSHIFT_CLUSTER,
};
}
}
/**
* Create a Redshift cluster a given number of nodes.
*
* @resource AWS::Redshift::Cluster
*/
export class Cluster extends ClusterBase {
/**
* Import an existing DatabaseCluster from properties
*/
public static fromClusterAttributes(scope: Construct, id: string, attrs: ClusterAttributes): ICluster {
class Import extends ClusterBase {
public readonly connections = new ec2.Connections({
securityGroups: attrs.securityGroups,
defaultPort: ec2.Port.tcp(attrs.clusterEndpointPort),
});
public readonly clusterName = attrs.clusterName;
public readonly instanceIdentifiers: string[] = [];
public readonly clusterEndpoint = new Endpoint(attrs.clusterEndpointAddress, attrs.clusterEndpointPort);
}
return new Import(scope, id);
}
/**
* Identifier of the cluster
*/
public readonly clusterName: string;
/**
* The endpoint to use for read/write operations
*/
public readonly clusterEndpoint: Endpoint;
/**
* Access to the network connections
*/
public readonly connections: ec2.Connections;
/**
* The secret attached to this cluster
*/
public readonly secret?: secretsmanager.ISecret;
private readonly singleUserRotationApplication: secretsmanager.SecretRotationApplication;
private readonly multiUserRotationApplication: secretsmanager.SecretRotationApplication;
/**
* The VPC where the DB subnet group is created.
*/
private readonly vpc: ec2.IVpc;
/**
* The subnets used by the DB subnet group.
*/
private readonly vpcSubnets?: ec2.SubnetSelection;
constructor(scope: Construct, id: string, props: ClusterProps) {
super(scope, id);
this.vpc = props.vpc;
this.vpcSubnets = props.vpcSubnets ?? {
subnetType: ec2.SubnetType.PRIVATE,
};
const removalPolicy = props.removalPolicy ?? RemovalPolicy.RETAIN;
const subnetGroup = props.subnetGroup ?? new ClusterSubnetGroup(this, 'Subnets', {
description: `Subnets for ${id} Redshift cluster`,
vpc: this.vpc,
vpcSubnets: this.vpcSubnets,
removalPolicy: removalPolicy,
});
const securityGroups = props.securityGroups ?? [new ec2.SecurityGroup(this, 'SecurityGroup', {
description: 'Redshift security group',
vpc: this.vpc,
})];
const securityGroupIds = securityGroups.map(sg => sg.securityGroupId);
let secret: DatabaseSecret | undefined;
if (!props.masterUser.masterPassword) {
secret = new DatabaseSecret(this, 'Secret', {
username: props.masterUser.masterUsername,
encryptionKey: props.masterUser.encryptionKey,
});
}
const clusterType = props.clusterType || ClusterType.MULTI_NODE;
const nodeCount = this.validateNodeCount(clusterType, props.numberOfNodes);
if (props.encrypted === false && props.encryptionKey !== undefined) {
throw new Error('Cannot set property encryptionKey without enabling encryption!');
}
this.singleUserRotationApplication = secretsmanager.SecretRotationApplication.REDSHIFT_ROTATION_SINGLE_USER;
this.multiUserRotationApplication = secretsmanager.SecretRotationApplication.REDSHIFT_ROTATION_MULTI_USER;
let loggingProperties;
if (props.loggingBucket) {
loggingProperties = {
bucketName: props.loggingBucket.bucketName,
s3KeyPrefix: props.loggingKeyPrefix,
};
}
const cluster = new CfnCluster(this, 'Resource', {
// Basic
allowVersionUpgrade: true,
automatedSnapshotRetentionPeriod: 1,
clusterType,
clusterIdentifier: props.clusterName,
clusterSubnetGroupName: subnetGroup.clusterSubnetGroupName,
vpcSecurityGroupIds: securityGroupIds,
port: props.port,
clusterParameterGroupName: props.parameterGroup && props.parameterGroup.clusterParameterGroupName,
// Admin (unsafeUnwrap here is safe)
masterUsername: secret?.secretValueFromJson('username').unsafeUnwrap() ?? props.masterUser.masterUsername,
masterUserPassword: secret?.secretValueFromJson('password').unsafeUnwrap()
?? props.masterUser.masterPassword?.unsafeUnwrap()
?? 'default',
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
nodeType: props.nodeType || NodeType.DC2_LARGE,
numberOfNodes: nodeCount,
loggingProperties,
iamRoles: props?.roles?.map(role => role.roleArn),
dbName: props.defaultDatabaseName || 'default_db',
publiclyAccessible: props.publiclyAccessible || false,
// Encryption
kmsKeyId: props.encryptionKey?.keyId,
encrypted: props.encrypted ?? true,
});
cluster.applyRemovalPolicy(removalPolicy, {
applyToUpdateReplacePolicy: true,
});
this.clusterName = cluster.ref;
// create a number token that represents the port of the cluster
const portAttribute = Token.asNumber(cluster.attrEndpointPort);
this.clusterEndpoint = new Endpoint(cluster.attrEndpointAddress, portAttribute);
if (secret) {
this.secret = secret.attach(this);
}
const defaultPort = ec2.Port.tcp(this.clusterEndpoint.port);
this.connections = new ec2.Connections({ securityGroups, defaultPort });
}
/**
* Adds the single user rotation of the master password to this cluster.
*
* @param [automaticallyAfter=Duration.days(30)] Specifies the number of days after the previous rotation
* before Secrets Manager triggers the next automatic rotation.
*/
public addRotationSingleUser(automaticallyAfter?: Duration): secretsmanager.SecretRotation {
if (!this.secret) {
throw new Error('Cannot add single user rotation for a cluster without secret.');
}
const id = 'RotationSingleUser';
const existing = this.node.tryFindChild(id);
if (existing) {
throw new Error('A single user rotation was already added to this cluster.');
}
return new secretsmanager.SecretRotation(this, id, {
secret: this.secret,
automaticallyAfter,
application: this.singleUserRotationApplication,
vpc: this.vpc,
vpcSubnets: this.vpcSubnets,
target: this,
});
}
/**
* Adds the multi user rotation to this cluster.
*/
public addRotationMultiUser(id: string, options: RotationMultiUserOptions): secretsmanager.SecretRotation {
if (!this.secret) {
throw new Error('Cannot add multi user rotation for a cluster without secret.');
}
return new secretsmanager.SecretRotation(this, id, {
secret: options.secret,
masterSecret: this.secret,
automaticallyAfter: options.automaticallyAfter,
application: this.multiUserRotationApplication,
vpc: this.vpc,
vpcSubnets: this.vpcSubnets,
target: this,
});
}
private validateNodeCount(clusterType: ClusterType, numberOfNodes?: number): number | undefined {
if (clusterType === ClusterType.SINGLE_NODE) {
// This property must not be set for single-node clusters; be generous and treat a value of 1 node as undefined.
if (numberOfNodes !== undefined && numberOfNodes !== 1) {
throw new Error('Number of nodes must be not be supplied or be 1 for cluster type single-node');
}
return undefined;
} else {
if (Token.isUnresolved(numberOfNodes)) {
return numberOfNodes;
}
const nodeCount = numberOfNodes ?? 2;
if (nodeCount < 2 || nodeCount > 100) {
throw new Error('Number of nodes for cluster type multi-node must be at least 2 and no more than 100');
}
return nodeCount;
}
}
}