1
- # ###############################################################################
1
+ # ##################################################################
2
2
# # defaults
3
- # ###############################################################################
3
+ # ##################################################################
4
4
terraform {
5
5
required_version = " ~> 1.5"
6
6
@@ -11,32 +11,85 @@ terraform {
11
11
}
12
12
}
13
13
}
14
+ provider "aws" {
15
+ region = var. region
16
+ }
14
17
15
- # ###############################################################################
18
+ # ##################################################################
16
19
# # Load balancer
17
- # ###############################################################################
20
+ # ##################################################################
21
+ resource "aws_security_group" "lb_sg" {
22
+ name = " ${ var . alb . name } -sg"
23
+ description = " Default security group for internet facing ALB"
24
+ vpc_id = var. vpc_id
25
+
26
+ ingress {
27
+ from_port = 80
28
+ to_port = 80
29
+ protocol = " tcp"
30
+ cidr_blocks = [" 0.0.0.0/0" ]
31
+ }
18
32
19
- resource "aws_lb" "this" {
20
- count = var. create_alb ? 1 : 0
33
+ ingress {
34
+ from_port = 443
35
+ to_port = 443
36
+ protocol = " tcp"
37
+ cidr_blocks = [" 0.0.0.0/0" ]
38
+ }
39
+
40
+ egress {
41
+ from_port = 0
42
+ to_port = 0
43
+ protocol = " -1"
44
+ cidr_blocks = [" 0.0.0.0/0" ]
45
+ }
46
+
47
+ tags = {
48
+ Name = " ${ var . alb . name } -sg"
49
+ }
50
+ }
51
+
52
+
53
+ data "aws_subnets" "public" {
54
+ filter {
55
+ name = " vpc-id"
56
+ values = [var . vpc_id ]
57
+ }
58
+
59
+ tags = {
60
+ Type = " public"
61
+ }
62
+ }
21
63
64
+ locals {
65
+ alb_subnets = var. create_alb ? [for subnet in data . aws_subnets . public : subnet . id ] : []
66
+ }
67
+
68
+ resource "aws_lb" "this" {
22
69
name = var. alb . name
23
70
internal = var. alb . internal
24
71
load_balancer_type = var. alb . load_balancer_type
25
72
security_groups = [aws_security_group . lb_sg . id ]
26
- subnets = [ for subnet in aws_subnet . public : subnet . id ]
73
+ subnets = var . alb . subnets
27
74
idle_timeout = var. alb . idle_timeout
28
75
enable_deletion_protection = var. alb . enable_deletion_protection
29
76
enable_http2 = var. alb . enable_http2
30
77
31
- access_logs {
32
- bucket = var. alb . access_logs . bucket
33
- enabled = var. alb . access_logs . enabled
34
- prefix = var. alb . access_logs . prefix
78
+ dynamic "access_logs" {
79
+ for_each = var. alb . access_logs != null ? [var . alb . access_logs ] : []
80
+
81
+ content {
82
+ bucket = access_logs. value . bucket
83
+ enabled = access_logs. value . enabled
84
+ prefix = access_logs. value . prefix
85
+ }
35
86
}
36
87
}
37
88
38
89
90
+ # ##################################################################
39
91
# # Target Group
92
+ # ##################################################################
40
93
41
94
resource "aws_lb_target_group" "this" {
42
95
for_each = { for tg in var . alb_target_group : tg . name => tg }
@@ -66,7 +119,7 @@ resource "aws_lb_target_group" "this" {
66
119
}
67
120
68
121
dynamic "stickiness" {
69
- for_each = each. value . stickiness != null && each . value . stickiness . enabled ? [each . value . stickiness ] : []
122
+ for_each = each. value . stickiness != null ? [each . value . stickiness ] : []
70
123
content {
71
124
cookie_duration = stickiness. value . cookie_duration
72
125
type = stickiness. value . type
@@ -77,51 +130,63 @@ resource "aws_lb_target_group" "this" {
77
130
create_before_destroy = true
78
131
}
79
132
80
- tags = each. value . tags
133
+ tags = each. value . tags
81
134
}
82
135
83
- # Listener
136
+ # ##################################################################
137
+ # # Listener
138
+ # ##################################################################
139
+
84
140
resource "aws_lb_listener" "http" {
85
141
load_balancer_arn = aws_lb. this . arn
86
142
port = var. alb . port
87
143
protocol = var. alb . protocol
88
144
89
- certificate_arn = var. alb . certificate_arn
145
+ certificate_arn = var. alb . certificate_arn
90
146
147
+ # Static "default_action" for forward
148
+ default_action {
149
+ type = " forward"
150
+ target_group_arn = aws_lb_target_group. this [var . alb_target_group [0 ]. name ]. arn
151
+ }
152
+
153
+ # Dynamic "default_action" for variable-driven actions
91
154
dynamic "default_action" {
92
155
for_each = var. listener_rules
156
+
93
157
content {
94
- type = each. value . actions [0 ]. type
95
- target_group_arn = lookup (each. value . actions [0 ], " target_group_arn" , null )
158
+ type = length ( each. value . actions ) > 0 ? each . value . actions [0 ]. type : null
159
+ target_group_arn = length (each . value . actions ) > 0 ? lookup (each. value . actions [0 ], " target_group_arn" , null ) : null
96
160
}
97
161
}
98
162
}
99
163
100
164
165
+
101
166
resource "aws_lb_listener_rule" "this" {
102
167
for_each = var. create_listener_rule ? { for rule in var . listener_rules : " ${ rule . priority } " => rule } : {}
103
168
104
169
listener_arn = aws_lb_listener. http . arn
105
170
priority = each. value . priority
106
171
107
172
dynamic "condition" {
108
- for_each = each. value . conditions
109
- content {
110
- dynamic "host_header" {
111
- for_each = each. value . field == " host-header" ? [each . value ] : []
112
- content {
113
- values = each. value . values
173
+ for_each = each. value . conditions
174
+ content {
175
+ dynamic "host_header" {
176
+ for_each = each. value . field == " host-header" ? [each . value ] : []
177
+ content {
178
+ values = each. value . values
179
+ }
114
180
}
115
- }
116
181
117
- dynamic "path_pattern" {
118
- for_each = each. value . field == " path-pattern" ? [each . value ] : []
119
- content {
120
- values = each. value . values
182
+ dynamic "path_pattern" {
183
+ for_each = each. value . field == " path-pattern" ? [each . value ] : []
184
+ content {
185
+ values = each. value . values
186
+ }
121
187
}
122
188
}
123
189
}
124
- }
125
190
126
191
dynamic "action" {
127
192
for_each = each. value . actions
0 commit comments