forked from umotif-public/terraform-aws-ecs-fargate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
389 lines (339 loc) · 12.5 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#####
# Cloudwatch
#####
resource "aws_cloudwatch_log_group" "main" {
name = var.name_prefix
retention_in_days = var.log_retention_in_days
kms_key_id = var.logs_kms_key
tags = var.tags
}
#####
# IAM - Task execution role, needed to pull ECR images etc.
#####
resource "aws_iam_role" "execution" {
name = "${var.name_prefix}-task-execution-role"
assume_role_policy = data.aws_iam_policy_document.task_assume.json
tags = var.tags
}
resource "aws_iam_role_policy" "task_execution" {
name = "${var.name_prefix}-task-execution"
role = aws_iam_role.execution.id
policy = data.aws_iam_policy_document.task_execution_permissions.json
}
resource "aws_iam_role_policy" "read_repository_credentials" {
count = var.create_repository_credentials_iam_policy ? 1 : 0
name = "${var.name_prefix}-read-repository-credentials"
role = aws_iam_role.execution.id
policy = data.aws_iam_policy_document.read_repository_credentials[0].json
}
resource "aws_iam_role_policy" "get_environment_files" {
count = length(var.task_container_environment_files) != 0 ? 1 : 0
name = "${var.name_prefix}-read-repository-credentials"
role = aws_iam_role.execution.id
policy = data.aws_iam_policy_document.get_environment_files[0].json
}
#####
# IAM - Task role, basic. Append policies to this role for S3, DynamoDB etc.
#####
resource "aws_iam_role" "task" {
name = "${var.name_prefix}-task-role"
assume_role_policy = data.aws_iam_policy_document.task_assume.json
tags = var.tags
}
resource "aws_iam_role_policy" "log_agent" {
name = "${var.name_prefix}-log-permissions"
role = aws_iam_role.task.id
policy = data.aws_iam_policy_document.task_permissions.json
}
resource "aws_iam_role_policy" "ecs_exec_inline_policy" {
count = var.enable_execute_command ? 1 : 0
name = "${var.name_prefix}-ecs-exec-permissions"
role = aws_iam_role.task.id
policy = data.aws_iam_policy_document.task_ecs_exec_policy[0].json
}
#####
# Security groups
#####
resource "aws_security_group" "ecs_service" {
vpc_id = var.vpc_id
name_prefix = var.sg_name_prefix == "" ? "${var.name_prefix}-ecs-service-sg-" : "${var.sg_name_prefix}-"
description = "Fargate service security group"
tags = merge(
var.tags,
{
Name = var.sg_name_prefix == "" ? "${var.name_prefix}-ecs-service-sg" : var.sg_name_prefix
},
)
revoke_rules_on_delete = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "egress_service" {
security_group_id = aws_security_group.ecs_service.id
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
#####
# Load Balancer Target group
#####
resource "aws_lb_target_group" "task" {
for_each = var.load_balanced ? { for tg in var.target_groups : tg.target_group_name => tg } : {}
name = lookup(each.value, "target_group_name")
vpc_id = var.vpc_id
protocol = var.task_container_protocol
port = lookup(each.value, "container_port", var.task_container_port)
deregistration_delay = lookup(each.value, "deregistration_delay", null)
target_type = "ip"
dynamic "health_check" {
for_each = [var.health_check]
content {
enabled = lookup(health_check.value, "enabled", null)
interval = lookup(health_check.value, "interval", null)
path = lookup(health_check.value, "path", null)
port = lookup(health_check.value, "port", null)
protocol = lookup(health_check.value, "protocol", null)
timeout = lookup(health_check.value, "timeout", null)
healthy_threshold = lookup(health_check.value, "healthy_threshold", null)
unhealthy_threshold = lookup(health_check.value, "unhealthy_threshold", null)
matcher = lookup(health_check.value, "matcher", null)
}
}
lifecycle {
create_before_destroy = true
}
tags = merge(
var.tags,
{
Name = lookup(each.value, "target_group_name")
},
)
}
#####
# ECS Task/Service
#####
locals {
task_environment = [
for k, v in var.task_container_environment : {
name = k
value = v
}
]
target_group_portMaps = length(var.target_groups) > 0 ? distinct([
for tg in var.target_groups : {
containerPort = contains(keys(tg), "container_port") ? tg.container_port : var.task_container_port
protocol = contains(keys(tg), "protocol") ? lower(tg.protocol) : "tcp"
}
]) : []
task_environment_files = [
for file in var.task_container_environment_files : {
value = file
type = "s3"
}
]
}
resource "aws_ecs_task_definition" "task" {
family = var.name_prefix
execution_role_arn = aws_iam_role.execution.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.task_definition_cpu
memory = var.task_definition_memory
task_role_arn = aws_iam_role.task.arn
dynamic "ephemeral_storage" {
for_each = var.task_definition_ephemeral_storage == 0 ? [] : [var.task_definition_ephemeral_storage]
content {
size_in_gib = var.task_definition_ephemeral_storage
}
}
container_definitions = <<EOF
[{
"name": "${var.container_name != "" ? var.container_name : var.name_prefix}",
"image": "${var.task_container_image}",
%{if var.repository_credentials != ""~}
"repositoryCredentials": {
"credentialsParameter": "${var.repository_credentials}"
},
%{~endif}
"essential": true,
%{if length(local.target_group_portMaps) > 0}
"portMappings": ${jsonencode(local.target_group_portMaps)},
%{else}
%{if var.task_container_port != 0 || var.task_host_port != 0~}
"portMappings": [
{
%{if var.task_host_port != 0~}
"hostPort": ${var.task_host_port},
%{~endif}
%{if var.task_container_port != 0~}
"containerPort": ${var.task_container_port},
%{~endif}
"protocol":"tcp"
}
],
%{~endif}
%{~endif}
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${aws_cloudwatch_log_group.main.name}",
"awslogs-region": "${data.aws_region.current.name}",
"awslogs-stream-prefix": "container"
}
},
%{if var.task_health_check != null || var.task_health_command != null~}
"healthcheck": {
"command": ${jsonencode(var.task_health_command)},
"interval": ${lookup(var.task_health_check, "interval", 30)},
"timeout": ${lookup(var.task_health_check, "timeout", 5)},
"retries": ${lookup(var.task_health_check, "retries", 3)},
"startPeriod": ${lookup(var.task_health_check, "startPeriod", 0)}
},
%{~endif}
"command": ${jsonencode(var.task_container_command)},
%{if var.task_container_working_directory != ""~}
"workingDirectory": ${var.task_container_working_directory},
%{~endif}
%{if var.task_container_memory != null~}
"memory": ${var.task_container_memory},
%{~endif}
%{if var.task_container_memory_reservation != null~}
"memoryReservation": ${var.task_container_memory_reservation},
%{~endif}
%{if var.task_container_cpu != null~}
"cpu": ${var.task_container_cpu},
%{~endif}
%{if var.task_start_timeout != null~}
"startTimeout": ${var.task_start_timeout},
%{~endif}
%{if var.task_stop_timeout != null~}
"stopTimeout": ${var.task_stop_timeout},
%{~endif}
%{if var.task_mount_points != null~}
"mountPoints": ${jsonencode(var.task_mount_points)},
%{~endif}
%{if var.task_container_secrets != null~}
"secrets": ${jsonencode(var.task_container_secrets)},
%{~endif}
%{if var.task_pseudo_terminal != null~}
"pseudoTerminal": ${var.task_pseudo_terminal},
%{~endif}
"environment": ${jsonencode(local.task_environment)},
"environmentFiles": ${jsonencode(local.task_environment_files)}
}]
EOF
runtime_platform {
operating_system_family = var.operating_system_family
cpu_architecture = var.cpu_architecture
}
dynamic "placement_constraints" {
for_each = var.placement_constraints
content {
expression = lookup(placement_constraints.value, "expression", null)
type = placement_constraints.value.type
}
}
dynamic "proxy_configuration" {
for_each = var.proxy_configuration
content {
container_name = proxy_configuration.value.container_name
properties = lookup(proxy_configuration.value, "properties", null)
type = lookup(proxy_configuration.value, "type", null)
}
}
dynamic "volume" {
for_each = var.volume
content {
name = volume.value.name
host_path = lookup(volume.value, "host_path", null)
dynamic "docker_volume_configuration" {
for_each = lookup(volume.value, "docker_volume_configuration", [])
content {
scope = lookup(docker_volume_configuration.value, "scope", null)
autoprovision = lookup(docker_volume_configuration.value, "autoprovision", null)
driver = lookup(docker_volume_configuration.value, "driver", null)
driver_opts = lookup(docker_volume_configuration.value, "driver_opts", null)
labels = lookup(docker_volume_configuration.value, "labels", null)
}
}
dynamic "efs_volume_configuration" {
for_each = lookup(volume.value, "efs_volume_configuration", [])
content {
file_system_id = lookup(efs_volume_configuration.value, "file_system_id", null)
root_directory = lookup(efs_volume_configuration.value, "root_directory", null)
transit_encryption = lookup(efs_volume_configuration.value, "transit_encryption", null)
transit_encryption_port = lookup(efs_volume_configuration.value, "transit_encryption_port", null)
dynamic "authorization_config" {
for_each = length(lookup(efs_volume_configuration.value, "authorization_config", {})) == 0 ? [] : [lookup(efs_volume_configuration.value, "authorization_config", {})]
content {
access_point_id = lookup(authorization_config.value, "access_point_id", null)
iam = lookup(authorization_config.value, "iam", null)
}
}
}
}
}
}
tags = merge(
var.tags,
{
Name = var.container_name != "" ? var.container_name : var.name_prefix
},
)
}
resource "aws_ecs_service" "service" {
name = var.name_prefix
cluster = var.cluster_id
task_definition = "${aws_ecs_task_definition.task.family}:${max(aws_ecs_task_definition.task.revision, data.aws_ecs_task_definition.task.revision)}"
desired_count = var.desired_count
propagate_tags = var.propogate_tags
platform_version = var.platform_version
launch_type = length(var.capacity_provider_strategy) == 0 ? "FARGATE" : null
force_new_deployment = var.force_new_deployment
wait_for_steady_state = var.wait_for_steady_state
enable_execute_command = var.enable_execute_command
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
deployment_maximum_percent = var.deployment_maximum_percent
health_check_grace_period_seconds = var.load_balanced ? var.health_check_grace_period_seconds : null
network_configuration {
subnets = var.private_subnet_ids
security_groups = [aws_security_group.ecs_service.id]
assign_public_ip = var.task_container_assign_public_ip
}
dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategy
content {
capacity_provider = capacity_provider_strategy.value.capacity_provider
weight = capacity_provider_strategy.value.weight
base = lookup(capacity_provider_strategy.value, "base", null)
}
}
dynamic "load_balancer" {
for_each = var.load_balanced ? var.target_groups : []
content {
container_name = var.container_name != "" ? var.container_name : var.name_prefix
container_port = lookup(load_balancer.value, "container_port", var.task_container_port)
target_group_arn = aws_lb_target_group.task[lookup(load_balancer.value, "target_group_name")].arn
}
}
deployment_controller {
type = var.deployment_controller_type # CODE_DEPLOY or ECS or EXTERNAL
}
dynamic "service_registries" {
for_each = var.service_registry_arn == "" ? [] : [1]
content {
registry_arn = var.service_registry_arn
container_name = var.container_name != "" ? var.container_name : var.name_prefix
}
}
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-service"
},
)
}