forked from cloudposse/terraform-aws-alb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
248 lines (214 loc) · 10.4 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
locals {
# cidrnetmask returns an error for IPv6 addresses
# cidrhost works with both IPv4 and IPv6, and returns an error if the argument is not a valid IPv4/IPv6 CIDR prefix
http_ingress_cidr_blocks_v4 = [for cidr in var.http_ingress_cidr_blocks : cidr if can(cidrnetmask(cidr))]
http_ingress_cidr_blocks_v6 = var.ip_address_type == "dualstack" ? [for cidr in var.http_ingress_cidr_blocks : cidr if !can(cidrnetmask(cidr)) && can(cidrhost(cidr, 0))] : []
https_ingress_cidr_blocks_v4 = [for cidr in var.https_ingress_cidr_blocks : cidr if can(cidrnetmask(cidr))]
https_ingress_cidr_blocks_v6 = var.ip_address_type == "dualstack" ? [for cidr in var.https_ingress_cidr_blocks : cidr if !can(cidrnetmask(cidr)) && can(cidrhost(cidr, 0))] : []
}
resource "aws_security_group" "default" {
count = module.this.enabled && var.security_group_enabled ? 1 : 0
description = "Controls access to the ALB (HTTP/HTTPS)"
vpc_id = var.vpc_id
name = module.this.id
tags = module.this.tags
}
resource "aws_security_group_rule" "egress" {
count = module.this.enabled && var.security_group_enabled ? 1 : 0
type = "egress"
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = one(aws_security_group.default[*].id)
}
resource "aws_security_group_rule" "http_ingress" {
count = module.this.enabled && var.security_group_enabled && var.http_enabled ? 1 : 0
type = "ingress"
from_port = var.http_port
to_port = var.http_port
protocol = "tcp"
cidr_blocks = local.http_ingress_cidr_blocks_v4
ipv6_cidr_blocks = local.http_ingress_cidr_blocks_v6
prefix_list_ids = var.http_ingress_prefix_list_ids
security_group_id = one(aws_security_group.default[*].id)
}
resource "aws_security_group_rule" "https_ingress" {
count = module.this.enabled && var.security_group_enabled && var.https_enabled ? 1 : 0
type = "ingress"
from_port = var.https_port
to_port = var.https_port
protocol = "tcp"
cidr_blocks = local.https_ingress_cidr_blocks_v4
ipv6_cidr_blocks = local.https_ingress_cidr_blocks_v6
prefix_list_ids = var.https_ingress_prefix_list_ids
security_group_id = one(aws_security_group.default[*].id)
}
module "access_logs" {
source = "git::https://github.com/miquido/terraform-aws-lb-s3-bucket"
enabled = module.this.enabled && var.access_logs_enabled && var.access_logs_s3_bucket_id == null
attributes = compact(concat(module.this.attributes, ["alb", "access", "logs"]))
allow_ssl_requests_only = var.allow_ssl_requests_only
force_destroy = var.alb_access_logs_s3_bucket_force_destroy
lifecycle_configuration_rules = var.lifecycle_configuration_rules
lifecycle_rule_enabled = var.lifecycle_rule_enabled
enable_glacier_transition = var.enable_glacier_transition
expiration_days = var.expiration_days
glacier_transition_days = var.glacier_transition_days
noncurrent_version_expiration_days = var.noncurrent_version_expiration_days
noncurrent_version_transition_days = var.noncurrent_version_transition_days
standard_transition_days = var.standard_transition_days
context = module.this.context
}
module "default_load_balancer_label" {
source = "cloudposse/label/null"
version = "0.25.0"
id_length_limit = var.load_balancer_name_max_length
context = module.this.context
}
resource "aws_lb" "default" {
#bridgecrew:skip=BC_AWS_NETWORKING_41 - Skipping Ensure that ALB Drops HTTP Headers
#bridgecrew:skip=BC_AWS_LOGGING_22 - Skipping Ensure ELBv2 has Access Logging Enabled
count = module.this.enabled ? 1 : 0
name = var.load_balancer_name == "" ? module.default_load_balancer_label.id : substr(var.load_balancer_name, 0, var.load_balancer_name_max_length)
tags = module.this.tags
internal = var.internal
load_balancer_type = "application"
security_groups = compact(
concat(var.security_group_ids, [one(aws_security_group.default[*].id)]),
)
subnets = var.subnet_ids
enable_cross_zone_load_balancing = var.cross_zone_load_balancing_enabled
enable_http2 = var.http2_enabled
idle_timeout = var.idle_timeout
ip_address_type = var.ip_address_type
enable_deletion_protection = var.deletion_protection_enabled
drop_invalid_header_fields = var.drop_invalid_header_fields
preserve_host_header = var.preserve_host_header
xff_header_processing_mode = var.xff_header_processing_mode
client_keep_alive = var.client_keep_alive
access_logs {
bucket = try(element(compact([var.access_logs_s3_bucket_id, module.access_logs.bucket_id]), 0), "")
prefix = var.access_logs_prefix
enabled = var.access_logs_enabled
}
}
module "default_target_group_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = concat(module.this.attributes, ["default"])
id_length_limit = var.target_group_name_max_length
context = module.this.context
}
resource "aws_lb_target_group" "default" {
count = module.this.enabled && var.default_target_group_enabled ? 1 : 0
name = var.target_group_name == "" ? module.default_target_group_label.id : substr(var.target_group_name, 0, var.target_group_name_max_length)
port = var.target_group_port
protocol = var.target_group_protocol
protocol_version = var.target_group_protocol_version
vpc_id = var.vpc_id
target_type = var.target_group_target_type
load_balancing_algorithm_type = var.load_balancing_algorithm_type
deregistration_delay = var.deregistration_delay
slow_start = var.slow_start
health_check {
protocol = var.health_check_protocol != null ? var.health_check_protocol : var.target_group_protocol
path = var.health_check_path
port = var.health_check_port
timeout = var.health_check_timeout
healthy_threshold = var.health_check_healthy_threshold
unhealthy_threshold = var.health_check_unhealthy_threshold
interval = var.health_check_interval
matcher = var.health_check_matcher
}
dynamic "stickiness" {
for_each = var.stickiness == null ? [] : [var.stickiness]
content {
type = "lb_cookie"
cookie_duration = stickiness.value.cookie_duration
enabled = var.target_group_protocol == "TCP" ? false : stickiness.value.enabled
}
}
lifecycle {
create_before_destroy = true
}
tags = merge(
module.default_target_group_label.tags,
var.target_group_additional_tags
)
}
resource "aws_lb_listener" "http_forward" {
#bridgecrew:skip=BC_AWS_GENERAL_43 - Skipping Ensure that load balancer is using TLS 1.2.
#bridgecrew:skip=BC_AWS_NETWORKING_29 - Skipping Ensure ALB Protocol is HTTPS
count = module.this.enabled && var.http_enabled && var.http_redirect != true ? 1 : 0
load_balancer_arn = one(aws_lb.default[*].arn)
port = var.http_port
protocol = "HTTP"
tags = merge(module.this.tags, var.listener_additional_tags)
default_action {
target_group_arn = var.listener_http_fixed_response != null ? null : one(aws_lb_target_group.default[*].arn)
type = var.listener_http_fixed_response != null ? "fixed-response" : "forward"
dynamic "fixed_response" {
for_each = var.listener_http_fixed_response != null ? [var.listener_http_fixed_response] : []
content {
content_type = fixed_response.value["content_type"]
message_body = fixed_response.value["message_body"]
status_code = fixed_response.value["status_code"]
}
}
}
}
resource "aws_lb_listener" "http_redirect" {
count = module.this.enabled && var.http_enabled && var.http_redirect == true ? 1 : 0
load_balancer_arn = one(aws_lb.default[*].arn)
port = var.http_port
protocol = "HTTP"
tags = merge(module.this.tags, var.listener_additional_tags)
default_action {
target_group_arn = one(aws_lb_target_group.default[*].arn)
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_listener" "https" {
#bridgecrew:skip=BC_AWS_GENERAL_43 - Skipping Ensure that load balancer is using TLS 1.2.
count = module.this.enabled && var.https_enabled ? 1 : 0
load_balancer_arn = one(aws_lb.default[*].arn)
port = var.https_port
protocol = "HTTPS"
ssl_policy = var.https_ssl_policy
certificate_arn = var.certificate_arn
tags = merge(module.this.tags, var.listener_additional_tags)
default_action {
target_group_arn = var.listener_https_fixed_response != null || var.listener_https_redirect != null ? null : one(aws_lb_target_group.default[*].arn)
type = var.listener_https_fixed_response != null ? "fixed-response" : var.listener_https_redirect != null ? "redirect" : "forward"
dynamic "fixed_response" {
for_each = var.listener_https_fixed_response != null ? [var.listener_https_fixed_response] : []
content {
content_type = fixed_response.value["content_type"]
message_body = fixed_response.value["message_body"]
status_code = fixed_response.value["status_code"]
}
}
dynamic "redirect" {
for_each = var.listener_https_redirect != null ? [var.listener_https_redirect] : []
content {
host = redirect.value["host"]
path = redirect.value["path"]
port = redirect.value["port"]
protocol = redirect.value["protocol"]
query = redirect.value["query"]
status_code = redirect.value["status_code"]
}
}
}
}
resource "aws_lb_listener_certificate" "https_sni" {
count = module.this.enabled && var.https_enabled && length(var.additional_certs) > 0 ? length(var.additional_certs) : 0
listener_arn = one(aws_lb_listener.https[*].arn)
certificate_arn = var.additional_certs[count.index]
}