-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from binbashar/feature/nfw-module
Change NFW implementation to the module approach
- Loading branch information
Showing
2 changed files
with
57 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,68 @@ | ||
# Firewall | ||
resource "aws_networkfirewall_firewall" "firewall" { | ||
module "firewall" { | ||
|
||
count = var.enable_network_firewall ? 1 : 0 | ||
|
||
name = "${var.project}-${var.environment}-firewall" | ||
firewall_policy_arn = aws_networkfirewall_firewall_policy.policy[0].arn | ||
vpc_id = module.vpc.vpc_id | ||
|
||
# subnet_mapping | ||
dynamic "subnet_mapping" { | ||
for_each = [for k, v in module.network_firewall_private_subnets.az_subnet_ids : | ||
v if contains(local.firewall_endpoints, k) | ||
] | ||
|
||
content { | ||
subnet_id = subnet_mapping.value | ||
} | ||
} | ||
|
||
tags = local.tags | ||
} | ||
|
||
# Policy | ||
resource "aws_networkfirewall_firewall_policy" "policy" { | ||
|
||
count = var.enable_network_firewall ? 1 : 0 | ||
|
||
name = "${var.project}-${var.environment}-firewall-policy" | ||
|
||
firewall_policy { | ||
stateless_default_actions = ["aws:pass"] | ||
stateless_fragment_default_actions = ["aws:drop"] | ||
|
||
stateless_rule_group_reference { | ||
priority = 10 | ||
resource_arn = aws_networkfirewall_rule_group.staless_rule_group[0].arn | ||
} | ||
|
||
stateful_rule_group_reference { | ||
resource_arn = aws_networkfirewall_rule_group.staleful_rule_group[0].arn | ||
} | ||
} | ||
|
||
tags = local.tags | ||
} | ||
|
||
# Stateless rule groups | ||
resource "aws_networkfirewall_rule_group" "staless_rule_group" { | ||
|
||
count = var.enable_network_firewall ? 1 : 0 | ||
|
||
name = "${var.project}-${var.environment}-default-forward" | ||
|
||
description = "Stateless Rule" | ||
capacity = 100 | ||
type = "STATELESS" | ||
rule_group { | ||
rules_source { | ||
stateless_rules_and_custom_actions { | ||
stateless_rule { | ||
source = "github.com/binbashar/terraform-aws-network-firewall.git?ref=v0.1.0" | ||
|
||
name = "${var.project}-${var.environment}-firewall" | ||
|
||
description = "AWS Network Firewall example" | ||
delete_protection = false | ||
firewall_policy_change_protection = false | ||
subnet_change_protection = false | ||
vpc_id = module.vpc.vpc_id | ||
|
||
stateless_default_actions = ["aws:pass"] | ||
stateless_fragment_default_actions = ["aws:drop"] | ||
|
||
subnet_mapping = module.network_firewall_private_subnets.az_subnet_ids | ||
|
||
# Stateless rule groups | ||
stateless_rule_groups = { | ||
# stateless-group-1 rules | ||
staless-group-1 = { | ||
description = "Staless rules" | ||
priority = 10 | ||
capacity = 100 | ||
rules = [ | ||
{ | ||
priority = 1 | ||
actions = ["aws:drop"] | ||
protocols = [1] # ICMP | ||
source = { | ||
address = "0.0.0.0/0" | ||
} | ||
destination = { | ||
address = "0.0.0.0/0" | ||
} | ||
}, | ||
{ | ||
priority = 10 | ||
rule_definition { | ||
actions = ["aws:forward_to_sfe"] | ||
match_attributes { | ||
source { | ||
address_definition = "0.0.0.0/0" | ||
} | ||
#source_port { | ||
# from_port = 0 | ||
# to_port = 0 | ||
#} | ||
destination { | ||
address_definition = "0.0.0.0/0" | ||
} | ||
#destination_port { | ||
# from_port = 0 | ||
# to_port = 0 | ||
#} | ||
} | ||
actions = ["aws:forward_to_sfe"] | ||
source = { | ||
address = "0.0.0.0/0" | ||
} | ||
} | ||
} | ||
destination = { | ||
address = "0.0.0.0/0" | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
|
||
tags = local.tags | ||
} | ||
|
||
# Stateful rule groups | ||
resource "aws_networkfirewall_rule_group" "staleful_rule_group" { | ||
|
||
count = var.enable_network_firewall ? 1 : 0 | ||
|
||
name = "${var.project}-${var.environment}-deny-wikipedia" | ||
capacity = 50 | ||
description = "Deny Wikipedia access" | ||
type = "STATEFUL" | ||
rule_group { | ||
rule_variables { | ||
ip_sets { | ||
key = "HOME_NET" | ||
ip_set { | ||
definition = ["0.0.0.0/0"] | ||
} | ||
|
||
} | ||
} | ||
rules_source { | ||
rules_source_list { | ||
# Stateful rules | ||
stateful_rule_groups = { | ||
# rules_source_list examples | ||
stateful-group-1 = { | ||
description = "Stateful Inspection for denying access to domains" | ||
capacity = 100 | ||
#rule_variables = {} | ||
rules_source_list = { | ||
generated_rules_type = "DENYLIST" | ||
target_types = ["TLS_SNI", "HTTP_HOST"] | ||
targets = [".wikipedia.org"] | ||
targets = [".wikipedia.org", ".bad-domain.com"] | ||
} | ||
} | ||
} | ||
|
||
tags = local.tags | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters