/
application-multiple-target-groups-fargate-service.ts
131 lines (117 loc) · 4.87 KB
/
application-multiple-target-groups-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
125
126
127
128
129
130
131
import { Construct } from 'constructs';
import { FargateService, FargateTaskDefinition } from '../../../aws-ecs';
import { ApplicationTargetGroup } from '../../../aws-elasticloadbalancingv2';
import { FeatureFlags } from '../../../core';
import * as cxapi from '../../../cx-api';
import {
ApplicationMultipleTargetGroupsServiceBase,
ApplicationMultipleTargetGroupsServiceBaseProps,
} from '../base/application-multiple-target-groups-service-base';
import { FargateServiceBaseProps } from '../base/fargate-service-base';
/**
* The properties for the ApplicationMultipleTargetGroupsFargateService service.
*/
export interface ApplicationMultipleTargetGroupsFargateServiceProps extends ApplicationMultipleTargetGroupsServiceBaseProps, FargateServiceBaseProps {
/**
* Determines whether the service will be assigned a public IP address.
*
* @default false
*/
readonly assignPublicIp?: boolean;
}
/**
* A Fargate service running on an ECS cluster fronted by an application load balancer.
*/
export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMultipleTargetGroupsServiceBase {
/**
* Determines whether the service will be assigned a public IP address.
*/
public readonly assignPublicIp: boolean;
/**
* The Fargate service in this construct.
*/
public readonly service: FargateService;
/**
* The Fargate task definition in this construct.
*/
public readonly taskDefinition: FargateTaskDefinition;
/**
* The default target group for the service.
* @deprecated - Use `targetGroups` instead.
*/
public readonly targetGroup: ApplicationTargetGroup;
/**
* Constructs a new instance of the ApplicationMultipleTargetGroupsFargateService class.
*/
constructor(scope: Construct, id: string, props: ApplicationMultipleTargetGroupsFargateServiceProps = {}) {
super(scope, id, props);
this.assignPublicIp = props.assignPublicIp ?? false;
if (props.taskDefinition && props.taskImageOptions) {
throw new Error('You must specify only one of TaskDefinition or TaskImageOptions.');
} else if (props.taskDefinition) {
this.taskDefinition = props.taskDefinition;
} else if (props.taskImageOptions) {
const taskImageOptions = props.taskImageOptions;
this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', {
memoryLimitMiB: props.memoryLimitMiB,
cpu: props.cpu,
ephemeralStorageGiB: props.ephemeralStorageGiB,
executionRole: taskImageOptions.executionRole,
taskRole: taskImageOptions.taskRole,
family: taskImageOptions.family,
runtimePlatform: props.runtimePlatform,
});
const containerName = taskImageOptions.containerName ?? 'web';
const container = this.taskDefinition.addContainer(containerName, {
image: taskImageOptions.image,
logging: this.logDriver,
environment: taskImageOptions.environment,
secrets: taskImageOptions.secrets,
dockerLabels: taskImageOptions.dockerLabels,
});
if (taskImageOptions.containerPorts) {
for (const containerPort of taskImageOptions.containerPorts) {
container.addPortMappings({
containerPort,
});
}
}
} else {
throw new Error('You must specify one of: taskDefinition or image');
}
if (!this.taskDefinition.defaultContainer) {
throw new Error('At least one essential container must be specified');
}
if (this.taskDefinition.defaultContainer.portMappings.length === 0) {
this.taskDefinition.defaultContainer.addPortMappings({
containerPort: 80,
});
}
this.service = this.createFargateService(props);
if (props.targetGroups) {
this.addPortMappingForTargets(this.taskDefinition.defaultContainer, props.targetGroups);
this.targetGroup = this.registerECSTargets(this.service, this.taskDefinition.defaultContainer, props.targetGroups);
} else {
this.targetGroup = this.listener.addTargets('ECS', {
targets: [this.service],
port: this.taskDefinition.defaultContainer.portMappings[0].containerPort,
});
}
}
private createFargateService(props: ApplicationMultipleTargetGroupsFargateServiceProps): FargateService {
const desiredCount = FeatureFlags.of(this).isEnabled(cxapi.ECS_REMOVE_DEFAULT_DESIRED_COUNT) ? this.internalDesiredCount : this.desiredCount;
return new FargateService(this, 'Service', {
cluster: this.cluster,
desiredCount: desiredCount,
taskDefinition: this.taskDefinition,
assignPublicIp: this.assignPublicIp,
serviceName: props.serviceName,
healthCheckGracePeriod: props.healthCheckGracePeriod,
propagateTags: props.propagateTags,
enableECSManagedTags: props.enableECSManagedTags,
cloudMapOptions: props.cloudMapOptions,
platformVersion: props.platformVersion,
enableExecuteCommand: props.enableExecuteCommand,
});
}
}