forked from Flaconi/terraform-aws-alb-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
232 lines (196 loc) · 5.98 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
# -------------------------------------------------------------------------------------------------
# VPC
# -------------------------------------------------------------------------------------------------
resource "aws_vpc" "this" {
cidr_block = var.cidr
assign_generated_ipv6_cidr_block = true
tags = merge(
{
"Name" = var.name
},
var.tags,
)
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.this.id
tags = merge(
{
"Name" = var.name
"Type" = "public"
},
var.tags,
)
}
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.this.id
cidr_block = cidrsubnet(var.cidr, var.ipv4_newbits, count.index)
ipv6_cidr_block = cidrsubnet(aws_vpc.this.ipv6_cidr_block, 8, count.index)
map_public_ip_on_launch = false
assign_ipv6_address_on_creation = true
availability_zone = data.aws_availability_zones.available.names[count.index]
# If AWS adds another availability zone, this will not have effect on the already picked subnets.
lifecycle {
ignore_changes = [availability_zone]
}
tags = merge(
{
"Name" = format(
"%s-%s",
var.name,
count.index,
)
},
var.tags,
)
}
resource "aws_route_table_association" "this" {
count = 2
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_route" "public_internet_gateway" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this.id
timeouts {
create = "5m"
}
}
resource "aws_route" "ipv6_internet_gateway" {
count = var.ipv6_networking_enabled ? 1 : 0
route_table_id = aws_route_table.public.id
destination_ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.this.id
timeouts {
create = "5m"
}
}
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = merge(
{
"Name" = var.name
},
var.tags,
)
}
resource "aws_security_group" "this" {
name = "${var.name}-albredirect"
description = "Allow inbound 80/443 traffic alb redirect"
vpc_id = aws_vpc.this.id
ingress {
# TLS (change to whatever ports you need)
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = var.ipv6_networking_enabled ? ["::/0"] : []
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = var.ipv6_networking_enabled ? ["::/0"] : []
}
}
# -------------------------------------------------------------------------------------------------
# LB
# -------------------------------------------------------------------------------------------------
resource "aws_lb" "this" {
load_balancer_type = "application"
name = var.name
security_groups = [aws_security_group.this.id]
subnets = aws_subnet.public.*.id
enable_cross_zone_load_balancing = false
enable_deletion_protection = false
enable_http2 = true
ip_address_type = var.lb_ip_address_type
tags = merge(
var.tags,
{
"Name" = var.name
},
)
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.this.arn
port = 80
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = var.response_message_body
status_code = var.response_code
}
}
}
resource "aws_lb_listener" "https" {
count = var.https_enabled ? 1 : 0
load_balancer_arn = aws_lb.this.arn
port = 443
protocol = "HTTPS"
certificate_arn = var.certificate_arn
ssl_policy = "ELBSecurityPolicy-2016-08"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = var.response_message_body
status_code = var.response_code
}
}
}
resource "aws_lb_listener_certificate" "https" {
listener_arn = join("", aws_lb_listener.https.*.arn)
certificate_arn = var.extra_ssl_certs[count.index]
count = var.extra_ssl_certs_count
}
locals {
listener_types = slice(["HTTP", "HTTPS"], 0, var.https_enabled ? 2 : 1)
rules = flatten([
for rule in var.redirect_rules : [
for listener_type in local.listener_types : {
listener_type = listener_type,
rule = rule,
}
]])
}
resource "aws_lb_listener_rule" "this" {
# If condition makes sure we not forward from HTTPS to HTTP, as this is not allowed by the AWS ALB
for_each = { for rule in local.rules :
"${rule.listener_type}://${rule.rule.host_match}${rule.rule.path_match}" => merge(
rule.rule,
{ "listener_type" = rule.listener_type }
) if lookup(rule.rule, "disabled_for", "") != rule.listener_type }
listener_arn = each.value.listener_type == "HTTP" ? aws_lb_listener.http.arn : join("", aws_lb_listener.https.*.arn)
action {
type = "redirect"
redirect {
port = lookup(each.value, "redirect_port", "#{port}")
protocol = lookup(each.value, "redirect_protocol", "#{protocol}")
host = lookup(each.value, "redirect_host", "#{host}")
query = lookup(each.value, "redirect_query", null)
path = lookup(each.value, "redirect_path", null)
status_code = lookup(each.value, "redirect_status_code", "HTTP_302")
}
}
dynamic condition {
for_each = lookup(each.value, "path_match", "*") != "*" ? [1] : []
content {
path_pattern {
values = [lookup(each.value, "path_match", "*")]
}
}
}
dynamic condition {
for_each = lookup(each.value, "host_match", "*") != "*" ? [1] : []
content {
host_header {
values = [lookup(each.value, "host_match", "*")]
}
}
}
}