-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
fargate-service.ts
158 lines (137 loc) · 4.75 KB
/
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
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
import * as ec2 from '@aws-cdk/aws-ec2';
import * as cdk from '@aws-cdk/core';
import { BaseService, BaseServiceOptions, IService, LaunchType, PropagatedTagSource } from '../base/base-service';
import { TaskDefinition } from '../base/task-definition';
/**
* The properties for defining a service using the Fargate launch type.
*/
export interface FargateServiceProps extends BaseServiceOptions {
/**
* The task definition to use for tasks in the service.
*
* [disable-awslint:ref-via-interface]
*/
readonly taskDefinition: TaskDefinition;
/**
* Specifies whether the task's elastic network interface receives a public IP address.
*
* If true, each task will receive a public IP address.
*
* @default - Use subnet default.
*/
readonly assignPublicIp?: boolean;
/**
* The subnets to associate with the service.
*
* @default - Private subnets.
*/
readonly vpcSubnets?: ec2.SubnetSelection;
/**
* The security groups to associate with the service. If you do not specify a security group, the default security group for the VPC is used.
*
* @default - A new security group is created.
*/
readonly securityGroup?: ec2.ISecurityGroup;
/**
* 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;
/**
* Specifies whether to propagate the tags from the task definition or the service to the tasks in the service.
* Tags can only be propagated to the tasks within the service during service creation.
*
* @deprecated Use `propagateTags` instead.
* @default PropagatedTagSource.NONE
*/
readonly propagateTaskTagsFrom?: PropagatedTagSource;
}
/**
* The interface for a service using the Fargate launch type on an ECS cluster.
*/
export interface IFargateService extends IService {
}
/**
* This creates a service using the Fargate launch type on an ECS cluster.
*
* @resource AWS::ECS::Service
*/
export class FargateService extends BaseService implements IFargateService {
/**
* Import a task definition from the specified task definition ARN.
*/
public static fromFargateServiceArn(scope: cdk.Construct, id: string, fargateServiceArn: string): IFargateService {
class Import extends cdk.Resource implements IFargateService {
public readonly serviceArn = fargateServiceArn;
}
return new Import(scope, id);
}
/**
* Constructs a new instance of the FargateService class.
*/
constructor(scope: cdk.Construct, id: string, props: FargateServiceProps) {
if (!props.taskDefinition.isFargateCompatible) {
throw new Error('Supplied TaskDefinition is not configured for compatibility with Fargate');
}
if (props.propagateTags && props.propagateTaskTagsFrom) {
throw new Error('You can only specify either propagateTags or propagateTaskTagsFrom. Alternatively, you can leave both blank');
}
const propagateTagsFromSource = props.propagateTaskTagsFrom !== undefined ? props.propagateTaskTagsFrom
: (props.propagateTags !== undefined ? props.propagateTags : PropagatedTagSource.NONE);
super(scope, id, {
...props,
desiredCount: props.desiredCount !== undefined ? props.desiredCount : 1,
launchType: LaunchType.FARGATE,
propagateTags: propagateTagsFromSource,
enableECSManagedTags: props.enableECSManagedTags,
}, {
cluster: props.cluster.clusterName,
taskDefinition: props.taskDefinition.taskDefinitionArn,
platformVersion: props.platformVersion,
}, props.taskDefinition);
this.configureAwsVpcNetworking(props.cluster.vpc, props.assignPublicIp, props.vpcSubnets, props.securityGroup);
if (!props.taskDefinition.defaultContainer) {
throw new Error('A TaskDefinition must have at least one essential container');
}
}
}
/**
* The platform version on which to run your service.
*
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html
*/
export enum FargatePlatformVersion {
/**
* The latest, recommended platform version
*/
LATEST = 'LATEST',
/**
* Version 1.3.0
*
* Supports secrets, task recycling.
*/
VERSION1_3 = '1.3.0',
/**
* Version 1.2.0
*
* Supports private registries.
*/
VERSION1_2 = '1.2.0',
/**
* Version 1.1.0
*
* Supports task metadata, health checks, service discovery.
*/
VERSION1_1 = '1.1.0',
/**
* Initial release
*
* Based on Amazon Linux 2017.09.
*/
VERSION1_0 = '1.0.0',
}