-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmain.tf
More file actions
184 lines (167 loc) · 7.1 KB
/
main.tf
File metadata and controls
184 lines (167 loc) · 7.1 KB
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
# The ternary operators in this module are mandatory. All composite types (e.g., lists and maps) require encoding to
# pass as arguments to the Terraform `template_file`[1] data source The `locals.tf` file contains the encoded values of
# the composite types defined in the ECS Task Definition[2]. Certain variables, such as `healthCheck`, `linuxParameters`
# and `portMappings`, require the `replace` function since they contain numerical values. For example, given the
# following JSON:
#
# -var 'portMappings=[{containerPort = 80, protocol = "TCP"}]'
#
# [
# {
# "containerDefinitions": [
# {
# "portMappings": [
# {
# "containerPort": 80,
# "protocol": "TCP"
# }
# ]
# }
# ]
# }
# ]
#
# Since the `containerPort` and `hostPort`[3] fields are both integer types, then the `replace` function is necessary to
# prevent quoting the value in strings. This issue is a known limitation in the Terraform `jsonencode`[4] function.
#
# - 1. https://www.terraform.io/docs/providers/template/d/file.html
# - 2. https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerDefinition.html
# - 3. https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_PortMapping.html
# - 4. https://github.com/hashicorp/terraform/issues/17033
locals {
command = "${jsonencode(var.command)}"
dnsSearchDomains = "${jsonencode(var.dnsSearchDomains)}"
dnsServers = "${jsonencode(var.dnsServers)}"
dockerLabels = "${jsonencode(var.dockerLabels)}"
dockerSecurityOptions = "${jsonencode(var.dockerSecurityOptions)}"
entryPoint = "${jsonencode(var.entryPoint)}"
environment = "${jsonencode(var.environment)}"
extraHosts = "${jsonencode(var.extraHosts)}"
healthCheck = "${
replace(
jsonencode(var.healthCheck),
local.classes["digit"],
"$1"
)
}"
links = "${jsonencode(var.links)}"
linuxParameters = "${
replace(
replace(
replace(
jsonencode(var.linuxParameters),
"/\"1\"/",
"true"
),
"/\"0\"/",
"false"
),
local.classes["digit"],
"$1"
)
}"
logConfiguration = "${jsonencode(var.logConfiguration)}"
mountPoints = "${
replace(
replace(
jsonencode(var.mountPoints),
"/\"1\"/",
"true"
),
"/\"0\"/",
"false"
)
}"
portMappings = "${
replace(
jsonencode(var.portMappings),
local.classes["digit"],
"$1"
)
}"
repositoryCredentials = "${jsonencode(var.repositoryCredentials)}"
resourceRequirements = "${jsonencode(var.resourceRequirements)}"
secrets = "${jsonencode(var.secrets)}"
systemControls = "${jsonencode(var.systemControls)}"
ulimits = "${
replace(
jsonencode(var.ulimits),
local.classes["digit"],
"$1"
)
}"
volumesFrom = "${
replace(
replace(
jsonencode(var.volumesFrom),
"/\"1\"/",
"true"
),
"/\"0\"/",
"false"
)
}"
# re2 ASCII character classes
# https://github.com/google/re2/wiki/Syntax
classes = {
digit = "/\"(-[[:digit:]]|[[:digit:]]+)\"/"
}
container_definition = "${
var.register_task_definition ?
format("[%s]", data.template_file.container_definition.rendered) :
format("%s", data.template_file.container_definition.rendered)
}"
container_definitions = "${replace(local.container_definition, "/\"(null)\"/", "$1")}"
}
data "template_file" "container_definition" {
template = "${file("${path.module}/templates/container-definition.json.tpl")}"
vars = {
command = "${local.command == "[]" ? "null" : local.command}"
cpu = "${var.cpu == 0 ? "null" : var.cpu}"
disableNetworking = "${var.disableNetworking ? true : false}"
dnsSearchDomains = "${local.dnsSearchDomains == "[]" ? "null" : local.dnsSearchDomains}"
dnsServers = "${local.dnsServers == "[]" ? "null" : local.dnsServers}"
dockerLabels = "${local.dockerLabels == "{}" ? "null" : local.dockerLabels}"
dockerSecurityOptions = "${local.dockerSecurityOptions == "[]" ? "null" : local.dockerSecurityOptions}"
entryPoint = "${local.entryPoint == "[]" ? "null" : local.entryPoint}"
environment = "${local.environment == "[]" ? "null" : local.environment}"
essential = "${var.essential ? true : false}"
extraHosts = "${local.extraHosts == "[]" ? "null" : local.extraHosts}"
healthCheck = "${local.healthCheck == "{}" ? "null" : local.healthCheck}"
hostname = "${var.hostname == "" ? "null" : var.hostname}"
image = "${var.image == "" ? "null" : var.image}"
interactive = "${var.interactive ? true : false}"
links = "${local.links == "[]" ? "null" : local.links}"
linuxParameters = "${local.linuxParameters == "{}" ? "null" : local.linuxParameters}"
logConfiguration = "${local.logConfiguration == "{}" ? "null" : local.logConfiguration}"
memory = "${var.memory == 0 ? "null" : var.memory}"
memoryReservation = "${var.memoryReservation == 0 ? "null" : var.memoryReservation}"
mountPoints = "${local.mountPoints == "[]" ? "null" : local.mountPoints}"
name = "${var.name == "" ? "null" : var.name}"
portMappings = "${local.portMappings == "[]" ? "null" : local.portMappings}"
privileged = "${var.privileged ? true : false}"
pseudoTerminal = "${var.pseudoTerminal ? true : false}"
readonlyRootFilesystem = "${var.readonlyRootFilesystem ? true : false}"
repositoryCredentials = "${local.repositoryCredentials == "{}" ? "null" : local.repositoryCredentials}"
resourceRequirements = "${local.resourceRequirements == "[]" ? "null" : local.resourceRequirements}"
secrets = "${local.secrets == "[]" ? "null" : local.secrets}"
systemControls = "${local.systemControls == "[]" ? "null" : local.systemControls}"
ulimits = "${local.ulimits == "[]" ? "null" : local.ulimits}"
user = "${var.user == "" ? "null" : var.user}"
volumesFrom = "${local.volumesFrom == "[]" ? "null" : local.volumesFrom}"
workingDirectory = "${var.workingDirectory == "" ? "null" : var.workingDirectory}"
}
}
resource "aws_ecs_task_definition" "ecs_task_definition" {
container_definitions = "${local.container_definitions}"
execution_role_arn = "${var.execution_role_arn}"
family = "${var.family}"
ipc_mode = "${var.ipc_mode}"
network_mode = "${var.network_mode}"
pid_mode = "${var.pid_mode}"
placement_constraints = "${var.placement_constraints}"
requires_compatibilities = "${var.requires_compatibilities}"
task_role_arn = "${var.task_role_arn}"
volume = "${var.volumes}"
count = "${var.register_task_definition ? 1 : 0}"
}