-
Notifications
You must be signed in to change notification settings - Fork 4k
/
queue-processing-fargate-service.ts
124 lines (114 loc) · 4.29 KB
/
queue-processing-fargate-service.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
import { FargatePlatformVersion, FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs';
import { Construct } from 'constructs';
import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base';
/**
* The properties for the QueueProcessingFargateService service.
*/
export interface QueueProcessingFargateServiceProps extends QueueProcessingServiceBaseProps {
/**
* 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 amount (in MiB) of memory used by the task.
*
* This field is required and you must use one of the following values, which determines your range of valid values
* for the cpu parameter:
*
* 0.5GB, 1GB, 2GB - Available cpu values: 256 (.25 vCPU)
*
* 1GB, 2GB, 3GB, 4GB - Available cpu values: 512 (.5 vCPU)
*
* 2GB, 3GB, 4GB, 5GB, 6GB, 7GB, 8GB - Available cpu values: 1024 (1 vCPU)
*
* Between 4GB and 16GB in 1GB increments - Available cpu values: 2048 (2 vCPU)
*
* Between 8GB and 30GB in 1GB increments - Available cpu values: 4096 (4 vCPU)
*
* This default is set in the underlying FargateTaskDefinition construct.
*
* @default 512
*/
readonly memoryLimitMiB?: number;
/**
* The platform version on which to run your service.
*
* If one is not specified, the LATEST platform version is used by default. For more information, see
* [AWS Fargate Platform Versions](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html)
* in the Amazon Elastic Container Service Developer Guide.
*
* @default Latest
*/
readonly platformVersion?: FargatePlatformVersion;
/**
* Optional name for the container added
*
* @default - QueueProcessingContainer
*/
readonly containerName?: string;
}
/**
* Class to create a queue processing Fargate service
*/
export class QueueProcessingFargateService extends QueueProcessingServiceBase {
/**
* The Fargate service in this construct.
*/
public readonly service: FargateService;
/**
* The Fargate task definition in this construct.
*/
public readonly taskDefinition: FargateTaskDefinition;
/**
* Constructs a new instance of the QueueProcessingFargateService class.
*/
constructor(scope: Construct, id: string, props: QueueProcessingFargateServiceProps) {
super(scope, id, props);
// Create a Task Definition for the container to start
this.taskDefinition = new FargateTaskDefinition(this, 'QueueProcessingTaskDef', {
memoryLimitMiB: props.memoryLimitMiB || 512,
cpu: props.cpu || 256,
family: props.family,
});
const containerName = props.containerName ?? 'QueueProcessingContainer';
this.taskDefinition.addContainer(containerName, {
image: props.image,
command: props.command,
environment: this.environment,
secrets: this.secrets,
logging: this.logDriver,
});
// Create a Fargate service with the previously defined Task Definition and configure
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
this.service = new FargateService(this, 'QueueProcessingFargateService', {
cluster: this.cluster,
desiredCount: this.desiredCount,
taskDefinition: this.taskDefinition,
serviceName: props.serviceName,
minHealthyPercent: props.minHealthyPercent,
maxHealthyPercent: props.maxHealthyPercent,
propagateTags: props.propagateTags,
enableECSManagedTags: props.enableECSManagedTags,
platformVersion: props.platformVersion,
deploymentController: props.deploymentController,
});
this.configureAutoscalingForService(this.service);
this.grantPermissionsToService(this.service);
}
}