Skip to content

Commit 3695416

Browse files
author
olamide
committed
Enable country selection filter for AWS WAF
1 parent 6b035e5 commit 3695416

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

aws/waf/main.tf

+44-23
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,44 @@ resource "aws_wafv2_web_acl" "main" {
1313
metric_name = "${var.name}-cloudfront-web-acl"
1414
}
1515

16-
rule {
17-
name = "${var.name}-IP-Ratelimit"
18-
priority = var.rate_limit["Priority"]
16+
dynamic "rule" {
17+
for_each = var.rate_limit
18+
content {
19+
name = "${rule.value["name"]}-IP-Ratelimit"
20+
priority = rule.value["priority"]
1921

20-
dynamic "action" {
21-
for_each = var.rate_limit["count_override"] == true ? [1] : []
22-
content {
23-
count {}
22+
dynamic "action" {
23+
for_each = rule.value["count_override"] == true ? [1] : []
24+
content {
25+
count {}
26+
}
2427
}
25-
}
26-
dynamic "action" {
27-
for_each = var.rate_limit["count_override"] == false ? [1] : []
28-
content {
29-
block {}
28+
dynamic "action" {
29+
for_each = rule.value["count_override"] == false ? [1] : []
30+
content {
31+
block {}
32+
}
3033
}
31-
}
32-
33-
statement {
34-
rate_based_statement {
35-
limit = var.rate_limit["Limit"]
36-
aggregate_key_type = "IP"
34+
statement {
35+
rate_based_statement {
36+
limit = rule.value["Limit"]
37+
aggregate_key_type = "IP"
38+
39+
dynamic "scope_down_statement" {
40+
for_each = length(rule.value["country_list"]) > 0 ? [1] : []
41+
content {
42+
geo_match_statement {
43+
country_codes = rule.value["country_list"]
44+
}
45+
}
46+
}
47+
}
48+
}
49+
visibility_config {
50+
cloudwatch_metrics_enabled = true
51+
sampled_requests_enabled = true
52+
metric_name = "${rule.value["name"]}-IP-Ratelimit"
3753
}
38-
}
39-
visibility_config {
40-
cloudwatch_metrics_enabled = true
41-
sampled_requests_enabled = true
42-
metric_name = "${var.name}-IP-Ratelimit"
4354
}
4455
}
4556

@@ -105,8 +116,18 @@ resource "aws_wafv2_web_acl" "main" {
105116
managed_rule_group_statement {
106117
name = rule.value["name"]
107118
vendor_name = "AWS"
119+
120+
dynamic "scope_down_statement" {
121+
for_each = length(rule.value["country_list"]) > 0 ? [1] : []
122+
content {
123+
geo_match_statement {
124+
country_codes = rule.value["country_list"]
125+
}
126+
}
127+
}
108128
}
109129
}
130+
110131
visibility_config {
111132
cloudwatch_metrics_enabled = true
112133
sampled_requests_enabled = true

0 commit comments

Comments
 (0)