|
| 1 | +locals { |
| 2 | + name = var.name |
| 3 | + tags = var.tags |
| 4 | +} |
| 5 | + |
| 6 | +# AWS WAFv2 Web ACL |
| 7 | +resource "aws_wafv2_web_acl" "main" { |
| 8 | + name = "${local.name}-web-acl" |
| 9 | + description = "WAF Web ACL for ${local.name}" |
| 10 | + scope = "REGIONAL" |
| 11 | + |
| 12 | + default_action { |
| 13 | + allow {} |
| 14 | + } |
| 15 | + |
| 16 | + # AWS Managed Rules - Core Rule Set |
| 17 | + rule { |
| 18 | + name = "AWSManagedRulesCommonRuleSet" |
| 19 | + priority = 1 |
| 20 | + |
| 21 | + override_action { |
| 22 | + none {} |
| 23 | + } |
| 24 | + |
| 25 | + statement { |
| 26 | + managed_rule_group_statement { |
| 27 | + name = "AWSManagedRulesCommonRuleSet" |
| 28 | + vendor_name = "AWS" |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + visibility_config { |
| 33 | + cloudwatch_metrics_enabled = true |
| 34 | + metric_name = "AWSManagedRulesCommonRuleSetMetric" |
| 35 | + sampled_requests_enabled = true |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + # AWS Managed Rules - Known Bad Inputs |
| 40 | + rule { |
| 41 | + name = "AWSManagedRulesKnownBadInputsRuleSet" |
| 42 | + priority = 2 |
| 43 | + |
| 44 | + override_action { |
| 45 | + none {} |
| 46 | + } |
| 47 | + |
| 48 | + statement { |
| 49 | + managed_rule_group_statement { |
| 50 | + name = "AWSManagedRulesKnownBadInputsRuleSet" |
| 51 | + vendor_name = "AWS" |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + visibility_config { |
| 56 | + cloudwatch_metrics_enabled = true |
| 57 | + metric_name = "AWSManagedRulesKnownBadInputsRuleSetMetric" |
| 58 | + sampled_requests_enabled = true |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + # Rate Limiting Rule |
| 63 | + rule { |
| 64 | + name = "RateLimitRule" |
| 65 | + priority = 3 |
| 66 | + |
| 67 | + action { |
| 68 | + block {} |
| 69 | + } |
| 70 | + |
| 71 | + statement { |
| 72 | + rate_based_statement { |
| 73 | + limit = var.rate_limit_requests_per_5_minutes |
| 74 | + aggregate_key_type = "IP" |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + visibility_config { |
| 79 | + cloudwatch_metrics_enabled = true |
| 80 | + metric_name = "RateLimitRuleMetric" |
| 81 | + sampled_requests_enabled = true |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + # Geo Restriction (if enabled) |
| 86 | + dynamic "rule" { |
| 87 | + for_each = length(var.geo_restriction_countries) > 0 ? [1] : [] |
| 88 | + content { |
| 89 | + name = "GeoRestrictionRule" |
| 90 | + priority = 4 |
| 91 | + |
| 92 | + action { |
| 93 | + block {} |
| 94 | + } |
| 95 | + |
| 96 | + statement { |
| 97 | + geo_match_statement { |
| 98 | + country_codes = var.geo_restriction_countries |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + visibility_config { |
| 103 | + cloudwatch_metrics_enabled = true |
| 104 | + metric_name = "GeoRestrictionRuleMetric" |
| 105 | + sampled_requests_enabled = true |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + # IP Rate Limiting for specific paths (API protection) |
| 111 | + rule { |
| 112 | + name = "APIRateLimitRule" |
| 113 | + priority = 5 |
| 114 | + |
| 115 | + action { |
| 116 | + block {} |
| 117 | + } |
| 118 | + |
| 119 | + statement { |
| 120 | + rate_based_statement { |
| 121 | + limit = var.api_rate_limit_requests_per_5_minutes |
| 122 | + aggregate_key_type = "IP" |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + visibility_config { |
| 127 | + cloudwatch_metrics_enabled = true |
| 128 | + metric_name = "APIRateLimitRuleMetric" |
| 129 | + sampled_requests_enabled = true |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + # SQL Injection Protection |
| 134 | + rule { |
| 135 | + name = "AWSManagedRulesSQLiRuleSet" |
| 136 | + priority = 6 |
| 137 | + |
| 138 | + override_action { |
| 139 | + none {} |
| 140 | + } |
| 141 | + |
| 142 | + statement { |
| 143 | + managed_rule_group_statement { |
| 144 | + name = "AWSManagedRulesSQLiRuleSet" |
| 145 | + vendor_name = "AWS" |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + visibility_config { |
| 150 | + cloudwatch_metrics_enabled = true |
| 151 | + metric_name = "AWSManagedRulesSQLiRuleSetMetric" |
| 152 | + sampled_requests_enabled = true |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + # XSS Protection |
| 157 | + rule { |
| 158 | + name = "AWSManagedRulesAnonymousIpList" |
| 159 | + priority = 7 |
| 160 | + |
| 161 | + override_action { |
| 162 | + none {} |
| 163 | + } |
| 164 | + |
| 165 | + statement { |
| 166 | + managed_rule_group_statement { |
| 167 | + name = "AWSManagedRulesAnonymousIpList" |
| 168 | + vendor_name = "AWS" |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + visibility_config { |
| 173 | + cloudwatch_metrics_enabled = true |
| 174 | + metric_name = "AWSManagedRulesAnonymousIpListMetric" |
| 175 | + sampled_requests_enabled = true |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + visibility_config { |
| 180 | + cloudwatch_metrics_enabled = true |
| 181 | + metric_name = "${local.name}WebACLMetric" |
| 182 | + sampled_requests_enabled = true |
| 183 | + } |
| 184 | + |
| 185 | + tags = local.tags |
| 186 | +} |
| 187 | + |
| 188 | +# WAF Logging Configuration (simplified - just CloudWatch) |
| 189 | +resource "aws_cloudwatch_log_group" "waf_logs" { |
| 190 | + count = var.enable_logging ? 1 : 0 |
| 191 | + name = "/aws/waf/${local.name}" |
| 192 | + retention_in_days = var.log_retention_days |
| 193 | + |
| 194 | + tags = local.tags |
| 195 | +} |
0 commit comments