forked from terra-mod/terraform-aws-ecs-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
356 lines (291 loc) · 9.31 KB
/
main.tf
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* Requires Terraform >= 0.12
*/
terraform {
required_version = ">= 0.12"
}
locals {
tags = merge(merge(var.tags, {
Environment = var.environment
ManagedBy = "terraform"
Cluster = var.cluster_name
Service = var.name
}), var.overwrite_tags)
}
/**
* Create a default Security Group - ingress and egress rules are attached separately to allow additional rules
* outside of this module to be attached to this security group if desired.
*/
resource aws_security_group security_group {
count = var.task_network_mode == "awsvpc" ? 1 : 0
description = "ECS Service security group for ${var.cluster_name} ${var.name}."
vpc_id = var.vpc_id
name = var.name
tags = local.tags
}
/**
* Creates an default egress rule.
*/
resource aws_security_group_rule egress {
count = var.task_network_mode == "awsvpc" && var.security_group_default_egress ? 1 : 0
security_group_id = aws_security_group.security_group[0].id
description = "Default egress - fully open."
type = "egress"
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
/**
* Creates the default ingress rule for the service for service discovery.
*/
resource aws_security_group_rule sds_ingress {
for_each = var.task_network_mode == "awsvpc" ? var.security_group_cidr_blocks : []
security_group_id = aws_security_group.security_group[0].id
description = "CIDR Block Ingress rule."
type = "ingress"
from_port = var.ingress_target_port
to_port = var.ingress_target_port
protocol = var.ingress_protocol
cidr_blocks = [each.value]
}
/**
* Creates the default ingress rule for the service for load balancing.
*/
resource aws_security_group_rule lb_ingress {
for_each = var.task_network_mode == "awsvpc" ? var.security_group_allowed_security_groups : []
security_group_id = aws_security_group.security_group[0].id
description = "Security Group Ingress rule."
type = "ingress"
from_port = var.ingress_target_port
to_port = var.ingress_target_port
protocol = var.ingress_protocol
source_security_group_id = each.value
}
/**
* The ECS task definition.
*/
resource aws_ecs_task_definition task {
family = var.name
task_role_arn = var.use_task_role ? aws_iam_role.ecs_task_role[0].arn : null
execution_role_arn = var.use_execution_role ? aws_iam_role.ecs_execution_role[0].arn : null
cpu = var.task_cpu
memory = var.task_memory
lifecycle {
create_before_destroy = true
}
network_mode = var.task_network_mode
requires_compatibilities = var.task_requires_compatibilities
container_definitions = var.task_definition
dynamic "volume" {
for_each = var.task_volumes
content {
name = volume.value["name"]
host_path = volume.value["host_path"]
}
}
tags = local.tags
}
/**
* Generate the actual ECS Service
*/
resource aws_ecs_service service {
name = var.name
cluster = var.cluster_name
launch_type = var.launch_type
scheduling_strategy = var.scheduling_strategy
platform_version = var.platform_version
task_definition = aws_ecs_task_definition.task.arn
desired_count = var.desired_count
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
deployment_maximum_percent = var.deployment_maximum_percent
dynamic "ordered_placement_strategy" {
for_each = var.launch_type == "FARGATE" || var.scheduling_strategy == "DAEMON" ? [] : [1]
content {
type = "spread"
field = "attribute:ecs.availability-zone"
}
}
# This is only used with Load Balancing
health_check_grace_period_seconds = length(var.load_balancer_target_groups) > 0 ? var.health_check_grace_period_seconds : null
# Only include a network configuration for `awsvpc` network mode
dynamic network_configuration {
for_each = var.task_network_mode == "awsvpc" ? [aws_security_group.security_group.*.id] : []
content {
security_groups = concat(network_configuration.value, var.service_security_groups)
subnets = var.networking_subnets
assign_public_ip = var.networking_assign_public_ip
}
}
# When Service Discovery is enabled
dynamic service_registries {
for_each = aws_service_discovery_service.sds
# Setting port is not supported whne using "host" or "bridge" network mode.
content {
registry_arn = service_registries.value.arn
port = var.task_network_mode == "awsvpc" ? var.service_discovery_container_port : null
container_name = var.service_discovery_container_name
container_port = var.service_discovery_container_port
}
}
# When Load Balancing is enabled
dynamic load_balancer {
for_each = var.load_balancer_target_groups
content {
container_name = var.ingress_target_container
container_port = var.ingress_target_port
target_group_arn = load_balancer.value
}
}
# AWS Accounts need to opt-in to allowing tagging of this resource, else this will
# cause the resource creation to fail.
tags = var.ecs_service_tagging_enabled ? local.tags : null
propagate_tags = "SERVICE"
}
/**
* Create a new Service Discovery Service.
*/
resource aws_service_discovery_service sds {
count = var.enable_service_discovery ? 1 : 0
name = var.name
dns_config {
namespace_id = var.service_discovery_namespace_id
dns_records {
ttl = var.service_discovery_dns_ttl
type = var.service_discovery_dns_record_type
}
routing_policy = var.service_discovery_routing_policy
}
health_check_custom_config {
failure_threshold = var.service_discovery_failure_threshold
}
tags = local.tags
}
/*
* When using Auto Scaling, we target the Desired Count on the service.
*/
resource aws_appautoscaling_target scaling_target {
count = var.enable_auto_scaling ? 1 : 0
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.service.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = var.scaling_min_capacity
max_capacity = var.scaling_max_capacity
}
/**
* Use a TargetTrackingScaling policy to scale based on the given metric (CPU or Memory).
*/
resource aws_appautoscaling_policy auto_scaling {
count = var.enable_auto_scaling ? 1 : 0
name = "${var.cluster_name}-${var.name}-auto-scaling"
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.service.name}"
target_tracking_scaling_policy_configuration {
target_value = var.scaling_threshold
scale_in_cooldown = var.scaling_cooldown
scale_out_cooldown = var.scaling_cooldown
predefined_metric_specification {
predefined_metric_type = var.scaling_metric
}
}
scalable_dimension = "ecs:service:DesiredCount"
policy_type = "TargetTrackingScaling"
depends_on = [aws_appautoscaling_target.scaling_target]
}
/**
* Generates the Role for the ECS Container
*/
resource aws_iam_role ecs_execution_role {
count = var.use_execution_role ? 1 : 0
name = "${var.cluster_name}-${var.name}-execution-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
tags = local.tags
}
/**
* Generates a default Policy for the ECS Container
*/
resource aws_iam_role_policy ecs_execution_default_policy {
count = var.use_execution_role ? 1 : 0
name = "${var.cluster_name}-${var.name}-default-policy"
role = aws_iam_role.ecs_execution_role[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage"
],
"Resource": "*"
},
%{ if var.cloudwatch_log_group_arn != null }
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "${replace(var.cloudwatch_log_group_arn, "/:\\*$/", "")}:*"
}
%{ endif }
]
}
EOF
}
/**
* Generates a Task Role for the ECS Task.
*/
resource aws_iam_role ecs_task_role {
count = var.use_task_role ? 1 : 0
name = "${var.cluster_name}-${var.name}-task-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
tags = local.tags
}
/**
* Attach a additional policies to the ECS Task Role.
*/
resource aws_iam_role_policy_attachment task_policy_attachments {
count = var.use_task_role ? length(var.task_role_policy_arns) : 0
role = aws_iam_role.ecs_task_role[0].id
policy_arn = element(var.task_role_policy_arns, count.index)
}
/**
* Attach a additional policies to the ECS Execution Role.
*/
resource aws_iam_role_policy_attachment execution_policy_attachments {
count = var.use_execution_role ? length(var.execution_role_policy_arns) : 0
role = aws_iam_role.ecs_execution_role[0].id
policy_arn = element(var.execution_role_policy_arns, count.index)
}