-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
scheduled-fargate-task.ts
114 lines (105 loc) · 4.06 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
106
107
108
109
110
111
112
113
114
import { FargateTaskDefinition } from '@aws-cdk/aws-ecs';
import { Construct } from '@aws-cdk/core';
import { ScheduledTaskBase, ScheduledTaskBaseProps, ScheduledTaskImageProps } from '../base/scheduled-task-base';
/**
* The properties for the ScheduledFargateTask task.
*/
export interface ScheduledFargateTaskProps extends ScheduledTaskBaseProps {
/**
* 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 {
/**
* The number of cpu units used by the task.
*
* Valid values, which determines your range of valid values for the memory parameter:
*
* 256 (.25 vCPU) - Available memory values: 0.5GB, 1GB, 2GB
*
* 512 (.5 vCPU) - Available memory values: 1GB, 2GB, 3GB, 4GB
*
* 1024 (1 vCPU) - Available memory values: 2GB, 3GB, 4GB, 5GB, 6GB, 7GB, 8GB
*
* 2048 (2 vCPU) - Available memory values: Between 4GB and 16GB in 1GB increments
*
* 4096 (4 vCPU) - Available memory values: Between 8GB and 30GB in 1GB increments
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 256
*/
readonly cpu?: number;
/**
* The hard limit (in MiB) of memory to present to the container.
*
* If your container attempts to exceed the allocated memory, the container
* is terminated.
*
* @default 512
*/
readonly memoryLimitMiB?: number;
}
/**
* 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;
/**
* 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 !== undefined ? taskImageOptions.logDriver : this.createAWSLogDriver(this.node.id)
});
} else {
throw new Error('You must specify one of: taskDefinition or image');
}
this.addTaskDefinitionToEventTarget(this.taskDefinition);
}
}