forked from cn-terraform/terraform-aws-ecs-alb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
347 lines (313 loc) · 12.1 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#------------------------------------------------------------------------------
# S3 BUCKET - For access logs
#------------------------------------------------------------------------------
resource "aws_s3_bucket" "logs" {
bucket = "${var.name_prefix}-lb-logs"
acl = "log-delivery-write"
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-lb-logs"
},
)
}
#------------------------------------------------------------------------------
# IAM POLICY DOCUMENT - For access logs to the S3 bucket
#------------------------------------------------------------------------------
data "aws_elb_service_account" "default" {}
data "aws_iam_policy_document" "lb_logs_access_policy_document" {
statement {
effect = "Allow"
principals {
type = "AWS"
identifiers = [data.aws_elb_service_account.default.arn]
}
actions = [
"s3:PutObject",
]
resources = [
"${aws_s3_bucket.logs.arn}/*",
"arn:aws:s3:::${var.name_prefix}-lb-logs/*",
]
}
}
#------------------------------------------------------------------------------
# IAM POLICY - For access logs to the s3 bucket
#------------------------------------------------------------------------------
resource "aws_s3_bucket_policy" "lb_logs_access_policy" {
bucket = aws_s3_bucket.logs.id
policy = data.aws_iam_policy_document.lb_logs_access_policy_document.json
}
#------------------------------------------------------------------------------
# S3 bucket block public access
#------------------------------------------------------------------------------
resource "aws_s3_bucket_public_access_block" "lb_logs_block_public_access" {
count = var.block_s3_bucket_public_access ? 1 : 0
bucket = aws_s3_bucket.logs.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
#------------------------------------------------------------------------------
# APPLICATION LOAD BALANCER
#------------------------------------------------------------------------------
resource "aws_lb" "lb" {
name = "${var.name_prefix}-lb"
internal = var.internal
load_balancer_type = "application"
drop_invalid_header_fields = var.drop_invalid_header_fields
subnets = var.internal ? var.private_subnets : var.public_subnets
idle_timeout = var.idle_timeout
enable_deletion_protection = var.enable_deletion_protection
enable_cross_zone_load_balancing = var.enable_cross_zone_load_balancing
enable_http2 = var.enable_http2
ip_address_type = var.ip_address_type
security_groups = compact(
concat(var.security_groups, [aws_security_group.lb_access_sg.id]),
)
access_logs {
bucket = aws_s3_bucket.logs.id
enabled = true
}
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-lb"
},
)
}
#------------------------------------------------------------------------------
# ACCESS CONTROL TO APPLICATION LOAD BALANCER
#------------------------------------------------------------------------------
resource "aws_security_group" "lb_access_sg" {
name = "${var.name_prefix}-lb-access-sg"
description = "Controls access to the Load Balancer"
vpc_id = var.vpc_id
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-lb-access-sg"
},
)
}
resource "aws_security_group_rule" "ingress_through_http" {
for_each = var.http_ports
security_group_id = aws_security_group.lb_access_sg.id
type = "ingress"
from_port = each.value.listener_port
to_port = each.value.listener_port
protocol = "tcp"
cidr_blocks = var.http_ingress_cidr_blocks
prefix_list_ids = var.http_ingress_prefix_list_ids
}
resource "aws_security_group_rule" "ingress_through_https" {
for_each = var.https_ports
security_group_id = aws_security_group.lb_access_sg.id
type = "ingress"
from_port = each.value.listener_port
to_port = each.value.listener_port
protocol = "tcp"
cidr_blocks = var.https_ingress_cidr_blocks
prefix_list_ids = var.https_ingress_prefix_list_ids
}
#------------------------------------------------------------------------------
# AWS LOAD BALANCER - Target Groups
#------------------------------------------------------------------------------
resource "aws_lb_target_group" "lb_http_tgs" {
for_each = {
for name, config in var.http_ports : name => config
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
}
name = "${var.name_prefix}-http-${each.value.target_group_port}"
port = each.value.target_group_port
protocol = lookup(each.value, "target_group_protocol", "") == "" ? "HTTP" : each.value.target_group_protocol
vpc_id = var.vpc_id
deregistration_delay = var.deregistration_delay
slow_start = var.slow_start
load_balancing_algorithm_type = var.load_balancing_algorithm_type
dynamic "stickiness" {
for_each = var.stickiness == null ? [] : [var.stickiness]
content {
type = stickiness.value.type
cookie_duration = stickiness.value.cookie_duration
enabled = stickiness.value.enabled
}
}
health_check {
enabled = var.target_group_health_check_enabled
interval = var.target_group_health_check_interval
path = var.target_group_health_check_path
protocol = lookup(each.value, "target_group_protocol", "") == "" ? "HTTP" : each.value.target_group_protocol
timeout = var.target_group_health_check_timeout
healthy_threshold = var.target_group_health_check_healthy_threshold
unhealthy_threshold = var.target_group_health_check_unhealthy_threshold
matcher = var.target_group_health_check_matcher
}
target_type = "ip"
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-http-${each.value.target_group_port}"
},
)
lifecycle {
create_before_destroy = true
}
depends_on = [aws_lb.lb]
}
resource "aws_lb_target_group" "lb_https_tgs" {
for_each = {
for name, config in var.https_ports : name => config
if lookup(config, "type", "") == "" || lookup(config, "type", "") == "forward"
}
name = "${var.name_prefix}-https-${each.value.target_group_port}"
port = each.value.target_group_port
protocol = lookup(each.value, "target_group_protocol", "") == "" ? "HTTPS" : each.value.target_group_protocol
vpc_id = var.vpc_id
deregistration_delay = var.deregistration_delay
slow_start = var.slow_start
load_balancing_algorithm_type = var.load_balancing_algorithm_type
dynamic "stickiness" {
for_each = var.stickiness == null ? [] : [var.stickiness]
content {
type = stickiness.value.type
cookie_duration = stickiness.value.cookie_duration
enabled = stickiness.value.enabled
}
}
health_check {
enabled = var.target_group_health_check_enabled
interval = var.target_group_health_check_interval
path = var.target_group_health_check_path
protocol = lookup(each.value, "target_group_protocol", "") == "" ? "HTTPS" : each.value.target_group_protocol
timeout = var.target_group_health_check_timeout
healthy_threshold = var.target_group_health_check_healthy_threshold
unhealthy_threshold = var.target_group_health_check_unhealthy_threshold
matcher = var.target_group_health_check_matcher
}
target_type = "ip"
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-https-${each.value.target_group_port}"
},
)
lifecycle {
create_before_destroy = true
}
depends_on = [aws_lb.lb]
}
#------------------------------------------------------------------------------
# AWS LOAD BALANCER - Listeners
#------------------------------------------------------------------------------
resource "aws_lb_listener" "lb_http_listeners" {
for_each = var.http_ports
load_balancer_arn = aws_lb.lb.arn
port = each.value.listener_port
protocol = "HTTP"
dynamic "default_action" {
for_each = lookup(each.value, "type", "") == "redirect" ? [1] : []
content {
type = "redirect"
redirect {
host = lookup(each.value, "host", "#{host}")
path = lookup(each.value, "path", "/#{path}")
port = lookup(each.value, "port", "#{port}")
protocol = lookup(each.value, "protocol", "#{protocol}")
query = lookup(each.value, "query", "#{query}")
status_code = lookup(each.value, "status_code", "HTTP_301")
}
}
}
dynamic "default_action" {
for_each = lookup(each.value, "type", "") == "fixed-response" ? [1] : []
content {
type = "fixed-response"
fixed_response {
content_type = lookup(each.value, "content_type", "text/plain")
message_body = lookup(each.value, "message_body", "Fixed response content")
status_code = lookup(each.value, "status_code", "200")
}
}
}
# We fallback to using forward type action if type is not defined
dynamic "default_action" {
for_each = (lookup(each.value, "type", "") == "" || lookup(each.value, "type", "") == "forward") ? [1] : []
content {
target_group_arn = aws_lb_target_group.lb_http_tgs[each.key].arn
type = "forward"
}
}
tags = var.tags
}
resource "aws_lb_listener" "lb_https_listeners" {
for_each = var.https_ports
load_balancer_arn = aws_lb.lb.arn
port = each.value.listener_port
protocol = "HTTPS"
ssl_policy = var.ssl_policy
certificate_arn = var.default_certificate_arn
dynamic "default_action" {
for_each = lookup(each.value, "type", "") == "redirect" ? [1] : []
content {
type = "redirect"
redirect {
host = lookup(each.value, "host", "#{host}")
path = lookup(each.value, "path", "/#{path}")
port = lookup(each.value, "port", "#{port}")
protocol = lookup(each.value, "protocol", "#{protocol}")
query = lookup(each.value, "query", "#{query}")
status_code = lookup(each.value, "status_code", "HTTP_301")
}
}
}
dynamic "default_action" {
for_each = lookup(each.value, "type", "") == "fixed-response" ? [1] : []
content {
type = "fixed-response"
fixed_response {
content_type = lookup(each.value, "content_type", "text/plain")
message_body = lookup(each.value, "message_body", "Fixed response content")
status_code = lookup(each.value, "status_code", "200")
}
}
}
# We fallback to using forward type action if type is not defined
dynamic "default_action" {
for_each = (lookup(each.value, "type", "") == "" || lookup(each.value, "type", "") == "forward") ? [1] : []
content {
target_group_arn = aws_lb_target_group.lb_https_tgs[each.key].arn
type = "forward"
}
}
tags = var.tags
}
locals {
list_maps_listener_certificate_arns = flatten([
for cert_arn in var.additional_certificates_arn_for_https_listeners : [
for listener in aws_lb_listener.lb_https_listeners : {
name = "${listener}-${cert_arn}"
listener_arn = listener.arn
certificate_arn = cert_arn
}
]
])
map_listener_certificate_arns = {
for obj in local.list_maps_listener_certificate_arns : obj.name => {
listener_arn = obj.listener_arn,
certificate_arn = obj.certificate_arn
}
}
}
resource "aws_lb_listener_certificate" "additional_certificates_for_https_listeners" {
for_each = local.map_listener_certificate_arns
listener_arn = each.value.listener_arn
certificate_arn = each.value.certificate_arn
}