-
Notifications
You must be signed in to change notification settings - Fork 34
/
task.tf
85 lines (69 loc) · 3.53 KB
/
task.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
# A task definition is created if the user does not specify a task
# definition arn. The task definition map is used to customize the
# task definition created.
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html
# The cpu and memory values define the TOTAL cpu and memory resources
# reserved for all the containers defined in the container definition
# file; individual container cpu and memory sizes are defined in
# the task's container definition file. These values must not exceed
# the TOTAL set here.
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size
# Compute container definition file name, then determine whether it's
# a template or simply a json file.
locals {
container_definition_file = var.task_definition.template_variables != null ? (var.task_definition.container_definition_file != null ? var.task_definition.container_definition_file : "containers.json.tftpl") : (var.task_definition.container_definition_file != null ? var.task_definition.container_definition_file : "containers.json")
container_definitions = endswith(local.container_definition_file, ".tftpl") ? try(templatefile(local.container_definition_file, var.task_definition.template_variables), false) : file(local.container_definition_file)
}
# The task_definition_arn local variable contains the ARN specified by caller, or the
# ARN generated by the aws_ecs.task_definition resource.
locals {
task_definition_arn = var.task_definition_arn != null ? var.task_definition_arn : aws_ecs_task_definition.default[var.launch_type].arn
}
# Smooth transition for infrastructure created by old module versions.
moved {
from = aws_ecs_task_definition.ec2[0]
to = aws_ecs_task_definition.default["EC2"]
}
moved {
from = aws_ecs_task_definition.fargate[0]
to = aws_ecs_task_definition.default["FARGATE"]
}
resource "aws_ecs_task_definition" "default" {
for_each = toset(var.task_definition_arn == null ? [var.launch_type] : [])
family = var.name
container_definitions = local.container_definitions
task_role_arn = var.task_definition.task_role_arn
execution_role_arn = format(
"arn:aws:iam::%s:role/ecsTaskExecutionRole",
data.aws_caller_identity.current.account_id,
)
network_mode = var.task_definition.network_mode
dynamic "volume" {
for_each = var.volume
content {
host_path = volume.value.host_path
name = volume.value.name
dynamic "docker_volume_configuration" {
for_each = volume.value.docker_volume_configuration != null ? [volume.value.docker_volume_configuration] : []
content {
autoprovision = docker_volume_configuration.value.autoprovision
driver = docker_volume_configuration.value.driver
driver_opts = docker_volume_configuration.value.driver_opts
labels = docker_volume_configuration.value.labels
scope = docker_volume_configuration.value.scope
}
}
dynamic "efs_volume_configuration" {
for_each = volume.value.efs_volume_configuration != null ? [volume.value.efs_volume_configuration] : []
content {
file_system_id = efs_volume_configuration.value.file_system_id
root_directory = efs_volume_configuration.value.root_directory
}
}
}
}
cpu = var.task_definition.cpu
memory = var.task_definition.memory
requires_compatibilities = var.launch_type == "FARGATE" ? ["FARGATE"] : ["EC2"]
tags = local.tags
}