forked from jocelynaladin/terraform-aws-ecs-task-definition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
200 lines (178 loc) · 9.12 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
# 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)
dependsOn = jsonencode(var.dependsOn)
dnsSearchDomains = jsonencode(var.dnsSearchDomains)
dnsServers = jsonencode(var.dnsServers)
dockerLabels = jsonencode(var.dockerLabels)
dockerSecurityOptions = jsonencode(var.dockerSecurityOptions)
entryPoint = jsonencode(var.entryPoint)
environmentFiles = jsonencode(var.environmentFiles)
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)
firelensConfiguration = jsonencode(var.firelensConfiguration)
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)
startTimeout = jsonencode(var.startTimeout)
stopTimeout = jsonencode(var.stopTimeout)
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
dependsOn = local.dependsOn == "[]" ? "null" : local.dependsOn
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
environmentFiles = local.environmentFiles == "[]" ? "null" : local.environmentFiles
essential = var.essential ? true : false
extraHosts = local.extraHosts == "[]" ? "null" : local.extraHosts
firelensConfiguration = local.firelensConfiguration == "{}" ? "null" : local.firelensConfiguration
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
startTimeout = var.startTimeout == 0 ? "null" : var.startTimeout
stopTimeout = var.stopTimeout == 0 ? "null" : var.stopTimeout
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
# # Fargate requires cpu and memory to be defined at the task level
# cpu = var.cpu
# memory = var.memory
# dynamic "placement_constraints" {
# for_each = var.placement_constraints
# content {
# # TF-UPGRADE-TODO: The automatic upgrade tool can't predict
# # which keys might be set in maps assigned here, so it has
# # produced a comprehensive set here. Consider simplifying
# # this after confirming which keys can be set in practice.
# expression = lookup(placement_constraints.value, "expression", null)
# type = placement_constraints.value.type
# }
# }
# requires_compatibilities = var.requires_compatibilities
# task_role_arn = var.task_role_arn
# dynamic "volume" {
# for_each = var.volumes
# content {
# # TF-UPGRADE-TODO: The automatic upgrade tool can't predict
# # which keys might be set in maps assigned here, so it has
# # produced a comprehensive set here. Consider simplifying
# # this after confirming which keys can be set in practice.
# host_path = lookup(volume.value, "host_path", null)
# name = volume.value.name
# dynamic "docker_volume_configuration" {
# for_each = lookup(volume.value, "docker_volume_configuration", [])
# content {
# 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)
# scope = lookup(docker_volume_configuration.value, "scope", 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)
# }
# }
# }
# }
# tags = var.tags
# count = var.register_task_definition ? 1 : 0
# }