-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathalb.tf
176 lines (143 loc) · 3.54 KB
/
alb.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
resource "aws_lb" "ecs" {
count = var.alb ? 1 : 0
load_balancer_type = "application"
internal = false
name = "ecs-${var.name}"
subnets = var.public_subnet_ids
drop_invalid_header_fields = var.alb_drop_invalid_header_fields
enable_deletion_protection = var.alb_enable_deletion_protection
security_groups = [
aws_security_group.alb[0].id,
]
idle_timeout = var.idle_timeout
dynamic "access_logs" {
for_each = compact([var.lb_access_logs_bucket])
content {
bucket = var.lb_access_logs_bucket
prefix = var.lb_access_logs_prefix
enabled = true
}
}
tags = merge(
var.tags,
{
"Terraform" = true,
"Name" = "ecs-${var.name}"
},
)
}
resource "aws_lb_listener" "ecs_https" {
count = var.alb ? 1 : 0
load_balancer_arn = aws_lb.ecs[0].arn
port = "443"
protocol = "HTTPS"
ssl_policy = var.alb_ssl_policy
certificate_arn = var.certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.ecs_default_https[0].arn
}
tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
resource "aws_lb_listener" "ecs_http_redirect" {
count = var.alb && var.alb_http_listener ? 1 : 0
load_balancer_arn = aws_lb.ecs[0].arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
resource "aws_lb_listener" "ecs_test_https" {
count = var.alb && var.alb_test_listener ? 1 : 0
load_balancer_arn = aws_lb.ecs[0].arn
port = "8443"
protocol = "HTTPS"
ssl_policy = var.alb_ssl_policy
certificate_arn = var.certificate_arn
default_action {
type = "forward"
#target_group_arn = aws_lb_target_group.ecs_replacement_https[0].arn
target_group_arn = aws_lb_target_group.ecs_default_https[0].arn
}
tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
resource "aws_lb_listener" "ecs_test_http_redirect" {
count = var.alb && var.alb_http_listener && var.alb_test_listener ? 1 : 0
load_balancer_arn = aws_lb.ecs[0].arn
port = "8080"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "8443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
# Generate a random string to add it to the name of the Target Group
resource "random_string" "alb_prefix" {
length = 4
upper = false
special = false
}
resource "aws_lb_target_group" "ecs_default_http" {
count = var.alb && var.alb_http_listener ? 1 : 0
name = replace(substr("ecs-${var.name}-default-http-${random_string.alb_prefix.result}", 0, 32), "/-+$/", "")
port = 80
protocol = "HTTP"
vpc_id = var.vpc_id
tags = merge(
var.tags,
{
"Terraform" = true
},
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_lb_target_group" "ecs_default_https" {
count = var.alb ? 1 : 0
name = replace(substr("ecs-${var.name}-default-https-${random_string.alb_prefix.result}", 0, 32), "/-+$/", "")
port = 80
protocol = "HTTP"
vpc_id = var.vpc_id
tags = merge(
var.tags,
{
"Terraform" = true
},
)
lifecycle {
create_before_destroy = true
}
}