forked from aws-samples/aws-waf-firewall-manager-terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3-aws-waf-ip.tf
104 lines (90 loc) · 2.84 KB
/
3-aws-waf-ip.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
#
# Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
# the License.
#
#-----use this resource to create your own IP Set and AWS WAF IP Rule ----------------------------
# Not part of AWS FM WAF policy by default
resource "aws_wafv2_ip_set" "AllowListsSet" {
provider = aws.regional
for_each = var.allow_lists_set
name = "${var.regional_policy_name}-Allowt_${var.allow_lists_set[each.key]["ip_address_version"]}"
scope = "REGIONAL"
ip_address_version = var.allow_lists_set[each.key]["ip_address_version"]
addresses = var.allow_lists_set[each.key]["addresses"]
tags = local.common_tags
}
resource "aws_wafv2_ip_set" "BlockListsSet" {
provider = aws.regional
for_each = var.block_lists_set
name = "${var.regional_policy_name}-Block_${var.block_lists_set[each.key]["ip_address_version"]}"
scope = "REGIONAL"
ip_address_version = var.block_lists_set[each.key]["ip_address_version"]
addresses = var.block_lists_set[each.key]["addresses"]
tags = local.common_tags
}
# Not part of AWS FM WAF policy by default
resource "aws_wafv2_rule_group" "IPRuleGroup" {
count = var.create_waf_ip_rule_group ? 1 : 0
provider = aws.regional
name = "${var.regional_policy_name}-IP"
scope = "REGIONAL"
capacity = 60
description = "IP Rule Group"
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "WAFCUSTIPRuleGroupMetric"
sampled_requests_enabled = true
}
rule {
name = "IPAllowListsRule"
priority = 3
action {
allow {}
}
statement {
or_statement {
dynamic "statement" {
for_each = aws_wafv2_ip_set.AllowListsSet
content {
ip_set_reference_statement {
arn = aws_wafv2_ip_set.AllowListsSet[statement.key].arn
}
}
}
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "IPAllowListsRule-metric"
sampled_requests_enabled = true
}
}
rule {
name = "IPBlockListsRule"
priority = 1
action {
block {}
}
statement {
or_statement {
dynamic "statement" {
for_each = aws_wafv2_ip_set.BlockListsSet
content {
ip_set_reference_statement {
arn = aws_wafv2_ip_set.BlockListsSet[statement.key].arn
}
}
}
}
}
visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "IPBlockListsRule-metric"
sampled_requests_enabled = true
}
}
}