-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathservice.ts
316 lines (264 loc) · 12.3 KB
/
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";
import * as ecs from ".";
import * as x from "..";
import * as utils from "../utils";
export abstract class Service extends pulumi.ComponentResource {
public readonly service: aws.ecs.Service;
public readonly cluster: ecs.Cluster;
public readonly taskDefinition: ecs.TaskDefinition;
/**
* Mapping from container in this service to the ELB listener exposing it through a load
* balancer. Only present if a listener was provided in [Container.portMappings] or in
* [Container.applicationListener] or [Container.networkListener].
*/
public readonly listeners: Record<string, x.lb.Listener> = {};
public readonly applicationListeners: Record<string, x.lb.ApplicationListener> = {};
public readonly networkListeners: Record<string, x.lb.NetworkListener> = {};
constructor(type: string, name: string,
args: ServiceArgs,
opts: pulumi.ComponentResourceOptions) {
super(type, name, {}, opts);
this.cluster = args.cluster || x.ecs.Cluster.getDefault();
this.listeners = args.taskDefinition.listeners;
this.applicationListeners = args.taskDefinition.applicationListeners;
this.networkListeners = args.taskDefinition.networkListeners;
// Determine which load balancers we're attached to based on the information supplied to the
// containers for this service.
const loadBalancers = getLoadBalancers(this, name, args);
this.service = new aws.ecs.Service(name, {
...args,
loadBalancers,
cluster: this.cluster.cluster.arn,
taskDefinition: args.taskDefinition.taskDefinition.arn,
desiredCount: utils.ifUndefined(args.desiredCount, 1),
launchType: utils.ifUndefined(args.launchType, "EC2"),
waitForSteadyState: utils.ifUndefined(args.waitForSteadyState, true),
}, pulumi.mergeOptions(opts, {
parent: this,
dependsOn: this.cluster.autoScalingGroups.map(g => g.stack),
}));
this.taskDefinition = args.taskDefinition;
}
}
function getLoadBalancers(service: ecs.Service, name: string, args: ServiceArgs) {
const result: pulumi.Output<ServiceLoadBalancer>[] = [];
// Get the initial set of load balancers if specified directly in our args.
if (args.loadBalancers) {
for (const obj of args.loadBalancers) {
const loadBalancer = isServiceLoadBalancerProvider(obj)
? obj.serviceLoadBalancer(name, service)
: obj;
result.push(pulumi.output(loadBalancer));
}
}
const containerLoadBalancerProviders = new Map<string, ecs.ContainerLoadBalancerProvider>();
// Now walk each container and see if it wants to add load balancer information as well.
for (const containerName of Object.keys(args.taskDefinition.containers)) {
const container = args.taskDefinition.containers[containerName];
if (!container.portMappings) {
continue;
}
for (const obj of container.portMappings) {
if (x.ecs.isContainerLoadBalancerProvider(obj)) {
containerLoadBalancerProviders.set(containerName, obj);
}
}
}
// Finally see if we were directly given load balancing listeners to associate our containers
// with. If so, use their information to populate our LB information.
for (const containerName of Object.keys(service.listeners)) {
if (!containerLoadBalancerProviders.has(containerName)) {
containerLoadBalancerProviders.set(containerName, service.listeners[containerName]);
}
}
for (const [containerName, provider] of containerLoadBalancerProviders) {
processContainerLoadBalancerProvider(containerName, provider);
}
return pulumi.output(result);
function processContainerLoadBalancerProvider(containerName: string, prov: ecs.ContainerLoadBalancerProvider) {
// Containers don't know their own name. So we add the name in here on their behalf.
const containerLoadBalancer = prov.containerLoadBalancer(name, service);
const serviceLoadBalancer = pulumi.output(containerLoadBalancer).apply(
lb => ({ ...lb, containerName }));
result.push(serviceLoadBalancer);
}
}
export interface ServiceLoadBalancer {
containerName: pulumi.Input<string>;
containerPort: pulumi.Input<number>;
elbName?: pulumi.Input<string>;
targetGroupArn?: pulumi.Input<string>;
}
export interface ServiceLoadBalancerProvider {
serviceLoadBalancer(name: string, parent: pulumi.Resource): pulumi.Input<ServiceLoadBalancer>;
}
/** @internal */
export function isServiceLoadBalancerProvider(obj: any): obj is ServiceLoadBalancerProvider {
return obj && (<ServiceLoadBalancerProvider>obj).serviceLoadBalancer instanceof Function;
}
// The shape we want for ClusterFileSystemArgs. We don't export this as 'Overwrite' types are not pleasant to
// work with. However, they internally allow us to succinctly express the shape we're trying to
// provide. Code later on will ensure these types are compatible.
type OverwriteShape = utils.Overwrite<utils.Mutable<aws.ecs.ServiceArgs>, {
cluster?: ecs.Cluster;
taskDefinition: ecs.TaskDefinition;
securityGroups: x.ec2.SecurityGroup[];
desiredCount?: pulumi.Input<number>;
launchType?: pulumi.Input<"EC2" | "FARGATE">;
os?: pulumi.Input<"linux" | "windows">;
waitForSteadyState?: pulumi.Input<boolean>;
loadBalancers?: (pulumi.Input<ServiceLoadBalancer> | ServiceLoadBalancerProvider)[];
tags?: pulumi.Input<aws.Tags>;
}>;
export interface NetworkConfiguration {
/**
* Assign a public IP address to the ENI (Fargate launch type only). Valid values are true or
* false. Default false.
*
*/
assignPublicIp?: pulumi.Input<boolean>;
/**
* The security groups associated with the task or service. If you do not specify a security
* group, the default security group for the VPC is used.
*/
securityGroups?: pulumi.Input<pulumi.Input<string>[]>;
/**
* The subnets associated with the task or service.
*/
subnets: pulumi.Input<pulumi.Input<string>[]>;
}
export interface ServiceArgs {
// Properties from aws.ecs.ServiceArgs
/**
* The capacity provider strategy to use for the service.
*/
capacityProviderStrategies?: aws.ecs.ServiceArgs["capacityProviderStrategies"];
/**
* onfiguration block containing deployment controller configuration.
*/
deploymentController?: aws.ecs.ServiceArgs["deploymentController"];
/**
* The upper limit (as a percentage of the service's desiredCount) of the number of running
* tasks that can be running in a service during a deployment. Not valid when using the `DAEMON`
* scheduling strategy.
*/
deploymentMaximumPercent?: pulumi.Input<number>;
/**
* The lower limit (as a percentage of the service's desiredCount) of the number of running
* tasks that must remain running and healthy in a service during a deployment.
*/
deploymentMinimumHealthyPercent?: pulumi.Input<number>;
/**
* The number of instances of the task definition to place and keep running. Defaults to 1. Do
* not specify if using the `DAEMON` scheduling strategy.
*/
desiredCount?: pulumi.Input<number>;
/**
* Specifies whether to enable Amazon ECS managed tags for the tasks within the service.
*/
enableEcsManagedTags?: pulumi.Input<boolean>;
/**
* Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent
* premature shutdown, up to 7200. Only valid for services configured to use load balancers.
*/
healthCheckGracePeriodSeconds?: pulumi.Input<number>;
/**
* ARN of the IAM role that allows Amazon ECS to make calls to your load balancer on your
* behalf. This parameter is required if you are using a load balancer with your service, but
* only if your task definition does not use the `awsvpc` network mode. If using `awsvpc`
* network mode, do not specify this role. If your account has already created the Amazon ECS
* service-linked role, that role is used by default for your service unless you specify a role
* here.
*/
iamRole?: pulumi.Input<string>;
/**
* The launch type on which to run your service. The valid values are `EC2` and `FARGATE`.
* Defaults to `EC2`.
*/
launchType?: pulumi.Input<"EC2" | "FARGATE">;
/**
* A load balancer block. Load balancers documented below.
*/
loadBalancers?: (pulumi.Input<ServiceLoadBalancer> | ServiceLoadBalancerProvider)[];
/**
* The name of the service (up to 255 letters, numbers, hyphens, and underscores)
*/
name?: pulumi.Input<string>;
/**
* The network configuration for the service. This parameter is required for task definitions
* that use the `awsvpc` network mode to receive their own Elastic Network Interface, and it is
* not supported for other network modes.
*/
networkConfiguration?: pulumi.Input<NetworkConfiguration>;
/**
* Service level strategy rules that are taken into consideration during task placement. List
* from top to bottom in order of precedence. The maximum number of `ordered_placement_strategy`
* blocks is `5`. Defined below.
*/
orderedPlacementStrategies?: aws.ecs.ServiceArgs["orderedPlacementStrategies"];
/**
* rules that are taken into consideration during task placement. Maximum number of
* `placement_constraints` is `10`. Defined below.
*/
placementConstraints?: aws.ecs.ServiceArgs["placementConstraints"];
/**
* The platform version on which to run your service. Only applicable for `launchType` set to `FARGATE`.
* Defaults to `LATEST`. More information about Fargate platform versions can be found in the
* [AWS ECS User Guide](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html).
*/
platformVersion?: pulumi.Input<string>;
/**
* Specifies whether to propagate the tags from the task definition or the service
* to the tasks. The valid values are `SERVICE` and `TASK_DEFINITION`.
*/
propagateTags?: pulumi.Input<string>;
/**
* The scheduling strategy to use for the service. The valid values are `REPLICA` and `DAEMON`.
* Defaults to `REPLICA`. Note that [*Fargate tasks do not support the `DAEMON` scheduling
* strategy*](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/scheduling_tasks.html).
*/
schedulingStrategy?: pulumi.Input<string>;
/**
* The service discovery registries for the service. The maximum number of `service_registries` blocks is `1`.
*/
serviceRegistries?: aws.ecs.ServiceArgs["serviceRegistries"];
// Changes we made to the core args type.
/**
* Cluster this service will run in. If not specified [Cluster.getDefault()] will be used.
*/
cluster?: ecs.Cluster;
/**
* The task definition to create the service from.
*/
taskDefinition: ecs.TaskDefinition;
/**
* Security groups determining how this service can be reached.
*/
securityGroups: x.ec2.SecurityGroup[];
/**
* Wait for the service to reach a steady state (like [`aws ecs wait
* services-stable`](https://docs.aws.amazon.com/cli/latest/reference/ecs/wait/services-stable.html))
* before continuing. Defaults to `true`.
*/
waitForSteadyState?: pulumi.Input<boolean>;
/**
* Key-value mapping of resource tags
*/
tags?: pulumi.Input<aws.Tags>;
}
// Make sure our exported args shape is compatible with the overwrite shape we're trying to provide.
const test1: string = utils.checkCompat<OverwriteShape, ServiceArgs>();