-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
job.ts
803 lines (709 loc) · 23.8 KB
/
job.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
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import * as logs from '@aws-cdk/aws-logs';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import { Code, JobExecutable, JobExecutableConfig, JobType } from '.';
import { IConnection } from './connection';
import { CfnJob } from './glue.generated';
import { ISecurityConfiguration } from './security-configuration';
/**
* The type of predefined worker that is allocated when a job runs.
*
* If you need to use a WorkerType that doesn't exist as a static member, you
* can instantiate a `WorkerType` object, e.g: `WorkerType.of('other type')`.
*/
export class WorkerType {
/**
* Each worker provides 4 vCPU, 16 GB of memory and a 50GB disk, and 2 executors per worker.
*/
public static readonly STANDARD = new WorkerType('Standard');
/**
* Each worker maps to 1 DPU (4 vCPU, 16 GB of memory, 64 GB disk), and provides 1 executor per worker. Suitable for memory-intensive jobs.
*/
public static readonly G_1X = new WorkerType('G.1X');
/**
* Each worker maps to 2 DPU (8 vCPU, 32 GB of memory, 128 GB disk), and provides 1 executor per worker. Suitable for memory-intensive jobs.
*/
public static readonly G_2X = new WorkerType('G.2X');
/**
* Custom worker type
* @param workerType custom worker type
*/
public static of(workerType: string): WorkerType {
return new WorkerType(workerType);
}
/**
* The name of this WorkerType, as expected by Job resource.
*/
public readonly name: string;
private constructor(name: string) {
this.name = name;
}
}
/**
* Job states emitted by Glue to CloudWatch Events.
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types for more information.
*/
export enum JobState {
/**
* State indicating job run succeeded
*/
SUCCEEDED = 'SUCCEEDED',
/**
* State indicating job run failed
*/
FAILED = 'FAILED',
/**
* State indicating job run timed out
*/
TIMEOUT = 'TIMEOUT',
/**
* State indicating job is starting
*/
STARTING = 'STARTING',
/**
* State indicating job is running
*/
RUNNING = 'RUNNING',
/**
* State indicating job is stopping
*/
STOPPING = 'STOPPING',
/**
* State indicating job stopped
*/
STOPPED = 'STOPPED',
}
/**
* The Glue CloudWatch metric type.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitoring-awsglue-with-cloudwatch-metrics.html
*/
export enum MetricType {
/**
* A value at a point in time.
*/
GAUGE = 'gauge',
/**
* An aggregate number.
*/
COUNT = 'count',
}
/**
* Interface representing a created or an imported {@link Job}.
*/
export interface IJob extends cdk.IResource, iam.IGrantable {
/**
* The name of the job.
* @attribute
*/
readonly jobName: string;
/**
* The ARN of the job.
* @attribute
*/
readonly jobArn: string;
/**
* Defines a CloudWatch event rule triggered when something happens with this job.
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types
*/
onEvent(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Defines a CloudWatch event rule triggered when this job moves to the input jobState.
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types
*/
onStateChange(id: string, jobState: JobState, options?: events.OnEventOptions): events.Rule;
/**
* Defines a CloudWatch event rule triggered when this job moves to the SUCCEEDED state.
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types
*/
onSuccess(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Defines a CloudWatch event rule triggered when this job moves to the FAILED state.
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types
*/
onFailure(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Defines a CloudWatch event rule triggered when this job moves to the TIMEOUT state.
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types
*/
onTimeout(id: string, options?: events.OnEventOptions): events.Rule;
/**
* Create a CloudWatch metric.
*
* @param metricName name of the metric typically prefixed with `glue.driver.`, `glue.<executorId>.` or `glue.ALL.`.
* @param type the metric type.
* @param props metric options.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitoring-awsglue-with-cloudwatch-metrics.html
*/
metric(metricName: string, type: MetricType, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Create a CloudWatch Metric indicating job success.
*/
metricSuccess(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Create a CloudWatch Metric indicating job failure.
*/
metricFailure(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* Create a CloudWatch Metric indicating job timeout.
*/
metricTimeout(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
}
abstract class JobBase extends cdk.Resource implements IJob {
public abstract readonly jobArn: string;
public abstract readonly jobName: string;
public abstract readonly grantPrincipal: iam.IPrincipal;
/**
* Create a CloudWatch Event Rule for this Glue Job when it's in a given state
*
* @param id construct id
* @param options event options. Note that some values are overridden if provided, these are
* - eventPattern.source = ['aws.glue']
* - eventPattern.detailType = ['Glue Job State Change', 'Glue Job Run Status']
* - eventPattern.detail.jobName = [this.jobName]
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types
*/
public onEvent(id: string, options: events.OnEventOptions = {}): events.Rule {
const rule = new events.Rule(this, id, options);
rule.addTarget(options.target);
rule.addEventPattern({
source: ['aws.glue'],
detailType: ['Glue Job State Change', 'Glue Job Run Status'],
detail: {
jobName: [this.jobName],
},
});
return rule;
}
/**
* Create a CloudWatch Event Rule for the transition into the input jobState.
*
* @param id construct id.
* @param jobState the job state.
* @param options optional event options.
*/
public onStateChange(id: string, jobState: JobState, options: events.OnEventOptions = {}): events.Rule {
const rule = this.onEvent(id, {
description: `Rule triggered when Glue job ${this.jobName} is in ${jobState} state`,
...options,
});
rule.addEventPattern({
detail: {
state: [jobState],
},
});
return rule;
}
/**
* Create a CloudWatch Event Rule matching JobState.SUCCEEDED.
*
* @param id construct id.
* @param options optional event options. default is {}.
*/
public onSuccess(id: string, options: events.OnEventOptions = {}): events.Rule {
return this.onStateChange(id, JobState.SUCCEEDED, options);
}
/**
* Return a CloudWatch Event Rule matching FAILED state.
*
* @param id construct id.
* @param options optional event options. default is {}.
*/
public onFailure(id: string, options: events.OnEventOptions = {}): events.Rule {
return this.onStateChange(id, JobState.FAILED, options);
}
/**
* Return a CloudWatch Event Rule matching TIMEOUT state.
*
* @param id construct id.
* @param options optional event options. default is {}.
*/
public onTimeout(id: string, options: events.OnEventOptions = {}): events.Rule {
return this.onStateChange(id, JobState.TIMEOUT, options);
}
/**
* Create a CloudWatch metric.
*
* @param metricName name of the metric typically prefixed with `glue.driver.`, `glue.<executorId>.` or `glue.ALL.`.
* @param type the metric type.
* @param props metric options.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitoring-awsglue-with-cloudwatch-metrics.html
*/
public metric(metricName: string, type: MetricType, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
metricName,
namespace: 'Glue',
dimensionsMap: {
JobName: this.jobName,
JobRunId: 'ALL',
Type: type,
},
...props,
}).attachTo(this);
}
/**
* Return a CloudWatch Metric indicating job success.
*
* This metric is based on the Rule returned by no-args onSuccess() call.
*/
public metricSuccess(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return metricRule(this.metricJobStateRule('SuccessMetricRule', JobState.SUCCEEDED), props);
}
/**
* Return a CloudWatch Metric indicating job failure.
*
* This metric is based on the Rule returned by no-args onFailure() call.
*/
public metricFailure(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return metricRule(this.metricJobStateRule('FailureMetricRule', JobState.FAILED), props);
}
/**
* Return a CloudWatch Metric indicating job timeout.
*
* This metric is based on the Rule returned by no-args onTimeout() call.
*/
public metricTimeout(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return metricRule(this.metricJobStateRule('TimeoutMetricRule', JobState.TIMEOUT), props);
}
/**
* Creates or retrieves a singleton event rule for the input job state for use with the metric JobState methods.
*
* @param id construct id.
* @param jobState the job state.
* @private
*/
private metricJobStateRule(id: string, jobState: JobState): events.Rule {
return this.node.tryFindChild(id) as events.Rule ?? this.onStateChange(id, jobState);
}
}
/**
* Properties for enabling Spark UI monitoring feature for Spark-based Glue jobs.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitor-spark-ui-jobs.html
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
export interface SparkUIProps {
/**
* Enable Spark UI.
*/
readonly enabled: boolean
/**
* The bucket where the Glue job stores the logs.
*
* @default a new bucket will be created.
*/
readonly bucket?: s3.IBucket;
/**
* The path inside the bucket (objects prefix) where the Glue job stores the logs.
*
* @default '/' - the logs will be written at the root of the bucket
*/
readonly prefix?: string;
}
/**
* The Spark UI logging location.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitor-spark-ui-jobs.html
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
export interface SparkUILoggingLocation {
/**
* The bucket where the Glue job stores the logs.
*/
readonly bucket: s3.IBucket;
/**
* The path inside the bucket (objects prefix) where the Glue job stores the logs.
*
* @default '/' - the logs will be written at the root of the bucket
*/
readonly prefix?: string;
}
/**
* Properties for enabling Continuous Logging for Glue Jobs.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitor-continuous-logging-enable.html
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
export interface ContinuousLoggingProps {
/**
* Enable continouous logging.
*/
readonly enabled: boolean;
/**
* Specify a custom CloudWatch log group name.
*
* @default - a log group is created with name `/aws-glue/jobs/logs-v2/`.
*/
readonly logGroup?: logs.ILogGroup;
/**
* Specify a custom CloudWatch log stream prefix.
*
* @default - the job run ID.
*/
readonly logStreamPrefix?: string;
/**
* Filter out non-useful Apache Spark driver/executor and Apache Hadoop YARN heartbeat log messages.
*
* @default true
*/
readonly quiet?: boolean;
/**
* Apply the provided conversion pattern.
*
* This is a Log4j Conversion Pattern to customize driver and executor logs.
*
* @default `%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n`
*/
readonly conversionPattern?: string;
}
/**
* Attributes for importing {@link Job}.
*/
export interface JobAttributes {
/**
* The name of the job.
*/
readonly jobName: string;
/**
* The IAM role assumed by Glue to run this job.
*
* @default - undefined
*/
readonly role?: iam.IRole;
}
/**
* Construction properties for {@link Job}.
*/
export interface JobProps {
/**
* The job's executable properties.
*/
readonly executable: JobExecutable;
/**
* The name of the job.
*
* @default - a name is automatically generated
*/
readonly jobName?: string;
/**
* The description of the job.
*
* @default - no value
*/
readonly description?: string;
/**
* The number of AWS Glue data processing units (DPUs) that can be allocated when this job runs.
* Cannot be used for Glue version 2.0 and later - workerType and workerCount should be used instead.
*
* @default - 10 when job type is Apache Spark ETL or streaming, 0.0625 when job type is Python shell
*/
readonly maxCapacity?: number;
/**
* The maximum number of times to retry this job after a job run fails.
*
* @default 0
*/
readonly maxRetries?: number;
/**
* The maximum number of concurrent runs allowed for the job.
*
* An error is returned when this threshold is reached. The maximum value you can specify is controlled by a service limit.
*
* @default 1
*/
readonly maxConcurrentRuns?: number;
/**
* The number of minutes to wait after a job run starts, before sending a job run delay notification.
*
* @default - no delay notifications
*/
readonly notifyDelayAfter?: cdk.Duration;
/**
* The maximum time that a job run can consume resources before it is terminated and enters TIMEOUT status.
*
* @default cdk.Duration.hours(48)
*/
readonly timeout?: cdk.Duration;
/**
* The type of predefined worker that is allocated when a job runs.
*
* @default - differs based on specific Glue version
*/
readonly workerType?: WorkerType;
/**
* The number of workers of a defined {@link WorkerType} that are allocated when a job runs.
*
* @default - differs based on specific Glue version/worker type
*/
readonly workerCount?: number;
/**
* The {@link Connection}s used for this job.
*
* Connections are used to connect to other AWS Service or resources within a VPC.
*
* @default [] - no connections are added to the job
*/
readonly connections?: IConnection[];
/**
* The {@link SecurityConfiguration} to use for this job.
*
* @default - no security configuration.
*/
readonly securityConfiguration?: ISecurityConfiguration;
/**
* The default arguments for this job, specified as name-value pairs.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html for a list of reserved parameters
* @default - no arguments
*/
readonly defaultArguments?: { [key: string]: string };
/**
* The tags to add to the resources on which the job runs
*
* @default {} - no tags
*/
readonly tags?: { [key: string]: string };
/**
* The IAM role assumed by Glue to run this job.
*
* If providing a custom role, it needs to trust the Glue service principal (glue.amazonaws.com) and be granted sufficient permissions.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/getting-started-access.html
*
* @default - a role is automatically generated
*/
readonly role?: iam.IRole;
/**
* Enables the collection of metrics for job profiling.
*
* @default - no profiling metrics emitted.
*
* @see `--enable-metrics` at https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly enableProfilingMetrics? :boolean;
/**
* Enables the Spark UI debugging and monitoring with the specified props.
*
* @default - Spark UI debugging and monitoring is disabled.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitor-spark-ui-jobs.html
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly sparkUI?: SparkUIProps,
/**
* Enables continuous logging with the specified props.
*
* @default - continuous logging is disabled.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitor-continuous-logging-enable.html
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly continuousLogging?: ContinuousLoggingProps,
}
/**
* A Glue Job.
*/
export class Job extends JobBase {
/**
* Creates a Glue Job
*
* @param scope The scope creating construct (usually `this`).
* @param id The construct's id.
* @param attrs Import attributes
*/
public static fromJobAttributes(scope: constructs.Construct, id: string, attrs: JobAttributes): IJob {
class Import extends JobBase {
public readonly jobName = attrs.jobName;
public readonly jobArn = jobArn(scope, attrs.jobName);
public readonly grantPrincipal = attrs.role ?? new iam.UnknownPrincipal({ resource: this });
}
return new Import(scope, id);
}
/**
* The ARN of the job.
*/
public readonly jobArn: string;
/**
* The name of the job.
*/
public readonly jobName: string;
/**
* The IAM role Glue assumes to run this job.
*/
public readonly role: iam.IRole;
/**
* The principal this Glue Job is running as.
*/
public readonly grantPrincipal: iam.IPrincipal;
/**
* The Spark UI logs location if Spark UI monitoring and debugging is enabled.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/monitor-spark-ui-jobs.html
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
public readonly sparkUILoggingLocation?: SparkUILoggingLocation;
constructor(scope: constructs.Construct, id: string, props: JobProps) {
super(scope, id, {
physicalName: props.jobName,
});
const executable = props.executable.bind();
this.role = props.role ?? new iam.Role(this, 'ServiceRole', {
assumedBy: new iam.ServicePrincipal('glue.amazonaws.com'),
managedPolicies: [iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSGlueServiceRole')],
});
this.grantPrincipal = this.role;
const sparkUI = props.sparkUI?.enabled ? this.setupSparkUI(executable, this.role, props.sparkUI) : undefined;
this.sparkUILoggingLocation = sparkUI?.location;
const continuousLoggingArgs = props.continuousLogging?.enabled ? this.setupContinuousLogging(this.role, props.continuousLogging) : {};
const profilingMetricsArgs = props.enableProfilingMetrics ? { '--enable-metrics': '' } : {};
const defaultArguments = {
...this.executableArguments(executable),
...continuousLoggingArgs,
...profilingMetricsArgs,
...sparkUI?.args,
...this.checkNoReservedArgs(props.defaultArguments),
};
const jobResource = new CfnJob(this, 'Resource', {
name: props.jobName,
description: props.description,
role: this.role.roleArn,
command: {
name: executable.type.name,
scriptLocation: this.codeS3ObjectUrl(executable.script),
pythonVersion: executable.pythonVersion,
},
glueVersion: executable.glueVersion.name,
workerType: props.workerType?.name,
numberOfWorkers: props.workerCount,
maxCapacity: props.maxCapacity,
maxRetries: props.maxRetries,
executionProperty: props.maxConcurrentRuns ? { maxConcurrentRuns: props.maxConcurrentRuns } : undefined,
notificationProperty: props.notifyDelayAfter ? { notifyDelayAfter: props.notifyDelayAfter.toMinutes() } : undefined,
timeout: props.timeout?.toMinutes(),
connections: props.connections ? { connections: props.connections.map((connection) => connection.connectionName) } : undefined,
securityConfiguration: props.securityConfiguration?.securityConfigurationName,
tags: props.tags,
defaultArguments,
});
const resourceName = this.getResourceNameAttribute(jobResource.ref);
this.jobArn = jobArn(this, resourceName);
this.jobName = resourceName;
}
/**
* Check no usage of reserved arguments.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
private checkNoReservedArgs(defaultArguments?: { [key: string]: string }) {
if (defaultArguments) {
const reservedArgs = new Set(['--conf', '--debug', '--mode', '--JOB_NAME']);
Object.keys(defaultArguments).forEach((arg) => {
if (reservedArgs.has(arg)) {
throw new Error(`The ${arg} argument is reserved by Glue. Don't set it`);
}
});
}
return defaultArguments;
}
private executableArguments(config: JobExecutableConfig) {
const args: { [key: string]: string } = {};
args['--job-language'] = config.language;
if (config.className) {
args['--class'] = config.className;
}
if (config.extraJars && config.extraJars?.length > 0) {
args['--extra-jars'] = config.extraJars.map(code => this.codeS3ObjectUrl(code)).join(',');
}
if (config.extraPythonFiles && config.extraPythonFiles.length > 0) {
args['--extra-py-files'] = config.extraPythonFiles.map(code => this.codeS3ObjectUrl(code)).join(',');
}
if (config.extraFiles && config.extraFiles.length > 0) {
args['--extra-files'] = config.extraFiles.map(code => this.codeS3ObjectUrl(code)).join(',');
}
if (config.extraJarsFirst) {
args['--user-jars-first'] = 'true';
}
return args;
}
private setupSparkUI(executable: JobExecutableConfig, role: iam.IRole, props: SparkUIProps) {
if (JobType.PYTHON_SHELL === executable.type) {
throw new Error('Spark UI is not available for JobType.PYTHON_SHELL jobs');
}
const bucket = props.bucket ?? new s3.Bucket(this, 'SparkUIBucket');
bucket.grantReadWrite(role);
const args = {
'--enable-spark-ui': 'true',
'--spark-event-logs-path': bucket.s3UrlForObject(props.prefix),
};
return {
location: {
prefix: props.prefix,
bucket,
},
args,
};
}
private setupContinuousLogging(role: iam.IRole, props: ContinuousLoggingProps) {
const args: {[key: string]: string} = {
'--enable-continuous-cloudwatch-log': 'true',
'--enable-continuous-log-filter': (props.quiet ?? true).toString(),
};
if (props.logGroup) {
args['--continuous-log-logGroup'] = props.logGroup.logGroupName;
props.logGroup.grantWrite(role);
}
if (props.logStreamPrefix) {
args['--continuous-log-logStreamPrefix'] = props.logStreamPrefix;
}
if (props.conversionPattern) {
args['--continuous-log-conversionPattern'] = props.conversionPattern;
}
return args;
}
private codeS3ObjectUrl(code: Code) {
const s3Location = code.bind(this, this.role).s3Location;
return `s3://${s3Location.bucketName}/${s3Location.objectKey}`;
}
}
/**
* Create a CloudWatch Metric that's based on Glue Job events
* {@see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EventTypes.html#glue-event-types}
* The metric has namespace = 'AWS/Events', metricName = 'TriggeredRules' and RuleName = rule.ruleName dimension.
*
* @param rule for use in setting RuleName dimension value
* @param props metric properties
*/
function metricRule(rule: events.IRule, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/Events',
metricName: 'TriggeredRules',
dimensionsMap: { RuleName: rule.ruleName },
statistic: cloudwatch.Statistic.SUM,
...props,
}).attachTo(rule);
}
/**
* Returns the job arn
* @param scope
* @param jobName
*/
function jobArn(scope: constructs.Construct, jobName: string) : string {
return cdk.Stack.of(scope).formatArn({
service: 'glue',
resource: 'job',
resourceName: jobName,
});
}