-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathscheduled-fargate-task.ts
105 lines (93 loc) · 3.74 KB
/
scheduled-fargate-task.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
import { Construct } from 'constructs';
import { FargateTaskDefinition } from '../../../aws-ecs';
import { EcsTask } from '../../../aws-events-targets';
import { FargateServiceBaseProps } from '../base/fargate-service-base';
import { ScheduledTaskBase, ScheduledTaskBaseProps, ScheduledTaskImageProps } from '../base/scheduled-task-base';
/**
* The properties for the ScheduledFargateTask task.
*/
export interface ScheduledFargateTaskProps extends ScheduledTaskBaseProps, FargateServiceBaseProps {
/**
* The properties to define if using an existing TaskDefinition in this construct.
* ScheduledFargateTaskDefinitionOptions or ScheduledFargateTaskImageOptions must be defined, but not both.
*
* @default none
*/
readonly scheduledFargateTaskDefinitionOptions?: ScheduledFargateTaskDefinitionOptions;
/**
* The properties to define if the construct is to create a TaskDefinition.
* ScheduledFargateTaskDefinitionOptions or ScheduledFargateTaskImageOptions must be defined, but not both.
*
* @default none
*/
readonly scheduledFargateTaskImageOptions?: ScheduledFargateTaskImageOptions;
}
/**
* The properties for the ScheduledFargateTask using an image.
*/
export interface ScheduledFargateTaskImageOptions extends ScheduledTaskImageProps, FargateServiceBaseProps {
}
/**
* The properties for the ScheduledFargateTask using a task definition.
*/
export interface ScheduledFargateTaskDefinitionOptions {
/**
* The task definition to use for tasks in the service. Image or taskDefinition must be specified, but not both.
*
* [disable-awslint:ref-via-interface]
*
* @default - none
*/
readonly taskDefinition: FargateTaskDefinition;
}
/**
* A scheduled Fargate task that will be initiated off of CloudWatch Events.
*/
export class ScheduledFargateTask extends ScheduledTaskBase {
/**
* The Fargate task definition in this construct.
*/
public readonly taskDefinition: FargateTaskDefinition;
/**
* The ECS task in this construct.
*/
public readonly task: EcsTask;
/**
* Constructs a new instance of the ScheduledFargateTask class.
*/
constructor(scope: Construct, id: string, props: ScheduledFargateTaskProps) {
super(scope, id, props);
if (props.scheduledFargateTaskDefinitionOptions && props.scheduledFargateTaskImageOptions) {
throw new Error('You must specify either a scheduledFargateTaskDefinitionOptions or scheduledFargateTaskOptions, not both.');
} else if (props.scheduledFargateTaskDefinitionOptions) {
this.taskDefinition = props.scheduledFargateTaskDefinitionOptions.taskDefinition;
} else if (props.scheduledFargateTaskImageOptions) {
const taskImageOptions = props.scheduledFargateTaskImageOptions;
this.taskDefinition = new FargateTaskDefinition(this, 'ScheduledTaskDef', {
memoryLimitMiB: taskImageOptions.memoryLimitMiB || 512,
cpu: taskImageOptions.cpu || 256,
});
this.taskDefinition.addContainer('ScheduledContainer', {
image: taskImageOptions.image,
command: taskImageOptions.command,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
logging: taskImageOptions.logDriver ?? this.createAWSLogDriver(this.node.id),
});
} else {
throw new Error('You must specify one of: taskDefinition or image');
}
// Use the EcsTask as the target of the EventRule
this.task = new EcsTask( {
cluster: this.cluster,
taskDefinition: this.taskDefinition,
taskCount: this.desiredTaskCount,
subnetSelection: this.subnetSelection,
platformVersion: props.platformVersion,
securityGroups: props.securityGroups,
propagateTags: props.propagateTags,
tags: props.tags,
});
this.addTaskAsTarget(this.task);
}
}