-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
287 lines (243 loc) · 8.6 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
resource "aws_cloudwatch_log_group" "main" {
name = var.application_name
retention_in_days = var.log_retention_in_days
tags = var.tags
}
data "aws_iam_policy_document" "task_assume" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "execution_role" {
name = "${var.application_name}-task-execution-role"
assume_role_policy = data.aws_iam_policy_document.task_assume.json
}
data "aws_iam_policy_document" "task_execution_permissions" {
statement {
effect = "Allow"
resources = [
"*",
]
actions = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents",
]
}
}
resource "aws_iam_role_policy" "task_execution" {
name = "${var.application_name}-task-execution"
role = aws_iam_role.execution_role.id
policy = data.aws_iam_policy_document.task_execution_permissions.json
}
resource "aws_iam_role" "task_role" {
name = "${var.application_name}-task-role"
assume_role_policy = data.aws_iam_policy_document.task_assume.json
}
data "aws_iam_policy_document" "ecs_task_logs" {
statement {
effect = "Allow"
resources = [
aws_cloudwatch_log_group.main.arn,
]
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
}
}
resource "aws_iam_role_policy" "ecs_task_logs" {
name = "${var.application_name}-log-permissions"
role = aws_iam_role.task_role.id
policy = data.aws_iam_policy_document.ecs_task_logs.json
}
resource "aws_security_group" "ecs_service" {
vpc_id = var.vpc_id
name = "${var.application_name}-ecs-service-sg"
description = "Fargate service security group"
tags = merge(
var.tags,
{ Name = "${var.application_name}-sg" }
)
}
resource "aws_security_group_rule" "service_from_loadbalancer" {
for_each = { for lb in var.lb_listeners : lb.listener_arn => lb.security_group_id }
security_group_id = aws_security_group.ecs_service.id
type = "ingress"
protocol = "tcp"
from_port = var.application_container.port
to_port = var.application_container.port
source_security_group_id = each.value
}
resource "aws_security_group_rule" "loadbalancer_to_service" {
for_each = { for lb in var.lb_listeners : lb.listener_arn => lb.security_group_id }
security_group_id = each.value
type = "egress"
protocol = "tcp"
from_port = var.application_container.port
to_port = var.application_container.port
source_security_group_id = aws_security_group.ecs_service.id
}
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"]
}
resource "aws_lb_target_group" "service" {
for_each = { for idx, value in var.lb_listeners : idx => value }
vpc_id = var.vpc_id
target_type = "ip"
port = var.application_container.port
protocol = var.application_container.protocol
deregistration_delay = var.lb_deregistration_delay
dynamic "health_check" {
for_each = [var.lb_health_check]
content {
enabled = lookup(health_check.value, "enabled", null)
healthy_threshold = lookup(health_check.value, "healthy_threshold", null)
interval = lookup(health_check.value, "interval", null)
matcher = lookup(health_check.value, "matcher", 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)
unhealthy_threshold = lookup(health_check.value, "unhealthy_threshold", null)
}
}
# NOTE: Terraform is unable to destroy a target group while a listener is attached,
# therefor we have to create a new one before destroying the old. This also means
# we have to let it have a random name, and then tag it with the desired name.
lifecycle {
create_before_destroy = true
}
tags = merge(
var.tags,
{ Name = "${var.application_name}-target-${var.application_container.port}-${each.key}" }
)
}
resource "aws_lb_listener_rule" "service" {
for_each = { for idx, value in var.lb_listeners : idx => value }
listener_arn = each.value.listener_arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.service[each.key].arn
}
dynamic "condition" {
for_each = each.value.conditions
content {
dynamic "path_pattern" {
for_each = condition.value.path_pattern != null ? [condition.value.path_pattern] : []
content {
values = [path_pattern.value]
}
}
dynamic "host_header" {
for_each = condition.value.host_header != null ? [condition.value.host_header] : []
content {
values = [host_header.value]
}
}
dynamic "http_header" {
for_each = condition.value.http_header != null ? [condition.value.http_header] : []
content {
http_header_name = http_header.value.name
values = http_header.value.values
}
}
}
}
}
locals {
containers = [
for container in concat([var.application_container], var.sidecar_containers) : {
name = container.name
image = container.image
command = try(container.command, null)
# Only the application container is essential
# Container names have to be unique, so this is guaranteed to be correct.
essential = try(container.essential, container.name == var.application_container.name)
environment = try(container.environment, {})
secrets = try(container.secrets, {})
port = container.port
network_protocol = try(container.network_protocol, "tcp")
health_check = try(container.health_check, null)
}
]
}
data "aws_region" "current" {}
resource "aws_ecs_task_definition" "task" {
family = var.application_name
container_definitions = jsonencode([
for container in local.containers : {
name = container.name
image = container.image
command = container.command
essential = container.essential
environment = [for key, value in container.environment : {
name = key
value = value
}]
secrets = [for key, value in container.secrets : {
name = key
valueFrom = value
}]
portMappings = [{
containerPort = tonumber(container.port)
hostPort = tonumber(container.port)
protocol = container.network_protocol
}]
logConfiguration = {
logDriver = "awslogs"
options = {
"awslogs-group" : aws_cloudwatch_log_group.main.name,
"awslogs-region" : data.aws_region.current.name,
"awslogs-stream-prefix" : container.name
}
}
healthCheck = container.health_check
}
])
execution_role_arn = aws_iam_role.execution_role.arn
task_role_arn = aws_iam_role.task_role.arn
requires_compatibilities = ["FARGATE"]
cpu = var.cpu
memory = var.memory
network_mode = "awsvpc"
}
resource "aws_ecs_service" "service" {
name = var.application_name
cluster = var.ecs_cluster_id
task_definition = aws_ecs_task_definition.task.arn
desired_count = var.desired_count
launch_type = "FARGATE"
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
deployment_maximum_percent = var.deployment_maximum_percent
health_check_grace_period_seconds = var.health_check_grace_period_seconds
wait_for_steady_state = var.wait_for_steady_state
propagate_tags = var.propagate_tags
network_configuration {
subnets = var.private_subnet_ids
security_groups = [aws_security_group.ecs_service.id]
assign_public_ip = var.assign_public_ip
}
dynamic "load_balancer" {
for_each = var.lb_listeners
content {
container_name = var.application_container.name
container_port = var.application_container.port
target_group_arn = aws_lb_target_group.service[load_balancer.key].arn
}
}
}