-
Notifications
You must be signed in to change notification settings - Fork 6
/
loadbalancer.tf
314 lines (271 loc) · 7.14 KB
/
loadbalancer.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
resource "aws_security_group" "alb" {
name = "ar-io-lb-${var.alias}-sg"
vpc_id = var.vpc_id
ingress {
description = "HTTPS"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTPS"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_lb" "alb" {
name = "ar-io-${var.alias}-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb.id]
subnets = var.public_subnets
drop_invalid_header_fields = true
}
resource "aws_lb_target_group" "ar_io_nodes_tg" {
name = "ar-io-nodes-${var.alias}-tg"
port = 3000
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "instance"
health_check {
matcher = "200-299"
path = "/ar-io/healthcheck"
timeout = 4
interval = 20
healthy_threshold = 2
unhealthy_threshold = 2
}
load_balancing_algorithm_type = "least_outstanding_requests"
}
resource "aws_lb_listener" "alb_http_listener" {
load_balancer_arn = aws_lb.alb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
lifecycle {
ignore_changes = [default_action]
}
}
resource "aws_lb_listener" "alb_https_listener" {
load_balancer_arn = aws_lb.alb.arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-Res-2021-06"
certificate_arn = aws_acm_certificate.alb.arn
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "Access denied"
status_code = "403"
}
}
}
resource "aws_lb_listener_rule" "alb_https_listener_general_rule" {
listener_arn = aws_lb_listener.alb_https_listener.arn
priority = 120
action {
type = "forward"
target_group_arn = aws_lb_target_group.ar_io_nodes_tg.arn
}
condition {
http_header {
http_header_name = "X-Origin-Secret"
values = [random_password.ar_io_cf.result]
}
}
}
resource "aws_wafv2_ip_set" "graphql_ip_allow_list" {
name = "graphql-allow-list"
description = "IP set which wont ever be rate-limited on /graphql"
scope = "REGIONAL"
ip_address_version = "IPV4"
addresses = [for ip in var.graphql_ip_allow_list : "${ip}/32"]
}
resource "aws_wafv2_web_acl" "alb_waf" {
name = "ar-io-${var.alias}-rules"
description = "Legacy gateway rules."
scope = "REGIONAL"
default_action {
allow {}
}
rule {
name = "aws-known-bad-inputs-rule"
priority = 0
override_action {
none {}
}
statement {
managed_rule_group_statement {
name = "AWSManagedRulesKnownBadInputsRuleSet"
vendor_name = "AWS"
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "ar-io-${var.alias}-aws-known-bad-inputs-rule"
sampled_requests_enabled = true
}
}
rule {
name = "aws-ip-reputation-rule"
priority = 2
override_action {
none {}
}
statement {
managed_rule_group_statement {
name = "AWSManagedRulesAmazonIpReputationList"
vendor_name = "AWS"
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "ar-io-${var.alias}-aws-ip-reputation-rule"
sampled_requests_enabled = true
}
}
rule {
name = "tx-header-rate-limit-rule"
priority = 10
action {
block {
custom_response {
response_code = 429
}
}
}
statement {
rate_based_statement {
limit = 4500 # 15/sec
aggregate_key_type = "FORWARDED_IP"
forwarded_ip_config {
header_name = "X-Forwarded-For"
fallback_behavior = "NO_MATCH"
}
scope_down_statement {
byte_match_statement {
field_to_match {
uri_path {}
}
positional_constraint = "STARTS_WITH"
search_string = "/tx/"
text_transformation {
priority = 0
type = "NONE"
}
}
}
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "ar-io-${var.alias}-header-rate-limit-rule"
sampled_requests_enabled = true
}
}
rule {
name = "graphql-rate-limit-rule"
priority = 11
action {
block {
custom_response {
response_code = 429
}
}
}
statement {
rate_based_statement {
limit = 600 # 2/sec
aggregate_key_type = "FORWARDED_IP"
forwarded_ip_config {
header_name = "X-Forwarded-For"
fallback_behavior = "NO_MATCH"
}
scope_down_statement {
and_statement {
statement {
byte_match_statement {
field_to_match {
uri_path {}
}
positional_constraint = "STARTS_WITH"
search_string = "/graphql"
text_transformation {
priority = 0
type = "NONE"
}
}
}
statement {
not_statement {
statement {
ip_set_reference_statement {
arn = aws_wafv2_ip_set.graphql_ip_allow_list.arn
ip_set_forwarded_ip_config {
header_name = "X-Forwarded-For"
fallback_behavior = "NO_MATCH"
position = "FIRST"
}
}
}
}
}
}
}
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "ar-io-${var.alias}-graphql-rate-limit-rule"
sampled_requests_enabled = true
}
}
rule {
name = "global-rate-limit-rule"
priority = 12
action {
block {
custom_response {
response_code = 429
}
}
}
statement {
rate_based_statement {
limit = 30000 # 100/sec
aggregate_key_type = "FORWARDED_IP"
forwarded_ip_config {
header_name = "X-Forwarded-For"
fallback_behavior = "NO_MATCH"
}
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "ar-io-${var.alias}-global-rate-limit-rule"
sampled_requests_enabled = true
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "ar-io-${var.alias}-rules"
sampled_requests_enabled = true
}
}