-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.tf
330 lines (319 loc) · 8.72 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
locals {
dev_server_enabled = var.consul_server_service_name != ""
gossip_encryption_enabled = var.gossip_key_secret_arn != ""
discover_server_container = {
name = "discover-servers"
image = var.consul_ecs_image
essential = false
user = "root"
command = [
"discover-servers",
"-service-name=${var.consul_server_service_name}",
"-out=/consul/server-ip"
]
mountPoints = [
local.consul_data_mount
]
environment = []
cpu = 0
portMappings = []
volumesFrom = []
logConfiguration = var.log_configuration
}
discover_servers_containers = local.dev_server_enabled ? [local.discover_server_container] : []
consul_data_volume_name = "consul_data"
consul_data_mount = {
sourceVolume = local.consul_data_volume_name
containerPath = "/consul"
}
// container_defs_with_depends_on is the app's container definitions with their dependsOn keys
// modified to add in dependencies on consul-ecs-mesh-init and sidecar-proxy.
// We add these dependencies in so that the app containers don't start until the proxy
// is ready to serve traffic.
container_defs_with_depends_on = [for def in var.container_definitions :
merge(
def,
{
dependsOn = flatten(
concat(
lookup(def, "dependsOn", []),
[
{
containerName = "consul-ecs-mesh-init"
condition = "SUCCESS"
},
{
containerName = "sidecar-proxy"
condition = "HEALTHY"
}
]
))
}
)
]
upstreams_flag = join(",", [for upstream in var.upstreams : "${upstream["destination_name"]}:${upstream["local_bind_port"]}"])
}
resource "aws_iam_role" "task" {
name = "${var.family}-task"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
},
]
})
inline_policy {
name = "exec"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
%{if local.dev_server_enabled~}
{
"Effect": "Allow",
"Action": [
"ecs:ListTasks",
"ecs:DescribeTasks"
],
"Resource": [
"*"
]
},
%{endif~}
{
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": [
"*"
]
}
]
}
EOF
}
}
resource "aws_iam_role_policy_attachment" "additional_task_policies" {
count = length(var.additional_task_role_policies)
role = aws_iam_role.task.id
policy_arn = var.additional_task_role_policies[count.index]
}
resource "aws_iam_policy" "execution" {
name = "${var.family}-execution"
path = "/ecs/"
description = "${var.family} mesh-task execution policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
%{if var.tls~}
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue"
],
"Resource": [
"${var.consul_server_ca_cert_arn}"
]
},
%{endif~}
%{if local.gossip_encryption_enabled~}
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue"
],
"Resource": [
"${var.gossip_key_secret_arn}"
]
},
%{endif~}
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role" "execution" {
name = "${var.family}-execution"
path = "/ecs/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "execution" {
role = aws_iam_role.execution.id
policy_arn = aws_iam_policy.execution.arn
}
resource "aws_iam_role_policy_attachment" "additional_execution_policies" {
count = length(var.additional_execution_role_policies)
role = aws_iam_role.execution.id
policy_arn = var.additional_execution_role_policies[count.index]
}
resource "aws_ecs_task_definition" "this" {
family = var.family
requires_compatibilities = ["FARGATE", "EC2"]
network_mode = "awsvpc"
cpu = 256
memory = 512
execution_role_arn = aws_iam_role.execution.arn
task_role_arn = aws_iam_role.task.arn
volume {
name = local.consul_data_volume_name
}
tags = {
"consul.hashicorp.com/mesh" = "true"
}
container_definitions = jsonencode(
flatten(
concat(
local.discover_servers_containers,
local.container_defs_with_depends_on,
[
{
name = "consul-ecs-mesh-init"
image = var.consul_ecs_image
essential = false
logConfiguration = var.log_configuration
command = [
"mesh-init",
"-envoy-bootstrap-file=/consul/envoy-bootstrap.json",
"-port=${var.port}",
"-upstreams=${local.upstreams_flag}",
]
user = "root"
mountPoints = [
local.consul_data_mount
]
dependsOn = local.dev_server_enabled ? [
{
containerName = "discover-servers"
condition = "SUCCESS"
},
] : []
cpu = 0
volumesFrom = []
environment = []
portMappings = []
},
{
name = "consul-client"
image = var.consul_image
essential = false
portMappings = [
{
containerPort = 8300
hostPort = 8300
protocol = "tcp"
},
{
containerPort = 8300
hostPort = 8300
protocol = "udp"
},
]
logConfiguration = var.log_configuration
entryPoint = ["/bin/sh", "-ec"]
command = [replace(
templatefile(
"${path.module}/templates/consul_client_command.tpl",
{
dev_server_enabled = local.dev_server_enabled
gossip_encryption_enabled = local.gossip_encryption_enabled
retry_join = var.retry_join
tls = var.tls
}
), "\r", "")
]
mountPoints = [
local.consul_data_mount
]
linuxParameters = {
initProcessEnabled = true
}
dependsOn = local.dev_server_enabled ? [{
containerName = "discover-servers"
condition = "SUCCESS"
}] : []
cpu = 0
volumesFrom = []
environment = []
secrets = concat(
var.tls ? [
{
name = "CONSUL_CACERT",
valueFrom = var.consul_server_ca_cert_arn
}
] : [],
local.gossip_encryption_enabled ? [
{
name = "CONSUL_GOSSIP_ENCRYPTION_KEY",
valueFrom = var.gossip_key_secret_arn
}
] : [],
)
},
{
name = "sidecar-proxy"
image = var.envoy_image
essential = false
logConfiguration = var.log_configuration
command = ["envoy", "--config-path", "/consul/envoy-bootstrap.json"]
portMappings = [
{
containerPort = 20000
hostPort = 20000
protocol = "tcp"
},
]
mountPoints = [
local.consul_data_mount
]
dependsOn = [
{
containerName = "consul-ecs-mesh-init"
condition = "SUCCESS"
},
]
healthCheck = {
command = ["nc", "-z", "127.0.0.1", "20000"]
interval = 30
retries = 3
timeout = 5
}
cpu = 0
volumesFrom = []
environment = []
}
]
)
)
)
}