This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
172 lines (145 loc) · 5.02 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
/**
* # Comtravo's Terraform AWS ALB module
*
* ## Usage:
*
* ```hcl
* module "website-alb" {
* source = "github.com/comtravo/terraform-aws-alb?ref=3.0.0"
*
* environment = terraform.workspace
* name = "website"
* internal = false
* vpc_id = module.main_vpc.vpc_id
* security_group_ids = [aws_security_group.website-alb.id]
* subnet_ids = module.main_vpc.public_subnets
* idle_timeout = 120
*
* http_listener_port = 80
*
* https_listener_config = {
* port = 443
* certificates = [
* data.aws_acm_certificate.comtravoDotCom.arn,
* data.aws_acm_certificate.webDotComtravoDotCom.arn,
* data.aws_acm_certificate.comtravoDotDe.arn
* ],
* number_of_certificates = 3
* }
* }
* ```
*
*/
locals {
enable_https = var.https_listener_config != null && var.enable == true ? true : false
}
locals {
enable_http_count = local.enable_count
enable_https_count = local.enable_https ? 1 : 0
}
locals {
certificate_length = local.enable_https ? var.https_listener_config.number_of_certificates : 0
certificates = local.enable_https ? var.https_listener_config.certificates : []
}
locals {
default_cert = local.enable_https ? element(local.certificates, 0) : ""
additional_certificate_length = local.enable_https && local.certificate_length > 1 ? local.certificate_length - 1 : 0
}
resource "aws_alb" "alb" {
count = local.enable_count
name = var.name
internal = var.internal
security_groups = var.security_group_ids
subnets = var.subnet_ids
idle_timeout = var.idle_timeout
ip_address_type = var.ip_address_type
enable_deletion_protection = var.enable_deletion_protection
dynamic "access_logs" {
for_each = var.access_logs
content {
bucket = var.access_logs[0].bucket
prefix = var.access_logs[0].prefix
enabled = var.access_logs[0].enabled
}
}
tags = {
Environment = var.environment
Name = var.name
}
timeouts {
create = lookup(var.timeouts, "create", null)
delete = lookup(var.timeouts, "delete", null)
update = lookup(var.timeouts, "update", null)
}
}
resource "aws_alb_target_group" "dummy_https" {
count = local.enable_https_count
name = "d-${var.name}-${var.https_listener_config["port"]}"
port = 80
protocol = "HTTP"
vpc_id = var.vpc_id
deregistration_delay = 10
health_check {
interval = lookup(var.health_check, "interval", 30)
path = lookup(var.health_check, "path", "/")
healthy_threshold = lookup(var.health_check, "healthy_threshold", 2)
unhealthy_threshold = lookup(var.health_check, "unhealthy_threshold", 8)
timeout = lookup(var.health_check, "timeout", 5)
matcher = lookup(var.health_check, "matcher", 301)
port = "traffic-port"
}
tags = {
Environment = var.environment
Name = var.name
}
}
resource "aws_alb_listener" "listener_https" {
count = local.enable_https_count
load_balancer_arn = aws_alb.alb[0].arn
port = var.https_listener_config["port"]
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = local.default_cert
default_action {
target_group_arn = element(aws_alb_target_group.dummy_https.*.arn, count.index)
type = "forward"
}
depends_on = [aws_alb_target_group.dummy_https]
}
resource "aws_alb_listener_certificate" "additional_certificates" {
count = local.additional_certificate_length
listener_arn = aws_alb_listener.listener_https[0].arn
certificate_arn = element(local.certificates, count.index + 1)
}
resource "aws_alb_target_group" "dummy_http" {
count = local.enable_http_count
name = "d-${var.name}-${var.http_listener_port}"
port = 80
protocol = "HTTP"
vpc_id = var.vpc_id
deregistration_delay = 10
health_check {
interval = lookup(var.health_check, "interval", 30)
path = lookup(var.health_check, "path", "/")
healthy_threshold = lookup(var.health_check, "healthy_threshold", 2)
unhealthy_threshold = lookup(var.health_check, "unhealthy_threshold", 8)
timeout = lookup(var.health_check, "timeout", 5)
matcher = lookup(var.health_check, "matcher", 301)
port = "traffic-port"
}
tags = {
Environment = var.environment
Name = var.name
}
}
resource "aws_alb_listener" "listener_http" {
count = local.enable_http_count
load_balancer_arn = aws_alb.alb[0].arn
port = var.http_listener_port
protocol = "HTTP"
default_action {
target_group_arn = element(aws_alb_target_group.dummy_http.*.arn, count.index)
type = "forward"
}
depends_on = [aws_alb_target_group.dummy_http]
}