1 | import * as appscaling from '@aws-cdk/aws-applicationautoscaling';
|
2 | import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
|
3 | import * as ec2 from '@aws-cdk/aws-ec2';
|
4 | import * as elb from '@aws-cdk/aws-elasticloadbalancing';
|
5 | import * as elbv2 from '@aws-cdk/aws-elasticloadbalancingv2';
|
6 | import * as cloudmap from '@aws-cdk/aws-servicediscovery';
|
7 | import { Duration, IResource, Resource } from '@aws-cdk/core';
|
8 | import { Construct } from 'constructs';
|
9 | import { LoadBalancerTargetOptions, TaskDefinition } from '../base/task-definition';
|
10 | import { ICluster, CapacityProviderStrategy } from '../cluster';
|
11 | import { ContainerDefinition, Protocol } from '../container-definition';
|
12 | import { CfnService } from '../ecs.generated';
|
13 | import { ScalableTaskCount } from './scalable-task-count';
|
14 | /**
|
15 | * The interface for a service.
|
16 | */
|
17 | export interface IService extends IResource {
|
18 | /**
|
19 | * The Amazon Resource Name (ARN) of the service.
|
20 | *
|
21 | * @attribute
|
22 | */
|
23 | readonly serviceArn: string;
|
24 | /**
|
25 | * The name of the service.
|
26 | *
|
27 | * @attribute
|
28 | */
|
29 | readonly serviceName: string;
|
30 | }
|
31 | /**
|
32 | * The deployment controller to use for the service.
|
33 | */
|
34 | export interface DeploymentController {
|
35 | /**
|
36 | * The deployment controller type to use.
|
37 | *
|
38 | * @default DeploymentControllerType.ECS
|
39 | */
|
40 | readonly type?: DeploymentControllerType;
|
41 | }
|
42 | /**
|
43 | * The deployment circuit breaker to use for the service
|
44 | */
|
45 | export interface DeploymentCircuitBreaker {
|
46 | /**
|
47 | * Whether to enable rollback on deployment failure
|
48 | * @default false
|
49 | */
|
50 | readonly rollback?: boolean;
|
51 | }
|
52 | export interface EcsTarget {
|
53 | /**
|
54 | * The name of the container.
|
55 | */
|
56 | readonly containerName: string;
|
57 | /**
|
58 | * The port number of the container. Only applicable when using application/network load balancers.
|
59 | *
|
60 | * @default - Container port of the first added port mapping.
|
61 | */
|
62 | readonly containerPort?: number;
|
63 | /**
|
64 | * The protocol used for the port mapping. Only applicable when using application load balancers.
|
65 | *
|
66 | * @default Protocol.TCP
|
67 | */
|
68 | readonly protocol?: Protocol;
|
69 | /**
|
70 | * ID for a target group to be created.
|
71 | */
|
72 | readonly newTargetGroupId: string;
|
73 | /**
|
74 | * Listener and properties for adding target group to the listener.
|
75 | */
|
76 | readonly listener: ListenerConfig;
|
77 | }
|
78 | /**
|
79 | * Interface for ECS load balancer target.
|
80 | */
|
81 | export interface IEcsLoadBalancerTarget extends elbv2.IApplicationLoadBalancerTarget, elbv2.INetworkLoadBalancerTarget, elb.ILoadBalancerTarget {
|
82 | }
|
83 | /**
|
84 | * The properties for the base Ec2Service or FargateService service.
|
85 | */
|
86 | export interface BaseServiceOptions {
|
87 | /**
|
88 | * The name of the cluster that hosts the service.
|
89 | */
|
90 | readonly cluster: ICluster;
|
91 | /**
|
92 | * The desired number of instantiations of the task definition to keep running on the service.
|
93 | *
|
94 | * @default - When creating the service, default is 1; when updating the service, default uses
|
95 | * the current task number.
|
96 | */
|
97 | readonly desiredCount?: number;
|
98 | /**
|
99 | * The name of the service.
|
100 | *
|
101 | * @default - CloudFormation-generated name.
|
102 | */
|
103 | readonly serviceName?: string;
|
104 | /**
|
105 | * The maximum number of tasks, specified as a percentage of the Amazon ECS
|
106 | * service's DesiredCount value, that can run in a service during a
|
107 | * deployment.
|
108 | *
|
109 | * @default - 100 if daemon, otherwise 200
|
110 | */
|
111 | readonly maxHealthyPercent?: number;
|
112 | /**
|
113 | * The minimum number of tasks, specified as a percentage of
|
114 | * the Amazon ECS service's DesiredCount value, that must
|
115 | * continue to run and remain healthy during a deployment.
|
116 | *
|
117 | * @default - 0 if daemon, otherwise 50
|
118 | */
|
119 | readonly minHealthyPercent?: number;
|
120 | /**
|
121 | * The period of time, in seconds, that the Amazon ECS service scheduler ignores unhealthy
|
122 | * Elastic Load Balancing target health checks after a task has first started.
|
123 | *
|
124 | * @default - defaults to 60 seconds if at least one load balancer is in-use and it is not already set
|
125 | */
|
126 | readonly healthCheckGracePeriod?: Duration;
|
127 | /**
|
128 | * The options for configuring an Amazon ECS service to use service discovery.
|
129 | *
|
130 | * @default - AWS Cloud Map service discovery is not enabled.
|
131 | */
|
132 | readonly cloudMapOptions?: CloudMapOptions;
|
133 | /**
|
134 | * Specifies whether to propagate the tags from the task definition or the service to the tasks in the service
|
135 | *
|
136 | * Valid values are: PropagatedTagSource.SERVICE, PropagatedTagSource.TASK_DEFINITION or PropagatedTagSource.NONE
|
137 | *
|
138 | * @default PropagatedTagSource.NONE
|
139 | */
|
140 | readonly propagateTags?: PropagatedTagSource;
|
141 | /**
|
142 | * Specifies whether to propagate the tags from the task definition or the service to the tasks in the service.
|
143 | * Tags can only be propagated to the tasks within the service during service creation.
|
144 | *
|
145 | * @deprecated Use `propagateTags` instead.
|
146 | * @default PropagatedTagSource.NONE
|
147 | */
|
148 | readonly propagateTaskTagsFrom?: PropagatedTagSource;
|
149 | /**
|
150 | * Specifies whether to enable Amazon ECS managed tags for the tasks within the service. For more information, see
|
151 | * [Tagging Your Amazon ECS Resources](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-using-tags.html)
|
152 | *
|
153 | * @default false
|
154 | */
|
155 | readonly enableECSManagedTags?: boolean;
|
156 | /**
|
157 | * Specifies which deployment controller to use for the service. For more information, see
|
158 | * [Amazon ECS Deployment Types](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/deployment-types.html)
|
159 | *
|
160 | * @default - Rolling update (ECS)
|
161 | */
|
162 | readonly deploymentController?: DeploymentController;
|
163 | /**
|
164 | * Whether to enable the deployment circuit breaker. If this property is defined, circuit breaker will be implicitly
|
165 | * enabled.
|
166 | * @default - disabled
|
167 | */
|
168 | readonly circuitBreaker?: DeploymentCircuitBreaker;
|
169 | /**
|
170 | * A list of Capacity Provider strategies used to place a service.
|
171 | *
|
172 | * @default - undefined
|
173 | *
|
174 | */
|
175 | readonly capacityProviderStrategies?: CapacityProviderStrategy[];
|
176 | /**
|
177 | * Whether to enable the ability to execute into a container
|
178 | *
|
179 | * @default - undefined
|
180 | */
|
181 | readonly enableExecuteCommand?: boolean;
|
182 | }
|
183 | /**
|
184 | * Complete base service properties that are required to be supplied by the implementation
|
185 | * of the BaseService class.
|
186 | */
|
187 | export interface BaseServiceProps extends BaseServiceOptions {
|
188 | /**
|
189 | * The launch type on which to run your service.
|
190 | *
|
191 | * LaunchType will be omitted if capacity provider strategies are specified on the service.
|
192 | *
|
193 | * @see - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html#cfn-ecs-service-capacityproviderstrategy
|
194 | *
|
195 | * Valid values are: LaunchType.ECS or LaunchType.FARGATE or LaunchType.EXTERNAL
|
196 | */
|
197 | readonly launchType: LaunchType;
|
198 | }
|
199 | /**
|
200 | * Base class for configuring listener when registering targets.
|
201 | */
|
202 | export declare abstract class ListenerConfig {
|
203 | /**
|
204 | * Create a config for adding target group to ALB listener.
|
205 | */
|
206 | static applicationListener(listener: elbv2.ApplicationListener, props?: elbv2.AddApplicationTargetsProps): ListenerConfig;
|
207 | /**
|
208 | * Create a config for adding target group to NLB listener.
|
209 | */
|
210 | static networkListener(listener: elbv2.NetworkListener, props?: elbv2.AddNetworkTargetsProps): ListenerConfig;
|
211 | /**
|
212 | * Create and attach a target group to listener.
|
213 | */
|
214 | abstract addTargets(id: string, target: LoadBalancerTargetOptions, service: BaseService): void;
|
215 | }
|
216 | /**
|
217 | * The interface for BaseService.
|
218 | */
|
219 | export interface IBaseService extends IService {
|
220 | /**
|
221 | * The cluster that hosts the service.
|
222 | */
|
223 | readonly cluster: ICluster;
|
224 | }
|
225 | /**
|
226 | * The base class for Ec2Service and FargateService services.
|
227 | */
|
228 | export declare abstract class BaseService extends Resource implements IBaseService, elbv2.IApplicationLoadBalancerTarget, elbv2.INetworkLoadBalancerTarget, elb.ILoadBalancerTarget {
|
229 | /**
|
230 | * Import an existing ECS/Fargate Service using the service cluster format.
|
231 | * The format is the "new" format "arn:aws:ecs:region:aws_account_id:service/cluster-name/service-name".
|
232 | * @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#ecs-resource-ids
|
233 | */
|
234 | static fromServiceArnWithCluster(scope: Construct, id: string, serviceArn: string): IBaseService;
|
235 | /**
|
236 | * The security groups which manage the allowed network traffic for the service.
|
237 | */
|
238 | readonly connections: ec2.Connections;
|
239 | /**
|
240 | * The Amazon Resource Name (ARN) of the service.
|
241 | */
|
242 | readonly serviceArn: string;
|
243 | /**
|
244 | * The name of the service.
|
245 | *
|
246 | * @attribute
|
247 | */
|
248 | readonly serviceName: string;
|
249 | /**
|
250 | * The task definition to use for tasks in the service.
|
251 | */
|
252 | readonly taskDefinition: TaskDefinition;
|
253 | /**
|
254 | * The cluster that hosts the service.
|
255 | */
|
256 | readonly cluster: ICluster;
|
257 | /**
|
258 | * The details of the AWS Cloud Map service.
|
259 | */
|
260 | protected cloudmapService?: cloudmap.Service;
|
261 | /**
|
262 | * A list of Elastic Load Balancing load balancer objects, containing the load balancer name, the container
|
263 | * name (as it appears in a container definition), and the container port to access from the load balancer.
|
264 | */
|
265 | protected loadBalancers: CfnService.LoadBalancerProperty[];
|
266 | /**
|
267 | * A list of Elastic Load Balancing load balancer objects, containing the load balancer name, the container
|
268 | * name (as it appears in a container definition), and the container port to access from the load balancer.
|
269 | */
|
270 | protected networkConfiguration?: CfnService.NetworkConfigurationProperty;
|
271 | /**
|
272 | * The details of the service discovery registries to assign to this service.
|
273 | * For more information, see Service Discovery.
|
274 | */
|
275 | protected serviceRegistries: CfnService.ServiceRegistryProperty[];
|
276 | private readonly resource;
|
277 | private scalableTaskCount?;
|
278 | /**
|
279 | * Constructs a new instance of the BaseService class.
|
280 | */
|
281 | constructor(scope: Construct, id: string, props: BaseServiceProps, additionalProps: any, taskDefinition: TaskDefinition);
|
282 | /**
|
283 | * The CloudMap service created for this service, if any.
|
284 | */
|
285 | get cloudMapService(): cloudmap.IService | undefined;
|
286 | private executeCommandLogConfiguration;
|
287 | private enableExecuteCommandEncryption;
|
288 | /**
|
289 | * This method is called to attach this service to an Application Load Balancer.
|
290 | *
|
291 | * Don't call this function directly. Instead, call `listener.addTargets()`
|
292 | * to add this service to a load balancer.
|
293 | */
|
294 | attachToApplicationTargetGroup(targetGroup: elbv2.IApplicationTargetGroup): elbv2.LoadBalancerTargetProps;
|
295 | /**
|
296 | * Registers the service as a target of a Classic Load Balancer (CLB).
|
297 | *
|
298 | * Don't call this. Call `loadBalancer.addTarget()` instead.
|
299 | */
|
300 | attachToClassicLB(loadBalancer: elb.LoadBalancer): void;
|
301 | /**
|
302 | * Return a load balancing target for a specific container and port.
|
303 | *
|
304 | * Use this function to create a load balancer target if you want to load balance to
|
305 | * another container than the first essential container or the first mapped port on
|
306 | * the container.
|
307 | *
|
308 | * Use the return value of this function where you would normally use a load balancer
|
309 | * target, instead of the `Service` object itself.
|
310 | *
|
311 | * @example
|
312 | *
|
313 | * declare const listener: elbv2.ApplicationListener;
|
314 | * declare const service: ecs.BaseService;
|
315 | * listener.addTargets('ECS', {
|
316 | * port: 80,
|
317 | * targets: [service.loadBalancerTarget({
|
318 | * containerName: 'MyContainer',
|
319 | * containerPort: 1234,
|
320 | * })],
|
321 | * });
|
322 | */
|
323 | loadBalancerTarget(options: LoadBalancerTargetOptions): IEcsLoadBalancerTarget;
|
324 | /**
|
325 | * Use this function to create all load balancer targets to be registered in this service, add them to
|
326 | * target groups, and attach target groups to listeners accordingly.
|
327 | *
|
328 | * Alternatively, you can use `listener.addTargets()` to create targets and add them to target groups.
|
329 | *
|
330 | * @example
|
331 | *
|
332 | * declare const listener: elbv2.ApplicationListener;
|
333 | * declare const service: ecs.BaseService;
|
334 | * service.registerLoadBalancerTargets(
|
335 | * {
|
336 | * containerName: 'web',
|
337 | * containerPort: 80,
|
338 | * newTargetGroupId: 'ECS',
|
339 | * listener: ecs.ListenerConfig.applicationListener(listener, {
|
340 | * protocol: elbv2.ApplicationProtocol.HTTPS
|
341 | * }),
|
342 | * },
|
343 | * )
|
344 | */
|
345 | registerLoadBalancerTargets(...targets: EcsTarget[]): void;
|
346 | /**
|
347 | * This method is called to attach this service to a Network Load Balancer.
|
348 | *
|
349 | * Don't call this function directly. Instead, call `listener.addTargets()`
|
350 | * to add this service to a load balancer.
|
351 | */
|
352 | attachToNetworkTargetGroup(targetGroup: elbv2.INetworkTargetGroup): elbv2.LoadBalancerTargetProps;
|
353 | /**
|
354 | * An attribute representing the minimum and maximum task count for an AutoScalingGroup.
|
355 | */
|
356 | autoScaleTaskCount(props: appscaling.EnableScalingProps): ScalableTaskCount;
|
357 | /**
|
358 | * Enable CloudMap service discovery for the service
|
359 | *
|
360 | * @returns The created CloudMap service
|
361 | */
|
362 | enableCloudMap(options: CloudMapOptions): cloudmap.Service;
|
363 | /**
|
364 | * Associates this service with a CloudMap service
|
365 | */
|
366 | associateCloudMapService(options: AssociateCloudMapServiceOptions): void;
|
367 | /**
|
368 | * This method returns the specified CloudWatch metric name for this service.
|
369 | */
|
370 | metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
|
371 | /**
|
372 | * This method returns the CloudWatch metric for this service's memory utilization.
|
373 | *
|
374 | * @default average over 5 minutes
|
375 | */
|
376 | metricMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
|
377 | /**
|
378 | * This method returns the CloudWatch metric for this service's CPU utilization.
|
379 | *
|
380 | * @default average over 5 minutes
|
381 | */
|
382 | metricCpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
|
383 | /**
|
384 | * This method is called to create a networkConfiguration.
|
385 | * @deprecated use configureAwsVpcNetworkingWithSecurityGroups instead.
|
386 | */
|
387 | protected configureAwsVpcNetworking(vpc: ec2.IVpc, assignPublicIp?: boolean, vpcSubnets?: ec2.SubnetSelection, securityGroup?: ec2.ISecurityGroup): void;
|
388 | /**
|
389 | * This method is called to create a networkConfiguration.
|
390 | */
|
391 | protected configureAwsVpcNetworkingWithSecurityGroups(vpc: ec2.IVpc, assignPublicIp?: boolean, vpcSubnets?: ec2.SubnetSelection, securityGroups?: ec2.ISecurityGroup[]): void;
|
392 | private renderServiceRegistry;
|
393 | /**
|
394 | * Shared logic for attaching to an ELB
|
395 | */
|
396 | private attachToELB;
|
397 | /**
|
398 | * Shared logic for attaching to an ELBv2
|
399 | */
|
400 | private attachToELBv2;
|
401 | private get defaultLoadBalancerTarget();
|
402 | /**
|
403 | * Generate the role that will be used for autoscaling this service
|
404 | */
|
405 | private makeAutoScalingRole;
|
406 | /**
|
407 | * Associate Service Discovery (Cloud Map) service
|
408 | */
|
409 | private addServiceRegistry;
|
410 | /**
|
411 | * Return the default grace period when load balancers are configured and
|
412 | * healthCheckGracePeriod is not already set
|
413 | */
|
414 | private evaluateHealthGracePeriod;
|
415 | private enableExecuteCommand;
|
416 | }
|
417 | /**
|
418 | * The options to enabling AWS Cloud Map for an Amazon ECS service.
|
419 | */
|
420 | export interface CloudMapOptions {
|
421 | /**
|
422 | * The name of the Cloud Map service to attach to the ECS service.
|
423 | *
|
424 | * @default CloudFormation-generated name
|
425 | */
|
426 | readonly name?: string;
|
427 | /**
|
428 | * The service discovery namespace for the Cloud Map service to attach to the ECS service.
|
429 | *
|
430 | * @default - the defaultCloudMapNamespace associated to the cluster
|
431 | */
|
432 | readonly cloudMapNamespace?: cloudmap.INamespace;
|
433 | /**
|
434 | * The DNS record type that you want AWS Cloud Map to create. The supported record types are A or SRV.
|
435 | *
|
436 | * @default - DnsRecordType.A if TaskDefinition.networkMode = AWS_VPC, otherwise DnsRecordType.SRV
|
437 | */
|
438 | readonly dnsRecordType?: cloudmap.DnsRecordType.A | cloudmap.DnsRecordType.SRV;
|
439 | /**
|
440 | * The amount of time that you want DNS resolvers to cache the settings for this record.
|
441 | *
|
442 | * @default Duration.minutes(1)
|
443 | */
|
444 | readonly dnsTtl?: Duration;
|
445 | /**
|
446 | * The number of 30-second intervals that you want Cloud Map to wait after receiving an UpdateInstanceCustomHealthStatus
|
447 | * request before it changes the health status of a service instance.
|
448 | *
|
449 | * NOTE: This is used for HealthCheckCustomConfig
|
450 | */
|
451 | readonly failureThreshold?: number;
|
452 | /**
|
453 | * The container to point to for a SRV record.
|
454 | * @default - the task definition's default container
|
455 | */
|
456 | readonly container?: ContainerDefinition;
|
457 | /**
|
458 | * The port to point to for a SRV record.
|
459 | * @default - the default port of the task definition's default container
|
460 | */
|
461 | readonly containerPort?: number;
|
462 | }
|
463 | /**
|
464 | * The options for using a cloudmap service.
|
465 | */
|
466 | export interface AssociateCloudMapServiceOptions {
|
467 | /**
|
468 | * The cloudmap service to register with.
|
469 | */
|
470 | readonly service: cloudmap.IService;
|
471 | /**
|
472 | * The container to point to for a SRV record.
|
473 | * @default - the task definition's default container
|
474 | */
|
475 | readonly container?: ContainerDefinition;
|
476 | /**
|
477 | * The port to point to for a SRV record.
|
478 | * @default - the default port of the task definition's default container
|
479 | */
|
480 | readonly containerPort?: number;
|
481 | }
|
482 | /**
|
483 | * The launch type of an ECS service
|
484 | */
|
485 | export declare enum LaunchType {
|
486 | /**
|
487 | * The service will be launched using the EC2 launch type
|
488 | */
|
489 | EC2 = "EC2",
|
490 | /**
|
491 | * The service will be launched using the FARGATE launch type
|
492 | */
|
493 | FARGATE = "FARGATE",
|
494 | /**
|
495 | * The service will be launched using the EXTERNAL launch type
|
496 | */
|
497 | EXTERNAL = "EXTERNAL"
|
498 | }
|
499 | /**
|
500 | * The deployment controller type to use for the service.
|
501 | */
|
502 | export declare enum DeploymentControllerType {
|
503 | /**
|
504 | * The rolling update (ECS) deployment type involves replacing the current
|
505 | * running version of the container with the latest version.
|
506 | */
|
507 | ECS = "ECS",
|
508 | /**
|
509 | * The blue/green (CODE_DEPLOY) deployment type uses the blue/green deployment model powered by AWS CodeDeploy
|
510 | */
|
511 | CODE_DEPLOY = "CODE_DEPLOY",
|
512 | /**
|
513 | * The external (EXTERNAL) deployment type enables you to use any third-party deployment controller
|
514 | */
|
515 | EXTERNAL = "EXTERNAL"
|
516 | }
|
517 | /**
|
518 | * Propagate tags from either service or task definition
|
519 | */
|
520 | export declare enum PropagatedTagSource {
|
521 | /**
|
522 | * Propagate tags from service
|
523 | */
|
524 | SERVICE = "SERVICE",
|
525 | /**
|
526 | * Propagate tags from task definition
|
527 | */
|
528 | TASK_DEFINITION = "TASK_DEFINITION",
|
529 | /**
|
530 | * Do not propagate
|
531 | */
|
532 | NONE = "NONE"
|
533 | }
|