-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
application-load-balanced-ecs-service.ts
164 lines (149 loc) · 6.19 KB
/
application-load-balanced-ecs-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
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
import { Ec2Service, Ec2TaskDefinition, PlacementConstraint, PlacementStrategy } from '@aws-cdk/aws-ecs';
import { FeatureFlags } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { ApplicationLoadBalancedServiceBase, ApplicationLoadBalancedServiceBaseProps } from '../base/application-load-balanced-service-base';
/**
* The properties for the ApplicationLoadBalancedEc2Service service.
*/
export interface ApplicationLoadBalancedEc2ServiceProps extends ApplicationLoadBalancedServiceBaseProps {
/**
* The task definition to use for tasks in the service. TaskDefinition or TaskImageOptions must be specified, but not both..
*
* [disable-awslint:ref-via-interface]
*
* @default - none
*/
readonly taskDefinition?: Ec2TaskDefinition;
/**
* 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 none
*/
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.
*
* At least one of memoryLimitMiB and memoryReservationMiB is required.
*
* @default - No memory limit.
*/
readonly memoryLimitMiB?: number;
/**
* The soft limit (in MiB) of memory to reserve for the container.
*
* When system memory is under contention, Docker attempts to keep the
* container memory within the limit. If the container requires more memory,
* it can consume up to the value specified by the Memory property or all of
* the available memory on the container instance—whichever comes first.
*
* At least one of memoryLimitMiB and memoryReservationMiB is required.
*
* @default - No memory reserved.
*/
readonly memoryReservationMiB?: number;
/**
* The placement constraints to use for tasks in the service. For more information, see
* [Amazon ECS Task Placement Constraints](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html).
*
* @default - No constraints.
*/
readonly placementConstraints?: PlacementConstraint[];
/**
* The placement strategies to use for tasks in the service. For more information, see
* [Amazon ECS Task Placement Strategies](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html).
*
* @default - No strategies.
*/
readonly placementStrategies?: PlacementStrategy[];
}
/**
* An EC2 service running on an ECS cluster fronted by an application load balancer.
*/
export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedServiceBase {
/**
* The EC2 service in this construct.
*/
public readonly service: Ec2Service;
/**
* The EC2 Task Definition in this construct.
*/
public readonly taskDefinition: Ec2TaskDefinition;
/**
* Constructs a new instance of the ApplicationLoadBalancedEc2Service class.
*/
constructor(scope: Construct, id: string, props: ApplicationLoadBalancedEc2ServiceProps = {}) {
super(scope, id, props);
if (props.taskDefinition && props.taskImageOptions) {
throw new Error('You must specify either a taskDefinition or taskImageOptions, not both.');
} else if (props.taskDefinition) {
this.taskDefinition = props.taskDefinition;
} else if (props.taskImageOptions) {
const taskImageOptions = props.taskImageOptions;
this.taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', {
executionRole: taskImageOptions.executionRole,
taskRole: taskImageOptions.taskRole,
family: taskImageOptions.family,
});
// Create log driver if logging is enabled
const enableLogging = taskImageOptions.enableLogging ?? true;
const logDriver = taskImageOptions.logDriver ?? (enableLogging ? this.createAWSLogDriver(this.node.id) : undefined);
const containerName = taskImageOptions.containerName ?? 'web';
const container = this.taskDefinition.addContainer(containerName, {
image: taskImageOptions.image,
cpu: props.cpu,
memoryLimitMiB: props.memoryLimitMiB,
memoryReservationMiB: props.memoryReservationMiB,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
logging: logDriver,
dockerLabels: taskImageOptions.dockerLabels,
command: taskImageOptions.command,
entryPoint: taskImageOptions.entryPoint,
});
container.addPortMappings({
containerPort: taskImageOptions.containerPort || 80,
});
} else {
throw new Error('You must specify one of: taskDefinition or image');
}
const desiredCount = FeatureFlags.of(this).isEnabled(cxapi.ECS_REMOVE_DEFAULT_DESIRED_COUNT) ? this.internalDesiredCount : this.desiredCount;
this.service = new Ec2Service(this, 'Service', {
cluster: this.cluster,
desiredCount: desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: false,
serviceName: props.serviceName,
healthCheckGracePeriod: props.healthCheckGracePeriod,
minHealthyPercent: props.minHealthyPercent,
maxHealthyPercent: props.maxHealthyPercent,
propagateTags: props.propagateTags,
enableECSManagedTags: props.enableECSManagedTags,
cloudMapOptions: props.cloudMapOptions,
deploymentController: props.deploymentController,
circuitBreaker: props.circuitBreaker,
enableExecuteCommand: props.enableExecuteCommand,
placementConstraints: props.placementConstraints,
placementStrategies: props.placementStrategies,
capacityProviderStrategies: props.capacityProviderStrategies,
});
this.addServiceAsTarget(this.service);
}
}