├── .gitignore
├── .header.md
├── .pre-commit-config.yaml
├── .terraform-docs.yaml
├── .tflint.hcl
├── .tfsec
├── launch_configuration_imdsv2_tfchecks.json
├── launch_template_imdsv2_tfchecks.json
├── no_launch_config_tfchecks.json
├── sg_no_embedded_egress_rules_tfchecks.json
└── sg_no_embedded_ingress_rules_tfchecks.json
├── CODEOWNERS
├── LICENSE
├── NOTICE.txt
├── README.md
├── docs
└── UPGRADE-GUIDE-1.0.md
├── examples
├── central_inspection_with_egress
│ ├── .header.md
│ ├── .terraform-docs.yaml
│ ├── README.md
│ ├── main.tf
│ ├── outputs.tf
│ ├── policy.tf
│ ├── providers.tf
│ └── variables.tf
├── central_inspection_without_egress
│ ├── .header.md
│ ├── .terraform-docs.yaml
│ ├── README.md
│ ├── main.tf
│ ├── outputs.tf
│ ├── policy.tf
│ ├── providers.tf
│ └── variables.tf
├── intra_vpc_inspection
│ ├── .header.md
│ ├── .terraform-docs.yaml
│ ├── README.md
│ ├── main.tf
│ ├── outputs.tf
│ ├── policy.tf
│ ├── providers.tf
│ └── variables.tf
└── single_vpc_logging
│ ├── .header.md
│ ├── .terraform-docs.yaml
│ ├── README.md
│ ├── kms_keys.tf
│ ├── main.tf
│ ├── outputs.tf
│ ├── policy.tf
│ ├── providers.tf
│ └── variables.tf
├── main.tf
├── modules
├── central_inspection_with_egress_routing
│ ├── main.tf
│ ├── outputs.tf
│ ├── providers.tf
│ └── variables.tf
├── intra_vpc_routing
│ ├── main.tf
│ ├── outputs.tf
│ ├── providers.tf
│ └── variables.tf
└── logging
│ ├── main.tf
│ ├── outputs.tf
│ ├── providers.tf
│ └── variables.tf
├── moved.tf
├── outputs.tf
├── providers.tf
├── test
├── examples_central_inspection_with_egress_test.go
├── examples_central_inspection_without_egress_test.go
├── examples_intra_vpc_inspection_test.go
└── examples_single_vpc_logging_test.go
└── variables.tf
/.gitignore:
--------------------------------------------------------------------------------
1 | build/
2 | plan.out
3 | plan.out.json
4 |
5 | # Local .terraform directories
6 | .terraform/
7 |
8 | # .tfstate files
9 | *.tfstate
10 | *.tfstate.*
11 |
12 | # Crash log files
13 | crash.log
14 |
15 | # Exclude all .tfvars files, which are likely to contain sentitive data, such as
16 | # password, private keys, and other secrets. These should not be part of version
17 | # control as they are data points which are potentially sensitive and subject
18 | # to change depending on the environment.
19 | #
20 | *.tfvars
21 |
22 | # Ignore override files as they are usually used to override resources locally and so
23 | # are not checked in
24 | override.tf
25 | override.tf.json
26 | *_override.tf
27 | *_override.tf.json
28 |
29 | # Include override files you do wish to add to version control using negated pattern
30 | #
31 | # !example_override.tf
32 |
33 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
34 | # example: *tfplan*
35 |
36 | # Ignore CLI configuration files
37 | .terraformrc
38 | terraform.rc
39 | .terraform.lock.hcl
40 |
41 | go.mod
42 | go.sum
43 |
44 | # DS_Store
45 |
46 | .DS_Store
47 |
--------------------------------------------------------------------------------
/.header.md:
--------------------------------------------------------------------------------
1 | # AWS Network Firewall Module
2 |
3 | *NOTE*: For information regarding the 1.0 upgrade see our [upgrade guide](./docs/UPGRADE-GUIDE-1.0.md)
4 |
5 | [AWS Network Firewall](https://docs.aws.amazon.com/network-firewall/latest/developerguide/what-is-aws-network-firewall.html) is a managed network security service that makes it easy to deploy threat prevention for Amazon VPCs. This module can be used to deploy an AWS Network Firewall resource in the desired VPC, automating all the routing and logging configuration when the resource is deployed.
6 |
7 | The module only handles the creation of the infrastructure, leaving full freedom to the user when defining the firewall rules (which should be done outside the module). Same applies to IAM roles and KMS keys when you define the firewall logging - rememeber that it is a best practice to encryt at rest your firewall logs.
8 |
9 | ## Usage
10 |
11 | To create AWS Network Firewall in your VPC, you need to provide the following information:
12 |
13 | - `network_firewall_name` = (Required|string) Name to provide the AWS Network Firewall resource.
14 | - `network_firewall_description` = (Required|string) A friendly description of the firewall resource.
15 | - `network_firewall_policy`= (Required|string) ARN of the firewall policy to apply in the AWS Network Firewall resource. Check the definition of [AWS Network Firewall Policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/networkfirewall_firewall_policy) and [AWS Network Firewall Rule Groups](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/networkfirewall_rule_group) to see how you can create firewall policies.
16 | - `network_firewall_delete_protection` = (Optional|bool) A boolean flag indicating whether it is possible to delete the firewall. Defaults to `false`.
17 | - `network_firewall_policy_change_protection` = (Optional|bool) To indicate whether it is possible to change the associated firewall policy after creation. Defaults to `false`.
18 | - `network_firewall_subnet_change_protection` = (Optional|bool) To indicate whether it is possible to change the associated subnet(s) after creation. Defaults to `false`.
19 | - `network_firewall_encryption_key_arn` = (Optional|string) ARN of Customer managed KMS key for data encryption. By default, AWS managed key will be used.
20 | - `vpc_id` = (Required|string) ID of the VPC where the AWS Network Firewall resource should be placed.
21 | - `vpc_subnets` = (Required|map(string)) Map of subnet IDs to place the Network Firewall endpoints. The expected format of the map is the Availability Zone as key, and the ID of the subnet as value. Example (supposing us-east-1 as AWS Region):
22 |
23 | ```hcl
24 | vpc_subnets = {
25 | us-east-1a = subnet-IDa
26 | us-east-1b = subnet-IDb
27 | us-east-1c = subnet-IDc
28 | }
29 | ```
30 |
31 | - `number_azs` = (Required|number) Number of Availability Zones to place the AWS Network Firewall endpoints.
32 | - `routing_configuration` = (Required|any) Configuration of the routing desired in the VPC. Depending the VPC type, the information to provide is different. The configuration types supported are: `single_vpc`, `intra_vpc_inspection`, `centralized_inspection_without_egress`, and `centralized_inspection_with_egress`. **Only one key (option) can be defined**. More information about the differences between each of the VPC types (and examples) can be checked in the section below.
33 | - `tags`= (Optional|map(string)) List of tags to apply to the AWS Network Firewall resource.
34 |
35 | ## Routing configuration
36 |
37 | Once the AWS Network Firewall resource is created, the routing to the firewall endpoints need to be created. However, depending the VPC and how we want to inspect the traffic, this routing configuration is going to be different. The module supports the routing configuration of 4 different types of VPCs, covering the most common [Inspection Deployment models with with AWS Network Firewall](https://d1.awsstatic.com/architecture-diagrams/ArchitectureDiagrams/inspection-deployment-models-with-AWS-network-firewall-ra.pdf).
38 |
39 | ### Single VPC
40 |
41 | The first use case is when the firewall endpoints are located in the VPC to inspect the traffic from/to workloads in that same VPC. When using this routing configuration, it is expected to place the firewall endpoints in subnets between the Internet gateway (IGW) and the public subnets (where you can place the Elastic Load Balancers and NAT gateways). The module expects in this configuration three variables:
42 |
43 | - `igw_route_table` = (Required|string) VPC route table ID associated to an Internet gateway. In this route table it will be created routes pointing to the Network Firewall endpoints with destination the CIDR blocks provided in the `protected_subnet_cidr_blocks` variable.
44 | - `protected_subnet_route_tables` = (Required|map(string)) Map of the VPC subnet route tables where the resources to protect (using the Network Firewall resource) are located - expected format of the map is Availability Zone (key) --> VPC route table (value). In these route tables it will be created the default route table pointing to the Network Firewall endpoints - ensuring Availability Zone affinity.
45 | - `protected_subnet_cidr_blocks` = (Required|map(string)) Map of IPv4 CIDR blocks indicating the subnets where the resources to protect (using the Network Firewall resource) are located - expected format of the map is Availability Zone (key) --> IPv4 CIDR block (value).
46 |
47 | An example of the definition of this routing configuration is the following one:
48 |
49 | ```hcl
50 | routing_configuration = {
51 | single_vpc = {
52 | igw_route_table = rtb-ID
53 | protected_subnet_route_tables = {
54 | us-east-1a = rtb-IDa
55 | us-east-1b = rtb-IDb
56 | us-east-1c = rtb-IDc
57 | }
58 | protected_subnet_cidr_blocks = {
59 | us-east-1a = "10.0.0.0/24"
60 | us-east-1b = "10.0.1.0/24"
61 | us-east-1c = "10.0.2.0/24"
62 | }
63 | }
64 | }
65 | ```
66 |
67 | ### Intra-VPC Inspection
68 |
69 | When placing firewall endpoints to inspect traffic between workloads inside the same VPC (between your EC2 instances and the database layer, for example) you can take advantage of the VPC routing enhacement - which allows you to include more specific routing than the local one. The module expects in this configuration two variables:
70 |
71 | - `number_routes` = (Required|number) Number of configured items in the `routes` variable.
72 | - `routes` = (Required|list(map(any))) List of intra-VPC route configurations. Important to note that only one direction is configured per item in this list, so in most situations you will need two items per group of subnets to inspect. Each item expects a map of strings with two values:
73 | - `source_subnet_route_tables` = (Required|map(string)) VPC route table of the source subnet - expected format of the map is Availability Zone (key) --> VPC route table (value)
74 | - `destination_subnet_cidr_blocks` = (Required|map(string)). IPv4 CIDR blocks of the destination subnet - expected format of the map is Availability Zone (key) --> IPv4 CIDR block (value)
75 |
76 | An example of the definition of this routing configuration is the following one.
77 |
78 | ```hcl
79 | routing_configuration = {
80 | intra_vpc_inspection = {
81 | number_routes = 2
82 | routes = {
83 | {
84 | source_subnet_route_tables = {
85 | us-east-1a = rtb-IDa
86 | us-east-1b = rtb-IDb
87 | us-east-1c = rtb-IDc
88 | }
89 | destination_subnet_cidr_blocks = {
90 | us-east-1a = "10.0.0.0/24"
91 | us-east-1b = "10.0.1.0/24"
92 | us-east-1c = "10.0.2.0/24"
93 | }
94 | },
95 | {
96 | source_subnet_route_tables = {
97 | us-east-1a = rtb-IDaa
98 | us-east-1b = rtb-IDbb
99 | us-east-1c = rtb-IDcc
100 | }
101 | destination_subnet_cidr_blocks = {
102 | us-east-1a = "10.0.3.0/24"
103 | us-east-1b = "10.0.4.0/24"
104 | us-east-1c = "10.0.5.0/24"
105 | }
106 | }
107 | }
108 | }
109 | }
110 | ```
111 |
112 | ### Hub and Spoke with Inspection VPC
113 |
114 | The use case covers the creation of a centralized Inspection VPC in a hub-and-spoke architecture with [AWS Transit Gateway](https://docs.aws.amazon.com/vpc/latest/tgw/what-is-transit-gateway.html) and/or [AWS Cloud WAN](https://docs.aws.amazon.com/network-manager/latest/cloudwan/what-is-cloudwan.html), with the idea of managing the traffic inspection at scale. When using the key `centralized_inspection_without_egress` it is supposed that the Inspection VPC created is only used to place the Transit Gateway or Cloud WAN's core network ENIs and the firewall endpoints. In this configuration, you can configurate the following variables:
115 |
116 | - `connectivity_subnet_route_tables` = (Optional|map(string)) Map of VPC subnet route tables where the Transit Gateway or Cloud WAN's core network ENIs are located. In these route tables a default route pointing to the Network Firewall endpoints is created - expected format of the map is Availability Zone (key) --> VPC route table (value)
117 |
118 | An example of the definition of this routing configuration is the following one:
119 |
120 | ```hcl
121 | routing_configuration = {
122 | centralized_inspection_without_egress = {
123 | connectivity_subnet_route_tables = {
124 | us-east-1a = rtb-IDa
125 | us-east-1b = rtb-IDb
126 | us-east-1c = rtb-IDc
127 | }
128 | }
129 | }
130 | ```
131 |
132 | ### Hub and Spoke with Inspection VPC (with egress traffic)
133 |
134 | The use case covers the creation of a centralized Inspection VPC in a hub-and-spoke architecture with [AWS Transit Gateway](https://docs.aws.amazon.com/vpc/latest/tgw/what-is-transit-gateway.html) or [AWS Cloud WAN](https://docs.aws.amazon.com/network-manager/latest/cloudwan/what-is-cloudwan.html), with the idea of managing the traffic inspection at scale. When using the key `centralized_inspection_with_egress` it is supposed that the Inspection VPC also has access to the Internet, to centralize inspection and egress traffic at the same time. In this configuration, you can configurate the following variables:
135 |
136 | - `connectivity_subnet_route_tables` = (Optional|map(string)) Map of VPC subnet route tables where the Transit Gateway or Cloud WAN's core network ENIs are located. In these route tables a default route pointing to the Network Firewall endpoints is created - expected format of the map is Availability Zone (key) --> VPC route table (value)
137 | - `public_subnet_route_tables` = (Required|map(string)) Map of VPC public subnet route tables. In these route tables, routes are created pointing to the Network Firewall endpoints with destination the CIDR blocks defined in the `network_cidr_blocks` variable. The expected format of the map is Availability Zone (key) --> VPC route table (value)
138 | - `network_cidr_blocks` = (Required|list(string)) List of IPv4 CIDR blocks defining the AWS network.
139 |
140 | An example of the definition of this routing configuration is the following one:
141 |
142 | ```hcl
143 | routing_configuration = {
144 | centralized_inspection_with_egress = {
145 | connectivity_subnet_route_tables = {
146 | us-east-1a = rtb-IDa
147 | us-east-1b = rtb-IDb
148 | us-east-1c = rtb-IDc
149 | }
150 | public_subnet_route_tables = {
151 | us-east-1a = rtb-IDaa
152 | us-east-1b = rtb-IDbb
153 | us-east-1c = rtb-IDcc
154 | }
155 | network_cidr_blocks = ["10.0.0.0/8", "192.168.0.0/24"]
156 | }
157 | }
158 | ```
159 |
160 | ## Logging
161 |
162 | You can enable AWS Network Firewall logging for the [stateful engine](https://docs.aws.amazon.com/network-firewall/latest/developerguide/firewall-rules-engines.html). You can record the flow logs and/or alert logs, with only one destination per log type:
163 |
164 | * [Amazon S3](https://aws.amazon.com/s3/) bucket.
165 | * [Amazon CloudWatch](https://aws.amazon.com/cloudwatch/) log group.
166 | * [Amazon Kinesis Data Firehose](https://aws.amazon.com/kinesis/data-firehose/) stream.
167 |
168 | For more information about the logging in AWS Network Firewall, check the [AWS Network Firewall documentation](https://docs.aws.amazon.com/network-firewall/latest/developerguide/firewall-logging.html).
169 |
170 | Regarding the format of the variable, you can define either `flow_log` or `alert_log` configurations; with only 1 destination in each one of them. As mentioned above, 3 different destinations can be configured:
171 |
172 | - `s3_bucket` = (Optional|map(string)) Configuration of an S3 bucket as logging destination. Two attributes are expected:
173 | - `bucketName` = (Required|string) Name of the S3 bucket to deliver the logs.
174 | - `logPrefix` = (Optional|string) Path inside the S3 bucket.
175 | - `cloudwatch_logs` = (Optional|map(string)) Configuration of a CloudWatch log group as logging destination. Only the attribute `logGroupName` (Required|string) is expected.
176 | - `kinesis_firehose` = (Optional|map(string)) Configuration of a Kinesis Data Firehose stream as logging desintation. Only the attribute `deliveryStreamName` (Required|string) is expected.
177 |
178 | Example definition of each type:
179 |
180 | ```hcl
181 | logging_configuration = {
182 | flow_log = {
183 | s3_bucket = {
184 | bucketName = "my-bucket"
185 | logPrefix = "/logs"
186 | }
187 | }
188 | }
189 |
190 | logging_configuration = {
191 | alert_log = {
192 | cloudwatch_logs = {
193 | logGroupName = "my-log-group"
194 | }
195 | }
196 | }
197 |
198 | logging_configuration = {
199 | alert_log = {
200 | kinesis_firehose = {
201 | deliveryStreamName = "my-stream"
202 | }
203 | }
204 | }
205 | ```
206 |
207 | ## References
208 |
209 | - Reference Architecture: [Inspection Deployment models with with AWS Network Firewall](https://d1.awsstatic.com/architecture-diagrams/ArchitectureDiagrams/inspection-deployment-models-with-AWS-network-firewall-ra.pdf)
210 | - Blog post: [Deployment models for AWS Network Firewall](https://aws.amazon.com/blogs/networking-and-content-delivery/deployment-models-for-aws-network-firewall/)
211 | - Blog post: [Deployment models for AWS Network Firewall with VPC routing enhancements](https://aws.amazon.com/blogs/networking-and-content-delivery/deployment-models-for-aws-network-firewall-with-vpc-routing-enhancements/)
212 |
--------------------------------------------------------------------------------
/.pre-commit-config.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | fail_fast: false
3 | minimum_pre_commit_version: "2.6.0"
4 | repos:
5 | -
6 | repo: https://github.com/aws-ia/pre-commit-configs
7 | # To update run:
8 | # pre-commit autoupdate --freeze
9 | rev: 80ed3f0a164f282afaac0b6aec70e20f7e541932 # frozen: v1.5.0
10 | hooks:
11 | - id: aws-ia-meta-hook
12 |
--------------------------------------------------------------------------------
/.terraform-docs.yaml:
--------------------------------------------------------------------------------
1 | formatter: markdown
2 | header-from: .header.md
3 | settings:
4 | anchor: true
5 | color: true
6 | default: true
7 | escape: true
8 | html: true
9 | indent: 2
10 | required: true
11 | sensitive: true
12 | type: true
13 |
14 | sort:
15 | enabled: true
16 | by: required
17 |
18 | output:
19 | file: README.md
20 | mode: replace
21 |
--------------------------------------------------------------------------------
/.tflint.hcl:
--------------------------------------------------------------------------------
1 | # https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/module-inspection.md
2 | # borrowed & modified indefinitely from https://github.com/ksatirli/building-infrastructure-you-can-mostly-trust/blob/main/.tflint.hcl
3 |
4 | plugin "aws" {
5 | enabled = true
6 | version = "0.21.1"
7 | source = "github.com/terraform-linters/tflint-ruleset-aws"
8 | }
9 |
10 | config {
11 | module = false
12 | force = false
13 | }
14 |
15 | rule "terraform_required_providers" {
16 | enabled = true
17 | }
18 |
19 | rule "terraform_required_version" {
20 | enabled = true
21 | }
22 |
23 | rule "terraform_naming_convention" {
24 | enabled = true
25 | format = "snake_case"
26 | }
27 |
28 | rule "terraform_typed_variables" {
29 | enabled = true
30 | }
31 |
32 | rule "terraform_unused_declarations" {
33 | enabled = true
34 | }
35 |
36 | rule "terraform_comment_syntax" {
37 | enabled = true
38 | }
39 |
40 | rule "terraform_deprecated_index" {
41 | enabled = true
42 | }
43 |
44 | rule "terraform_deprecated_interpolation" {
45 | enabled = true
46 | }
47 |
48 | rule "terraform_documented_outputs" {
49 | enabled = true
50 | }
51 |
52 | rule "terraform_documented_variables" {
53 | enabled = true
54 | }
55 |
56 | rule "terraform_module_pinned_source" {
57 | enabled = true
58 | }
59 |
60 | rule "terraform_standard_module_structure" {
61 | enabled = true
62 | }
63 |
64 | rule "terraform_workspace_remote" {
65 | enabled = true
66 | }
67 |
68 | # seems to be a bug when a resource is not created
69 | rule "aws_route_not_specified_target" {
70 | enabled = false
71 | }
72 |
73 | rule "aws_route_specified_multiple_targets" {
74 | enabled = false
75 | }
76 |
--------------------------------------------------------------------------------
/.tfsec/launch_configuration_imdsv2_tfchecks.json:
--------------------------------------------------------------------------------
1 | {
2 | "checks": [
3 | {
4 | "code": "CUS002",
5 | "description": "Check to IMDSv2 is required on EC2 instances created by this Launch Template",
6 | "impact": "Instance metadata service can be interacted with freely",
7 | "resolution": "Enable HTTP token requirement for IMDS",
8 | "requiredTypes": [
9 | "resource"
10 | ],
11 | "requiredLabels": [
12 | "aws_launch_configuration"
13 | ],
14 | "severity": "CRITICAL",
15 | "matchSpec": {
16 | "action": "isPresent",
17 | "name": "metadata_options",
18 | "subMatch": {
19 | "action": "and",
20 | "predicateMatchSpec": [
21 | {
22 | "action": "equals",
23 | "name": "http_tokens",
24 | "value": "required"
25 |
26 | }
27 | ]
28 | }
29 | },
30 |
31 | "errorMessage": "is missing `metadata_options` block - it is required with `http_tokens` set to `required` to make Instance Metadata Service more secure.",
32 | "relatedLinks": [
33 | "https://tfsec.dev/docs/aws/ec2/enforce-http-token-imds#aws/ec2",
34 | "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_configuration#metadata-options",
35 | "https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service"
36 | ]
37 | }
38 | ]
39 | }
40 |
--------------------------------------------------------------------------------
/.tfsec/launch_template_imdsv2_tfchecks.json:
--------------------------------------------------------------------------------
1 | {
2 | "checks": [
3 | {
4 | "code": "CUS001",
5 | "description": "Check to IMDSv2 is required on EC2 instances created by this Launch Template",
6 | "impact": "Instance metadata service can be interacted with freely",
7 | "resolution": "Enable HTTP token requirement for IMDS",
8 | "requiredTypes": [
9 | "resource"
10 | ],
11 | "requiredLabels": [
12 | "aws_launch_template"
13 | ],
14 | "severity": "CRITICAL",
15 | "matchSpec": {
16 | "action": "isPresent",
17 | "name": "metadata_options",
18 | "subMatch": {
19 | "action": "and",
20 | "predicateMatchSpec": [
21 | {
22 | "action": "equals",
23 | "name": "http_tokens",
24 | "value": "required"
25 |
26 | }
27 | ]
28 | }
29 | },
30 |
31 | "errorMessage": "is missing `metadata_options` block - it is required with `http_tokens` set to `required` to make Instance Metadata Service more secure.",
32 | "relatedLinks": [
33 | "https://tfsec.dev/docs/aws/ec2/enforce-http-token-imds#aws/ec2",
34 | "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template#metadata-options",
35 | "https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service"
36 | ]
37 | }
38 | ]
39 | }
40 |
--------------------------------------------------------------------------------
/.tfsec/no_launch_config_tfchecks.json:
--------------------------------------------------------------------------------
1 | {
2 | "checks": [
3 | {
4 | "code": "CUS003",
5 | "description": "Use `aws_launch_template` over `aws_launch_configuration",
6 | "impact": "Launch configurations are not capable of versions",
7 | "resolution": "Convert resource type and attributes to `aws_launch_template`",
8 | "requiredTypes": [
9 | "resource"
10 | ],
11 | "requiredLabels": [
12 | "aws_launch_configuration"
13 | ],
14 | "severity": "MEDIUM",
15 | "matchSpec": {
16 | "action": "notPresent",
17 | "name": "image_id"
18 | },
19 |
20 | "errorMessage": "should be changed to `aws_launch_template` since the functionality is the same but templates can be versioned.",
21 | "relatedLinks": [
22 | "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template",
23 | "https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service"
24 | ]
25 | }
26 | ]
27 | }
28 |
--------------------------------------------------------------------------------
/.tfsec/sg_no_embedded_egress_rules_tfchecks.json:
--------------------------------------------------------------------------------
1 | {
2 | "checks": [
3 | {
4 | "code": "CUS005",
5 | "description": "Security group rules should be defined with `aws_security_group_rule` instead of embedded.",
6 | "impact": "Embedded security group rules can cause issues during configuration updates.",
7 | "resolution": "Move `egress` rules to `aws_security_group_rule` and attach to `aws_security_group`.",
8 | "requiredTypes": [
9 | "resource"
10 | ],
11 | "requiredLabels": [
12 | "aws_security_group"
13 | ],
14 | "severity": "MEDIUM",
15 | "matchSpec": {
16 | "action": "notPresent",
17 | "name": "egress"
18 | },
19 |
20 | "errorMessage": "`egress` rules should be moved to `aws_security_group_rule` and attached to `aws_security_group` instead of embedded.",
21 | "relatedLinks": [
22 | "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule",
23 | "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group"
24 | ]
25 | }
26 | ]
27 | }
28 |
--------------------------------------------------------------------------------
/.tfsec/sg_no_embedded_ingress_rules_tfchecks.json:
--------------------------------------------------------------------------------
1 | {
2 | "checks": [
3 | {
4 | "code": "CUS004",
5 | "description": "Security group rules should be defined with `aws_security_group_rule` instead of embedded.",
6 | "impact": "Embedded security group rules can cause issues during configuration updates.",
7 | "resolution": "Move `ingress` rules to `aws_security_group_rule` and attach to `aws_security_group`.",
8 | "requiredTypes": [
9 | "resource"
10 | ],
11 | "requiredLabels": [
12 | "aws_security_group"
13 | ],
14 | "severity": "MEDIUM",
15 | "matchSpec": {
16 | "action": "notPresent",
17 | "name": "ingress"
18 | },
19 |
20 | "errorMessage": "`ingress` rules should be moved to `aws_security_group_rule` and attached to `aws_security_group` instead of embedded.",
21 | "relatedLinks": [
22 | "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule",
23 | "https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group"
24 | ]
25 | }
26 | ]
27 | }
28 |
--------------------------------------------------------------------------------
/CODEOWNERS:
--------------------------------------------------------------------------------
1 | * @aws-ia/aws-ia
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/NOTICE.txt:
--------------------------------------------------------------------------------
1 | Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
4 |
5 | http://aws.amazon.com/apache2.0/
6 |
7 | or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # AWS Network Firewall Module
3 |
4 | *NOTE*: For information regarding the 1.0 upgrade see our [upgrade guide](./docs/UPGRADE-GUIDE-1.0.md)
5 |
6 | [AWS Network Firewall](https://docs.aws.amazon.com/network-firewall/latest/developerguide/what-is-aws-network-firewall.html) is a managed network security service that makes it easy to deploy threat prevention for Amazon VPCs. This module can be used to deploy an AWS Network Firewall resource in the desired VPC, automating all the routing and logging configuration when the resource is deployed.
7 |
8 | The module only handles the creation of the infrastructure, leaving full freedom to the user when defining the firewall rules (which should be done outside the module). Same applies to IAM roles and KMS keys when you define the firewall logging - rememeber that it is a best practice to encryt at rest your firewall logs.
9 |
10 | ## Usage
11 |
12 | To create AWS Network Firewall in your VPC, you need to provide the following information:
13 |
14 | - `network_firewall_name` = (Required|string) Name to provide the AWS Network Firewall resource.
15 | - `network_firewall_description` = (Required|string) A friendly description of the firewall resource.
16 | - `network_firewall_policy`= (Required|string) ARN of the firewall policy to apply in the AWS Network Firewall resource. Check the definition of [AWS Network Firewall Policies](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/networkfirewall_firewall_policy) and [AWS Network Firewall Rule Groups](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/networkfirewall_rule_group) to see how you can create firewall policies.
17 | - `network_firewall_delete_protection` = (Optional|bool) A boolean flag indicating whether it is possible to delete the firewall. Defaults to `false`.
18 | - `network_firewall_policy_change_protection` = (Optional|bool) To indicate whether it is possible to change the associated firewall policy after creation. Defaults to `false`.
19 | - `network_firewall_subnet_change_protection` = (Optional|bool) To indicate whether it is possible to change the associated subnet(s) after creation. Defaults to `false`.
20 | - `network_firewall_encryption_key_arn` = (Optional|string) ARN of Customer managed KMS key for data encryption. By default, AWS managed key will be used.
21 | - `vpc_id` = (Required|string) ID of the VPC where the AWS Network Firewall resource should be placed.
22 | - `vpc_subnets` = (Required|map(string)) Map of subnet IDs to place the Network Firewall endpoints. The expected format of the map is the Availability Zone as key, and the ID of the subnet as value. Example (supposing us-east-1 as AWS Region):
23 |
24 | ```hcl
25 | vpc_subnets = {
26 | us-east-1a = subnet-IDa
27 | us-east-1b = subnet-IDb
28 | us-east-1c = subnet-IDc
29 | }
30 | ```
31 |
32 | - `number_azs` = (Required|number) Number of Availability Zones to place the AWS Network Firewall endpoints.
33 | - `routing_configuration` = (Required|any) Configuration of the routing desired in the VPC. Depending the VPC type, the information to provide is different. The configuration types supported are: `single_vpc`, `intra_vpc_inspection`, `centralized_inspection_without_egress`, and `centralized_inspection_with_egress`. **Only one key (option) can be defined**. More information about the differences between each of the VPC types (and examples) can be checked in the section below.
34 | - `tags`= (Optional|map(string)) List of tags to apply to the AWS Network Firewall resource.
35 |
36 | ## Routing configuration
37 |
38 | Once the AWS Network Firewall resource is created, the routing to the firewall endpoints need to be created. However, depending the VPC and how we want to inspect the traffic, this routing configuration is going to be different. The module supports the routing configuration of 4 different types of VPCs, covering the most common [Inspection Deployment models with with AWS Network Firewall](https://d1.awsstatic.com/architecture-diagrams/ArchitectureDiagrams/inspection-deployment-models-with-AWS-network-firewall-ra.pdf).
39 |
40 | ### Single VPC
41 |
42 | The first use case is when the firewall endpoints are located in the VPC to inspect the traffic from/to workloads in that same VPC. When using this routing configuration, it is expected to place the firewall endpoints in subnets between the Internet gateway (IGW) and the public subnets (where you can place the Elastic Load Balancers and NAT gateways). The module expects in this configuration three variables:
43 |
44 | - `igw_route_table` = (Required|string) VPC route table ID associated to an Internet gateway. In this route table it will be created routes pointing to the Network Firewall endpoints with destination the CIDR blocks provided in the `protected_subnet_cidr_blocks` variable.
45 | - `protected_subnet_route_tables` = (Required|map(string)) Map of the VPC subnet route tables where the resources to protect (using the Network Firewall resource) are located - expected format of the map is Availability Zone (key) --> VPC route table (value). In these route tables it will be created the default route table pointing to the Network Firewall endpoints - ensuring Availability Zone affinity.
46 | - `protected_subnet_cidr_blocks` = (Required|map(string)) Map of IPv4 CIDR blocks indicating the subnets where the resources to protect (using the Network Firewall resource) are located - expected format of the map is Availability Zone (key) --> IPv4 CIDR block (value).
47 |
48 | An example of the definition of this routing configuration is the following one:
49 |
50 | ```hcl
51 | routing_configuration = {
52 | single_vpc = {
53 | igw_route_table = rtb-ID
54 | protected_subnet_route_tables = {
55 | us-east-1a = rtb-IDa
56 | us-east-1b = rtb-IDb
57 | us-east-1c = rtb-IDc
58 | }
59 | protected_subnet_cidr_blocks = {
60 | us-east-1a = "10.0.0.0/24"
61 | us-east-1b = "10.0.1.0/24"
62 | us-east-1c = "10.0.2.0/24"
63 | }
64 | }
65 | }
66 | ```
67 |
68 | ### Intra-VPC Inspection
69 |
70 | When placing firewall endpoints to inspect traffic between workloads inside the same VPC (between your EC2 instances and the database layer, for example) you can take advantage of the VPC routing enhacement - which allows you to include more specific routing than the local one. The module expects in this configuration two variables:
71 |
72 | - `number_routes` = (Required|number) Number of configured items in the `routes` variable.
73 | - `routes` = (Required|list(map(any))) List of intra-VPC route configurations. Important to note that only one direction is configured per item in this list, so in most situations you will need two items per group of subnets to inspect. Each item expects a map of strings with two values:
74 | - `source_subnet_route_tables` = (Required|map(string)) VPC route table of the source subnet - expected format of the map is Availability Zone (key) --> VPC route table (value)
75 | - `destination_subnet_cidr_blocks` = (Required|map(string)). IPv4 CIDR blocks of the destination subnet - expected format of the map is Availability Zone (key) --> IPv4 CIDR block (value)
76 |
77 | An example of the definition of this routing configuration is the following one.
78 |
79 | ```hcl
80 | routing_configuration = {
81 | intra_vpc_inspection = {
82 | number_routes = 2
83 | routes = {
84 | {
85 | source_subnet_route_tables = {
86 | us-east-1a = rtb-IDa
87 | us-east-1b = rtb-IDb
88 | us-east-1c = rtb-IDc
89 | }
90 | destination_subnet_cidr_blocks = {
91 | us-east-1a = "10.0.0.0/24"
92 | us-east-1b = "10.0.1.0/24"
93 | us-east-1c = "10.0.2.0/24"
94 | }
95 | },
96 | {
97 | source_subnet_route_tables = {
98 | us-east-1a = rtb-IDaa
99 | us-east-1b = rtb-IDbb
100 | us-east-1c = rtb-IDcc
101 | }
102 | destination_subnet_cidr_blocks = {
103 | us-east-1a = "10.0.3.0/24"
104 | us-east-1b = "10.0.4.0/24"
105 | us-east-1c = "10.0.5.0/24"
106 | }
107 | }
108 | }
109 | }
110 | }
111 | ```
112 |
113 | ### Hub and Spoke with Inspection VPC
114 |
115 | The use case covers the creation of a centralized Inspection VPC in a hub-and-spoke architecture with [AWS Transit Gateway](https://docs.aws.amazon.com/vpc/latest/tgw/what-is-transit-gateway.html) and/or [AWS Cloud WAN](https://docs.aws.amazon.com/network-manager/latest/cloudwan/what-is-cloudwan.html), with the idea of managing the traffic inspection at scale. When using the key `centralized_inspection_without_egress` it is supposed that the Inspection VPC created is only used to place the Transit Gateway or Cloud WAN's core network ENIs and the firewall endpoints. In this configuration, you can configurate the following variables:
116 |
117 | - `connectivity_subnet_route_tables` = (Optional|map(string)) Map of VPC subnet route tables where the Transit Gateway or Cloud WAN's core network ENIs are located. In these route tables a default route pointing to the Network Firewall endpoints is created - expected format of the map is Availability Zone (key) --> VPC route table (value)
118 |
119 | An example of the definition of this routing configuration is the following one:
120 |
121 | ```hcl
122 | routing_configuration = {
123 | centralized_inspection_without_egress = {
124 | connectivity_subnet_route_tables = {
125 | us-east-1a = rtb-IDa
126 | us-east-1b = rtb-IDb
127 | us-east-1c = rtb-IDc
128 | }
129 | }
130 | }
131 | ```
132 |
133 | ### Hub and Spoke with Inspection VPC (with egress traffic)
134 |
135 | The use case covers the creation of a centralized Inspection VPC in a hub-and-spoke architecture with [AWS Transit Gateway](https://docs.aws.amazon.com/vpc/latest/tgw/what-is-transit-gateway.html) or [AWS Cloud WAN](https://docs.aws.amazon.com/network-manager/latest/cloudwan/what-is-cloudwan.html), with the idea of managing the traffic inspection at scale. When using the key `centralized_inspection_with_egress` it is supposed that the Inspection VPC also has access to the Internet, to centralize inspection and egress traffic at the same time. In this configuration, you can configurate the following variables:
136 |
137 | - `connectivity_subnet_route_tables` = (Optional|map(string)) Map of VPC subnet route tables where the Transit Gateway or Cloud WAN's core network ENIs are located. In these route tables a default route pointing to the Network Firewall endpoints is created - expected format of the map is Availability Zone (key) --> VPC route table (value)
138 | - `public_subnet_route_tables` = (Required|map(string)) Map of VPC public subnet route tables. In these route tables, routes are created pointing to the Network Firewall endpoints with destination the CIDR blocks defined in the `network_cidr_blocks` variable. The expected format of the map is Availability Zone (key) --> VPC route table (value)
139 | - `network_cidr_blocks` = (Required|list(string)) List of IPv4 CIDR blocks defining the AWS network.
140 |
141 | An example of the definition of this routing configuration is the following one:
142 |
143 | ```hcl
144 | routing_configuration = {
145 | centralized_inspection_with_egress = {
146 | connectivity_subnet_route_tables = {
147 | us-east-1a = rtb-IDa
148 | us-east-1b = rtb-IDb
149 | us-east-1c = rtb-IDc
150 | }
151 | public_subnet_route_tables = {
152 | us-east-1a = rtb-IDaa
153 | us-east-1b = rtb-IDbb
154 | us-east-1c = rtb-IDcc
155 | }
156 | network_cidr_blocks = ["10.0.0.0/8", "192.168.0.0/24"]
157 | }
158 | }
159 | ```
160 |
161 | ## Logging
162 |
163 | You can enable AWS Network Firewall logging for the [stateful engine](https://docs.aws.amazon.com/network-firewall/latest/developerguide/firewall-rules-engines.html). You can record the flow logs and/or alert logs, with only one destination per log type:
164 |
165 | * [Amazon S3](https://aws.amazon.com/s3/) bucket.
166 | * [Amazon CloudWatch](https://aws.amazon.com/cloudwatch/) log group.
167 | * [Amazon Kinesis Data Firehose](https://aws.amazon.com/kinesis/data-firehose/) stream.
168 |
169 | For more information about the logging in AWS Network Firewall, check the [AWS Network Firewall documentation](https://docs.aws.amazon.com/network-firewall/latest/developerguide/firewall-logging.html).
170 |
171 | Regarding the format of the variable, you can define either `flow_log` or `alert_log` configurations; with only 1 destination in each one of them. As mentioned above, 3 different destinations can be configured:
172 |
173 | - `s3_bucket` = (Optional|map(string)) Configuration of an S3 bucket as logging destination. Two attributes are expected:
174 | - `bucketName` = (Required|string) Name of the S3 bucket to deliver the logs.
175 | - `logPrefix` = (Optional|string) Path inside the S3 bucket.
176 | - `cloudwatch_logs` = (Optional|map(string)) Configuration of a CloudWatch log group as logging destination. Only the attribute `logGroupName` (Required|string) is expected.
177 | - `kinesis_firehose` = (Optional|map(string)) Configuration of a Kinesis Data Firehose stream as logging desintation. Only the attribute `deliveryStreamName` (Required|string) is expected.
178 |
179 | Example definition of each type:
180 |
181 | ```hcl
182 | logging_configuration = {
183 | flow_log = {
184 | s3_bucket = {
185 | bucketName = "my-bucket"
186 | logPrefix = "/logs"
187 | }
188 | }
189 | }
190 |
191 | logging_configuration = {
192 | alert_log = {
193 | cloudwatch_logs = {
194 | logGroupName = "my-log-group"
195 | }
196 | }
197 | }
198 |
199 | logging_configuration = {
200 | alert_log = {
201 | kinesis_firehose = {
202 | deliveryStreamName = "my-stream"
203 | }
204 | }
205 | }
206 | ```
207 |
208 | ## References
209 |
210 | - Reference Architecture: [Inspection Deployment models with with AWS Network Firewall](https://d1.awsstatic.com/architecture-diagrams/ArchitectureDiagrams/inspection-deployment-models-with-AWS-network-firewall-ra.pdf)
211 | - Blog post: [Deployment models for AWS Network Firewall](https://aws.amazon.com/blogs/networking-and-content-delivery/deployment-models-for-aws-network-firewall/)
212 | - Blog post: [Deployment models for AWS Network Firewall with VPC routing enhancements](https://aws.amazon.com/blogs/networking-and-content-delivery/deployment-models-for-aws-network-firewall-with-vpc-routing-enhancements/)
213 |
214 | ## Requirements
215 |
216 | | Name | Version |
217 | |------|---------|
218 | | [terraform](#requirement\_terraform) | >= 1.3.0 |
219 | | [aws](#requirement\_aws) | >= 3.73.0 |
220 |
221 | ## Providers
222 |
223 | | Name | Version |
224 | |------|---------|
225 | | [aws](#provider\_aws) | >= 3.73.0 |
226 |
227 | ## Modules
228 |
229 | | Name | Source | Version |
230 | |------|--------|---------|
231 | | [central\_inspection\_with\_egress\_routing](#module\_central\_inspection\_with\_egress\_routing) | ./modules/central_inspection_with_egress_routing | n/a |
232 | | [intra\_vpc\_routing](#module\_intra\_vpc\_routing) | ./modules/intra_vpc_routing | n/a |
233 | | [logging](#module\_logging) | ./modules/logging | n/a |
234 | | [tags](#module\_tags) | aws-ia/label/aws | 0.0.6 |
235 |
236 | ## Resources
237 |
238 | | Name | Type |
239 | |------|------|
240 | | [aws_networkfirewall_firewall.anfw](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/networkfirewall_firewall) | resource |
241 | | [aws_route.connectivity_to_firewall_endpoint](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
242 | | [aws_route.connectivity_to_firewall_endpoint_without_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
243 | | [aws_route.igw_route_table_to_protected_subnets](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
244 | | [aws_route.protected_route_table_to_internet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
245 |
246 | ## Inputs
247 |
248 | | Name | Description | Type | Default | Required |
249 | |------|-------------|------|---------|:--------:|
250 | | [network\_firewall\_description](#input\_network\_firewall\_description) | A friendly description of the firewall resource. | `string` | n/a | yes |
251 | | [network\_firewall\_name](#input\_network\_firewall\_name) | Name to give the AWS Network Firewall resource created. | `string` | n/a | yes |
252 | | [network\_firewall\_policy](#input\_network\_firewall\_policy) | ARN of the firewall policy to include in AWS Network Firewall. | `string` | n/a | yes |
253 | | [number\_azs](#input\_number\_azs) | Number of Availability Zones to place the Network Firewall endpoints. | `number` | n/a | yes |
254 | | [vpc\_id](#input\_vpc\_id) | VPC ID to place the Network Firewall endpoints. | `string` | n/a | yes |
255 | | [vpc\_subnets](#input\_vpc\_subnets) | Map of subnet IDs to place the Network Firewall endpoints. The expected format of the map is the Availability Zone as key, and the ID of the subnet as value.
Example (supposing us-east-1 as AWS Region):
vpc_subnets = {| `map(string)` | n/a | yes | 256 | | [logging\_configuration](#input\_logging\_configuration) | Configuration of the logging desired for the Network Firewall. You can configure at most 2 destinations for your logs, 1 for FLOW logs and 1 for ALERT logs.
us-east-1a = subnet-IDa
us-east-1b = subnet-IDb
us-east-1c = subnet-IDc
}
{| no | 70 | | [supernet](#input\_supernet) | Supernet CIDR block. | `map(string)` |
"cidr_block": "10.129.0.0/16",
"inspection_subnet_netmask": 28,
"number_azs": 2,
"public_subnet_netmask": 28,
"tgw_subnet_netmask": 28
}
{| no | 71 | 72 | ## Outputs 73 | 74 | | Name | Description | 75 | |------|-------------| 76 | | [inspection\_vpc](#output\_inspection\_vpc) | VPCs created. | 77 | | [network\_firewall](#output\_network\_firewall) | AWS Network Firewall ID. | 78 | | [transit\_gateway](#output\_transit\_gateway) | AWS Transit Gateway ID. | 79 | -------------------------------------------------------------------------------- /examples/central_inspection_with_egress/main.tf: -------------------------------------------------------------------------------- 1 | # --- examples/central_inspection_with_egress/main.tf --- 2 | 3 | # AWS Network Firewall 4 | module "network_firewall" { 5 | source = "aws-ia/networkfirewall/aws" 6 | version = "1.0.0" 7 | 8 | network_firewall_name = "anfw-${var.identifier}" 9 | network_firewall_description = "AWS Network Firewall - ${var.identifier}" 10 | network_firewall_policy = aws_networkfirewall_firewall_policy.anfw_policy.arn 11 | 12 | vpc_id = module.inspection_vpc.vpc_attributes.id 13 | number_azs = var.inspection_vpc.number_azs 14 | vpc_subnets = { for k, v in module.inspection_vpc.private_subnet_attributes_by_az : split("/", k)[1] => v.id if split("/", k)[0] == "inspection" } 15 | 16 | routing_configuration = { 17 | centralized_inspection_with_egress = { 18 | connectivity_subnet_route_tables = { for k, v in module.inspection_vpc.rt_attributes_by_type_by_az.transit_gateway : k => v.id } 19 | public_subnet_route_tables = { for k, v in module.inspection_vpc.rt_attributes_by_type_by_az.public : k => v.id } 20 | network_cidr_blocks = [var.supernet] 21 | } 22 | } 23 | } 24 | 25 | # AWS Transit Gateway 26 | resource "aws_ec2_transit_gateway" "tgw" { 27 | description = "Transit Gateway - ${var.identifier}" 28 | default_route_table_association = "disable" 29 | default_route_table_propagation = "disable" 30 | 31 | tags = { 32 | Name = "tgw-${var.identifier}" 33 | } 34 | } 35 | 36 | # Inspection VPC. Module - https://github.com/aws-ia/terraform-aws-vpc 37 | module "inspection_vpc" { 38 | source = "aws-ia/vpc/aws" 39 | version = "= 4.3.0" 40 | 41 | name = "inspection_vpc" 42 | cidr_block = var.inspection_vpc.cidr_block 43 | az_count = var.inspection_vpc.number_azs 44 | 45 | transit_gateway_id = aws_ec2_transit_gateway.tgw.id 46 | transit_gateway_routes = { 47 | inspection = var.supernet 48 | } 49 | 50 | subnets = { 51 | public = { 52 | netmask = var.inspection_vpc.public_subnet_netmask 53 | nat_gateway_configuration = "all_azs" 54 | } 55 | inspection = { 56 | netmask = var.inspection_vpc.inspection_subnet_netmask 57 | connect_to_public_natgw = true 58 | } 59 | transit_gateway = { 60 | netmask = var.inspection_vpc.tgw_subnet_netmask 61 | transit_gateway_default_route_table_association = false 62 | transit_gateway_default_route_table_propagation = false 63 | transit_gateway_appliance_mode_support = "enable" 64 | } 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /examples/central_inspection_with_egress/outputs.tf: -------------------------------------------------------------------------------- 1 | # --- examples/central_inspection_with_egress/outputs.tf --- 2 | 3 | output "transit_gateway" { 4 | description = "AWS Transit Gateway ID." 5 | value = aws_ec2_transit_gateway.tgw.id 6 | } 7 | 8 | output "inspection_vpc" { 9 | description = "VPCs created." 10 | value = module.inspection_vpc.vpc_attributes.id 11 | } 12 | 13 | output "network_firewall" { 14 | description = "AWS Network Firewall ID." 15 | value = module.network_firewall.aws_network_firewall.id 16 | } -------------------------------------------------------------------------------- /examples/central_inspection_with_egress/policy.tf: -------------------------------------------------------------------------------- 1 | # --- examples/central_inspection_with_egress/policy.tf --- 2 | 3 | resource "aws_networkfirewall_firewall_policy" "anfw_policy" { 4 | name = "firewall-policy-${var.identifier}" 5 | 6 | firewall_policy { 7 | 8 | # Stateless configuration 9 | stateless_default_actions = ["aws:forward_to_sfe"] 10 | stateless_fragment_default_actions = ["aws:forward_to_sfe"] 11 | 12 | stateless_rule_group_reference { 13 | priority = 10 14 | resource_arn = aws_networkfirewall_rule_group.drop_remote.arn 15 | } 16 | 17 | # Stateful configuration 18 | stateful_engine_options { 19 | rule_order = "STRICT_ORDER" 20 | } 21 | stateful_default_actions = ["aws:drop_strict", "aws:alert_strict"] 22 | stateful_rule_group_reference { 23 | priority = 10 24 | resource_arn = aws_networkfirewall_rule_group.allow_icmp.arn 25 | } 26 | stateful_rule_group_reference { 27 | priority = 20 28 | resource_arn = aws_networkfirewall_rule_group.allow_domains.arn 29 | } 30 | } 31 | } 32 | 33 | # Stateless Rule Group - Dropping any SSH or RDP connection 34 | resource "aws_networkfirewall_rule_group" "drop_remote" { 35 | capacity = 2 36 | name = "drop-remote-${var.identifier}" 37 | type = "STATELESS" 38 | rule_group { 39 | rules_source { 40 | stateless_rules_and_custom_actions { 41 | 42 | stateless_rule { 43 | priority = 1 44 | rule_definition { 45 | actions = ["aws:drop"] 46 | match_attributes { 47 | protocols = [6] 48 | source { 49 | address_definition = "0.0.0.0/0" 50 | } 51 | source_port { 52 | from_port = 22 53 | to_port = 22 54 | } 55 | destination { 56 | address_definition = "0.0.0.0/0" 57 | } 58 | destination_port { 59 | from_port = 22 60 | to_port = 22 61 | } 62 | } 63 | } 64 | } 65 | 66 | stateless_rule { 67 | priority = 2 68 | rule_definition { 69 | actions = ["aws:drop"] 70 | match_attributes { 71 | protocols = [27] 72 | source { 73 | address_definition = "0.0.0.0/0" 74 | } 75 | destination { 76 | address_definition = "0.0.0.0/0" 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | 86 | # Stateful Rule Group 1 - Allowing ICMP traffic 87 | resource "aws_networkfirewall_rule_group" "allow_icmp" { 88 | capacity = 100 89 | name = "allow-icmp-${var.identifier}" 90 | type = "STATEFUL" 91 | rule_group { 92 | rule_variables { 93 | ip_sets { 94 | key = "SUPERNET" 95 | ip_set { 96 | definition = [var.supernet] 97 | } 98 | } 99 | } 100 | rules_source { 101 | rules_string = <
"ipv4": "10.0.0.0/8",
"ipv6": "2001:db8:1234:1a00::/56"
}
{| no | 70 | 71 | ## Outputs 72 | 73 | | Name | Description | 74 | |------|-------------| 75 | | [inspection\_vpc](#output\_inspection\_vpc) | Inspection VPC ID. | 76 | | [network\_firewall](#output\_network\_firewall) | AWS Network Firewall ID. | 77 | | [transit\_gateway](#output\_transit\_gateway) | AWS Transit Gateway ID. | 78 | -------------------------------------------------------------------------------- /examples/central_inspection_without_egress/main.tf: -------------------------------------------------------------------------------- 1 | # --- examples/central_inspection_without_egress/main.tf --- 2 | 3 | # AWS Network Firewall 4 | module "network_firewall" { 5 | source = "aws-ia/networkfirewall/aws" 6 | version = "1.0.0" 7 | 8 | network_firewall_name = "anfw-${var.identifier}" 9 | network_firewall_description = "AWS Network Firewall - ${var.identifier}" 10 | network_firewall_policy = aws_networkfirewall_firewall_policy.anfw_policy.arn 11 | 12 | vpc_id = module.inspection_vpc.vpc_attributes.id 13 | number_azs = var.inspection_vpc.number_azs 14 | vpc_subnets = { for k, v in module.inspection_vpc.private_subnet_attributes_by_az : split("/", k)[1] => v.id if split("/", k)[0] == "inspection" } 15 | 16 | routing_configuration = { 17 | centralized_inspection_without_egress = { 18 | connectivity_subnet_route_tables = { for k, v in module.inspection_vpc.rt_attributes_by_type_by_az.transit_gateway : k => v.id } 19 | } 20 | } 21 | } 22 | 23 | # AWS Transit Gateway 24 | resource "aws_ec2_transit_gateway" "tgw" { 25 | description = "Transit Gateway - ${var.identifier}" 26 | default_route_table_association = "disable" 27 | default_route_table_propagation = "disable" 28 | 29 | tags = { 30 | Name = "tgw-${var.identifier}" 31 | } 32 | } 33 | 34 | # Inspection VPC. Module - https://github.com/aws-ia/terraform-aws-vpc 35 | module "inspection_vpc" { 36 | source = "aws-ia/vpc/aws" 37 | version = "= 4.3.0" 38 | 39 | transit_gateway_id = aws_ec2_transit_gateway.tgw.id 40 | transit_gateway_routes = { 41 | inspection = "0.0.0.0/0" 42 | } 43 | 44 | name = "inspection_vpc" 45 | cidr_block = var.inspection_vpc.cidr_block 46 | az_count = var.inspection_vpc.number_azs 47 | 48 | subnets = { 49 | inspection = { netmask = var.inspection_vpc.private_subnet_netmask } 50 | transit_gateway = { 51 | netmask = var.inspection_vpc.tgw_subnet_netmask 52 | transit_gateway_default_route_table_association = false 53 | transit_gateway_default_route_table_propagation = false 54 | transit_gateway_appliance_mode_support = "enable" 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /examples/central_inspection_without_egress/outputs.tf: -------------------------------------------------------------------------------- 1 | # --- examples/central_inspection_without_egress/outputs.tf --- 2 | 3 | output "transit_gateway" { 4 | description = "AWS Transit Gateway ID." 5 | value = aws_ec2_transit_gateway.tgw.id 6 | } 7 | 8 | output "inspection_vpc" { 9 | description = "Inspection VPC ID." 10 | value = module.inspection_vpc.vpc_attributes.id 11 | } 12 | 13 | output "network_firewall" { 14 | description = "AWS Network Firewall ID." 15 | value = module.network_firewall.aws_network_firewall.id 16 | } -------------------------------------------------------------------------------- /examples/central_inspection_without_egress/policy.tf: -------------------------------------------------------------------------------- 1 | # --- examples/central_inspection_without_egress/policy.tf --- 2 | 3 | resource "aws_networkfirewall_firewall_policy" "anfw_policy" { 4 | name = "firewall-policy-${var.identifier}" 5 | 6 | firewall_policy { 7 | 8 | # Stateless configuration 9 | stateless_default_actions = ["aws:forward_to_sfe"] 10 | stateless_fragment_default_actions = ["aws:forward_to_sfe"] 11 | 12 | stateless_rule_group_reference { 13 | priority = 10 14 | resource_arn = aws_networkfirewall_rule_group.drop_remote.arn 15 | } 16 | 17 | # Stateful configuration 18 | stateful_engine_options { 19 | rule_order = "STRICT_ORDER" 20 | } 21 | stateful_default_actions = ["aws:drop_strict", "aws:alert_strict"] 22 | stateful_rule_group_reference { 23 | priority = 10 24 | resource_arn = aws_networkfirewall_rule_group.allow_icmp_1_to_2.arn 25 | } 26 | stateful_rule_group_reference { 27 | priority = 20 28 | resource_arn = aws_networkfirewall_rule_group.allow_icmp_2_to_3.arn 29 | } 30 | } 31 | } 32 | 33 | # Stateless Rule Group - Dropping any SSH or RDP connection 34 | resource "aws_networkfirewall_rule_group" "drop_remote" { 35 | capacity = 2 36 | name = "drop-remote-${var.identifier}" 37 | type = "STATELESS" 38 | rule_group { 39 | rules_source { 40 | stateless_rules_and_custom_actions { 41 | 42 | stateless_rule { 43 | priority = 1 44 | rule_definition { 45 | actions = ["aws:drop"] 46 | match_attributes { 47 | protocols = [6] 48 | source { 49 | address_definition = "0.0.0.0/0" 50 | } 51 | source_port { 52 | from_port = 22 53 | to_port = 22 54 | } 55 | destination { 56 | address_definition = "0.0.0.0/0" 57 | } 58 | destination_port { 59 | from_port = 22 60 | to_port = 22 61 | } 62 | } 63 | } 64 | } 65 | 66 | stateless_rule { 67 | priority = 2 68 | rule_definition { 69 | actions = ["aws:drop"] 70 | match_attributes { 71 | protocols = [27] 72 | source { 73 | address_definition = "0.0.0.0/0" 74 | } 75 | destination { 76 | address_definition = "0.0.0.0/0" 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | 86 | # Stateful Rule Group 1 - Allowing ICMP traffic from Spoke VPC 1 to 2 87 | resource "aws_networkfirewall_rule_group" "allow_icmp_1_to_2" { 88 | capacity = 100 89 | name = "allow-icmp-12-${var.identifier}" 90 | type = "STATEFUL" 91 | rule_group { 92 | rule_variables { 93 | ip_sets { 94 | key = "SPOKE1" 95 | ip_set { 96 | definition = ["10.0.0.0/24"] 97 | } 98 | } 99 | ip_sets { 100 | key = "SPOKE2" 101 | ip_set { 102 | definition = ["10.0.1.0/24"] 103 | } 104 | } 105 | } 106 | rules_source { 107 | rules_string = <
"cidr_block": "10.129.0.0/16",
"number_azs": 2,
"private_subnet_netmask": 28,
"tgw_subnet_netmask": 28
}
{| no | 67 | 68 | ## Outputs 69 | 70 | | Name | Description | 71 | |------|-------------| 72 | | [network\_firewall](#output\_network\_firewall) | AWS Network Firewall ID. | 73 | | [vpc](#output\_vpc) | VPC ID. | 74 | -------------------------------------------------------------------------------- /examples/intra_vpc_inspection/main.tf: -------------------------------------------------------------------------------- 1 | # --- examples/intra_vpc_inspection/main.tf --- 2 | 3 | # AWS Network Firewall 4 | module "network_firewall" { 5 | source = "aws-ia/networkfirewall/aws" 6 | version = "1.0.0" 7 | 8 | network_firewall_name = "anfw-${var.identifier}" 9 | network_firewall_description = "AWS Network Firewall - ${var.identifier}" 10 | network_firewall_policy = aws_networkfirewall_firewall_policy.anfw_policy.arn 11 | 12 | vpc_id = module.vpc.vpc_attributes.id 13 | number_azs = var.vpc.number_azs 14 | vpc_subnets = { for k, v in module.vpc.private_subnet_attributes_by_az : split("/", k)[1] => v.id if split("/", k)[0] == "inspection" } 15 | 16 | routing_configuration = { 17 | intra_vpc_inspection = { 18 | number_routes = 6 19 | routes = [ 20 | { 21 | source_subnet_route_tables = { for k, v in module.vpc.rt_attributes_by_type_by_az.private : split("/", k)[1] => v.id if split("/", k)[0] == "private1" } 22 | destination_subnet_cidr_blocks = local.private2_cidrs 23 | }, 24 | { 25 | source_subnet_route_tables = { for k, v in module.vpc.rt_attributes_by_type_by_az.private : split("/", k)[1] => v.id if split("/", k)[0] == "private2" } 26 | destination_subnet_cidr_blocks = local.private1_cidrs 27 | }, 28 | { 29 | source_subnet_route_tables = { for k, v in module.vpc.rt_attributes_by_type_by_az.private : split("/", k)[1] => v.id if split("/", k)[0] == "private1" } 30 | destination_subnet_cidr_blocks = local.private3_cidrs 31 | }, 32 | { 33 | source_subnet_route_tables = { for k, v in module.vpc.rt_attributes_by_type_by_az.private : split("/", k)[1] => v.id if split("/", k)[0] == "private3" } 34 | destination_subnet_cidr_blocks = local.private1_cidrs 35 | }, 36 | { 37 | source_subnet_route_tables = { for k, v in module.vpc.rt_attributes_by_type_by_az.private : split("/", k)[1] => v.id if split("/", k)[0] == "private2" } 38 | destination_subnet_cidr_blocks = local.private3_cidrs 39 | }, 40 | { 41 | source_subnet_route_tables = { for k, v in module.vpc.rt_attributes_by_type_by_az.private : split("/", k)[1] => v.id if split("/", k)[0] == "private3" } 42 | destination_subnet_cidr_blocks = local.private2_cidrs 43 | } 44 | ] 45 | } 46 | } 47 | } 48 | 49 | # VPC Module - https://github.com/aws-ia/terraform-aws-vpc 50 | module "vpc" { 51 | source = "aws-ia/vpc/aws" 52 | version = "= 4.3.0" 53 | 54 | name = "vpc_intra_inspection" 55 | cidr_block = var.vpc.cidr_block 56 | az_count = var.vpc.number_azs 57 | 58 | subnets = { 59 | private1 = { cidrs = var.vpc.private1_subnet_cidrs } 60 | private2 = { cidrs = var.vpc.private2_subnet_cidrs } 61 | private3 = { cidrs = var.vpc.private3_subnet_cidrs } 62 | inspection = { cidrs = var.vpc.inspection_subnet_cidrs } 63 | } 64 | } 65 | 66 | # Local variables - creating maps of subnet CIDRs. Format: AZ --> CIDR block 67 | locals { 68 | private1_cidrs = tomap({ 69 | for i, az in module.vpc.azs : az => var.vpc.private1_subnet_cidrs[i] 70 | }) 71 | private2_cidrs = tomap({ 72 | for i, az in module.vpc.azs : az => var.vpc.private2_subnet_cidrs[i] 73 | }) 74 | private3_cidrs = tomap({ 75 | for i, az in module.vpc.azs : az => var.vpc.private3_subnet_cidrs[i] 76 | }) 77 | } 78 | 79 | 80 | -------------------------------------------------------------------------------- /examples/intra_vpc_inspection/outputs.tf: -------------------------------------------------------------------------------- 1 | # --- examples/intra_vpc_inspection/outputs.tf --- 2 | 3 | output "vpc" { 4 | description = "VPC ID." 5 | value = module.vpc.vpc_attributes.id 6 | } 7 | 8 | output "network_firewall" { 9 | description = "AWS Network Firewall ID." 10 | value = module.network_firewall.aws_network_firewall.id 11 | } 12 | -------------------------------------------------------------------------------- /examples/intra_vpc_inspection/policy.tf: -------------------------------------------------------------------------------- 1 | # --- examples/intra_vpc_inspection/policy.tf --- 2 | 3 | resource "aws_networkfirewall_firewall_policy" "anfw_policy" { 4 | name = "firewall-policy-${var.identifier}" 5 | 6 | firewall_policy { 7 | 8 | # Stateless configuration 9 | stateless_default_actions = ["aws:forward_to_sfe"] 10 | stateless_fragment_default_actions = ["aws:forward_to_sfe"] 11 | 12 | stateless_rule_group_reference { 13 | priority = 10 14 | resource_arn = aws_networkfirewall_rule_group.drop_remote.arn 15 | } 16 | 17 | # Stateful configuration 18 | stateful_engine_options { 19 | rule_order = "STRICT_ORDER" 20 | } 21 | stateful_default_actions = ["aws:drop_strict", "aws:alert_strict"] 22 | stateful_rule_group_reference { 23 | priority = 10 24 | resource_arn = aws_networkfirewall_rule_group.allow_icmp_private1_2.arn 25 | } 26 | stateful_rule_group_reference { 27 | priority = 20 28 | resource_arn = aws_networkfirewall_rule_group.allow_icmp_private2_3.arn 29 | } 30 | } 31 | } 32 | 33 | # Stateless Rule Group - Dropping any SSH or RDP connection 34 | resource "aws_networkfirewall_rule_group" "drop_remote" { 35 | capacity = 2 36 | name = "drop-remote-${var.identifier}" 37 | type = "STATELESS" 38 | rule_group { 39 | rules_source { 40 | stateless_rules_and_custom_actions { 41 | 42 | stateless_rule { 43 | priority = 1 44 | rule_definition { 45 | actions = ["aws:drop"] 46 | match_attributes { 47 | protocols = [6] 48 | source { 49 | address_definition = "0.0.0.0/0" 50 | } 51 | source_port { 52 | from_port = 22 53 | to_port = 22 54 | } 55 | destination { 56 | address_definition = "0.0.0.0/0" 57 | } 58 | destination_port { 59 | from_port = 22 60 | to_port = 22 61 | } 62 | } 63 | } 64 | } 65 | 66 | stateless_rule { 67 | priority = 2 68 | rule_definition { 69 | actions = ["aws:drop"] 70 | match_attributes { 71 | protocols = [27] 72 | source { 73 | address_definition = "0.0.0.0/0" 74 | } 75 | destination { 76 | address_definition = "0.0.0.0/0" 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | 86 | # Stateful Rule Group 1 - Allowing ICMP traffic from private1 subnets to private2 subnets 87 | resource "aws_networkfirewall_rule_group" "allow_icmp_private1_2" { 88 | capacity = 100 89 | name = "allow-icmp-private12-${var.identifier}" 90 | type = "STATEFUL" 91 | rule_group { 92 | rule_variables { 93 | ip_sets { 94 | key = "PRIVATE1" 95 | ip_set { 96 | definition = var.vpc.private1_subnet_cidrs 97 | } 98 | } 99 | ip_sets { 100 | key = "PRIVATE2" 101 | ip_set { 102 | definition = var.vpc.private2_subnet_cidrs 103 | } 104 | } 105 | } 106 | rules_source { 107 | rules_string = <
"cidr_block": "10.129.0.0/16",
"inspection_subnet_cidrs": [
"10.129.6.0/28",
"10.129.6.16/28"
],
"number_azs": 2,
"private1_subnet_cidrs": [
"10.129.0.0/24",
"10.129.1.0/24"
],
"private2_subnet_cidrs": [
"10.129.2.0/24",
"10.129.3.0/24"
],
"private3_subnet_cidrs": [
"10.129.4.0/24",
"10.129.5.0/24"
]
}
{| no | 79 | 80 | ## Outputs 81 | 82 | | Name | Description | 83 | |------|-------------| 84 | | [network\_firewall](#output\_network\_firewall) | AWS Network Firewall ID. | 85 | | [vpc](#output\_vpc) | VPC ID. | 86 | -------------------------------------------------------------------------------- /examples/single_vpc_logging/kms_keys.tf: -------------------------------------------------------------------------------- 1 | # DATA SOURCE: AWS CALLER IDENTITY - Used to get the Account ID 2 | data "aws_caller_identity" "current" {} 3 | 4 | # KMS Key for CloudWatch Log Groups 5 | resource "aws_kms_key" "log_key" { 6 | description = "KMS Logs Key" 7 | deletion_window_in_days = 7 8 | enable_key_rotation = true 9 | policy = data.aws_iam_policy_document.policy_kms_logs_document.json 10 | 11 | tags = { 12 | Name = "kms-key-${var.identifier}" 13 | } 14 | } 15 | 16 | # KMS Policy - it allows the use of the Key by the CloudWatch log groups created in this sample 17 | data "aws_iam_policy_document" "policy_kms_logs_document" { 18 | statement { 19 | sid = "Enable IAM User Permissions" 20 | actions = ["kms:*"] 21 | resources = ["arn:aws:kms:${var.aws_region}:${data.aws_caller_identity.current.account_id}:*"] 22 | 23 | principals { 24 | type = "AWS" 25 | identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] 26 | } 27 | } 28 | 29 | statement { 30 | sid = "Enable KMS to be used by CloudWatch Logs" 31 | actions = [ 32 | "kms:Encrypt*", 33 | "kms:Decrypt*", 34 | "kms:ReEncrypt*", 35 | "kms:GenerateDataKey*", 36 | "kms:Describe*" 37 | ] 38 | resources = ["arn:aws:kms:${var.aws_region}:${data.aws_caller_identity.current.account_id}:*"] 39 | 40 | principals { 41 | type = "Service" 42 | identifiers = ["logs.${var.aws_region}.amazonaws.com"] 43 | } 44 | 45 | condition { 46 | test = "ArnLike" 47 | variable = "kms:EncryptionContext:aws:logs:arn" 48 | values = ["arn:aws:logs:${var.aws_region}:${data.aws_caller_identity.current.account_id}:*"] 49 | } 50 | } 51 | } 52 | 53 | # Encryption KMS Key 54 | resource "aws_kms_key" "encryption_key" { 55 | description = "KMS Encryption Key" 56 | deletion_window_in_days = 7 57 | enable_key_rotation = true 58 | policy = data.aws_iam_policy_document.policy_encryption_key_document.json 59 | 60 | tags = { 61 | Name = "kms-key-${var.identifier}" 62 | } 63 | } 64 | 65 | # KMS Policy - it allows the use of the KMS Key by the root account and key creator 66 | data "aws_iam_policy_document" "policy_encryption_key_document" { 67 | statement { 68 | sid = "Enable IAM User Permissions" 69 | actions = ["kms:*"] 70 | resources = ["arn:aws:kms:${var.aws_region}:${data.aws_caller_identity.current.account_id}:*"] 71 | 72 | principals { 73 | type = "AWS" 74 | identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] 75 | } 76 | } 77 | 78 | statement { 79 | sid = "Allow access for Key Administrators" 80 | actions = [ 81 | "kms:Create*", 82 | "kms:Describe*", 83 | "kms:Enable*", 84 | "kms:List*", 85 | "kms:Put*", 86 | "kms:Update*", 87 | "kms:Revoke*", 88 | "kms:Disable*", 89 | "kms:Get*", 90 | "kms:Delete*", 91 | "kms:TagResource", 92 | "kms:UntagResource", 93 | "kms:ScheduleKeyDeletion", 94 | "kms:CancelKeyDeletion" 95 | ] 96 | 97 | resources = ["*"] 98 | principals { 99 | type = "AWS" 100 | identifiers = [data.aws_caller_identity.current.arn] 101 | } 102 | } 103 | 104 | statement { 105 | sid = "Allow attachment of persistent resources" 106 | actions = [ 107 | "kms:CreateGrant", 108 | "kms:ListGrants", 109 | "kms:RevokeGrant" 110 | ] 111 | resources = ["*"] 112 | principals { 113 | identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] 114 | type = "AWS" 115 | } 116 | condition { 117 | test = "Bool" 118 | values = ["true"] 119 | variable = "kms:GrantIsForAWSResource" 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /examples/single_vpc_logging/main.tf: -------------------------------------------------------------------------------- 1 | # --- examples/single_vpc_logging/main.tf --- 2 | 3 | # AWS Network Firewall 4 | module "network_firewall" { 5 | source = "../../" 6 | 7 | network_firewall_name = "anfw-${var.identifier}" 8 | network_firewall_description = "AWS Network Firewall - ${var.identifier}" 9 | network_firewall_policy = aws_networkfirewall_firewall_policy.anfw_policy.arn 10 | network_firewall_encryption_key_arn = aws_kms_key.encryption_key.arn 11 | 12 | vpc_id = module.vpc.vpc_attributes.id 13 | number_azs = var.vpc.number_azs 14 | vpc_subnets = { for k, v in module.vpc.private_subnet_attributes_by_az : split("/", k)[1] => v.id if split("/", k)[0] == "inspection" } 15 | 16 | routing_configuration = { 17 | single_vpc = { 18 | igw_route_table = aws_route_table.igw_route_table.id 19 | protected_subnet_route_tables = { for k, v in module.vpc.rt_attributes_by_type_by_az.public : k => v.id } 20 | protected_subnet_cidr_blocks = tomap({ 21 | for i, az in module.vpc.azs : az => var.vpc.protected_subnet_cidrs[i] 22 | }) 23 | } 24 | } 25 | 26 | logging_configuration = { 27 | flow_log = { 28 | cloudwatch_logs = { 29 | logGroupName = aws_cloudwatch_log_group.flow_lg.name 30 | } 31 | } 32 | alert_log = { 33 | cloudwatch_logs = { 34 | logGroupName = aws_cloudwatch_log_group.alert_lg.name 35 | } 36 | } 37 | } 38 | } 39 | 40 | # VPC Module - https://github.com/aws-ia/terraform-aws-vpc 41 | module "vpc" { 42 | source = "aws-ia/vpc/aws" 43 | version = "= 4.4.1" 44 | 45 | name = "single_vpc" 46 | cidr_block = var.vpc.cidr_block 47 | az_count = var.vpc.number_azs 48 | 49 | subnets = { 50 | public = { 51 | cidrs = var.vpc.protected_subnet_cidrs 52 | connect_to_igw = false 53 | } 54 | private = { cidrs = var.vpc.private_subnet_cidrs } 55 | inspection = { cidrs = var.vpc.inspection_subnet_cidrs } 56 | } 57 | } 58 | 59 | # Internet gateway route table 60 | resource "aws_route_table" "igw_route_table" { 61 | vpc_id = module.vpc.vpc_attributes.id 62 | 63 | tags = { 64 | Name = "igw-route-table" 65 | } 66 | } 67 | 68 | resource "aws_route_table_association" "igw_route_table_assoc" { 69 | gateway_id = module.vpc.internet_gateway.id 70 | route_table_id = aws_route_table.igw_route_table.id 71 | } 72 | 73 | # CloudWath Log Groups - for Flow and Alert 74 | resource "aws_cloudwatch_log_group" "alert_lg" { 75 | name = "alert-anfw-${var.identifier}" 76 | retention_in_days = 7 77 | kms_key_id = aws_kms_key.log_key.arn 78 | } 79 | 80 | resource "aws_cloudwatch_log_group" "flow_lg" { 81 | name = "flow-anfw-${var.identifier}" 82 | retention_in_days = 7 83 | kms_key_id = aws_kms_key.log_key.arn 84 | } 85 | -------------------------------------------------------------------------------- /examples/single_vpc_logging/outputs.tf: -------------------------------------------------------------------------------- 1 | # --- examples/single_vpc_logging/outputs.tf --- 2 | 3 | output "vpc" { 4 | description = "VPC ID." 5 | value = module.vpc.vpc_attributes.id 6 | } 7 | 8 | output "network_firewall" { 9 | description = "AWS Network Firewall ID." 10 | value = module.network_firewall.aws_network_firewall.id 11 | } 12 | -------------------------------------------------------------------------------- /examples/single_vpc_logging/policy.tf: -------------------------------------------------------------------------------- 1 | # --- examples/single_vpc_logging/policy.tf --- 2 | 3 | resource "aws_networkfirewall_firewall_policy" "anfw_policy" { 4 | name = "policy-firewall-${var.identifier}" 5 | 6 | firewall_policy { 7 | 8 | # Stateless configuration 9 | stateless_default_actions = ["aws:forward_to_sfe"] 10 | stateless_fragment_default_actions = ["aws:forward_to_sfe"] 11 | 12 | stateless_rule_group_reference { 13 | priority = 10 14 | resource_arn = aws_networkfirewall_rule_group.drop_remote.arn 15 | } 16 | 17 | # Stateful configuration 18 | stateful_engine_options { 19 | rule_order = "STRICT_ORDER" 20 | } 21 | stateful_default_actions = ["aws:drop_strict", "aws:alert_strict"] 22 | stateful_rule_group_reference { 23 | priority = 10 24 | resource_arn = aws_networkfirewall_rule_group.allow_icmp.arn 25 | } 26 | stateful_rule_group_reference { 27 | priority = 20 28 | resource_arn = aws_networkfirewall_rule_group.allow_domains.arn 29 | } 30 | } 31 | } 32 | 33 | # Stateless Rule Group - Dropping any SSH or RDP connection 34 | resource "aws_networkfirewall_rule_group" "drop_remote" { 35 | capacity = 2 36 | name = "drop-remote-${var.identifier}" 37 | type = "STATELESS" 38 | rule_group { 39 | rules_source { 40 | stateless_rules_and_custom_actions { 41 | 42 | stateless_rule { 43 | priority = 1 44 | rule_definition { 45 | actions = ["aws:drop"] 46 | match_attributes { 47 | protocols = [6] 48 | source { 49 | address_definition = "0.0.0.0/0" 50 | } 51 | source_port { 52 | from_port = 22 53 | to_port = 22 54 | } 55 | destination { 56 | address_definition = "0.0.0.0/0" 57 | } 58 | destination_port { 59 | from_port = 22 60 | to_port = 22 61 | } 62 | } 63 | } 64 | } 65 | 66 | stateless_rule { 67 | priority = 2 68 | rule_definition { 69 | actions = ["aws:drop"] 70 | match_attributes { 71 | protocols = [27] 72 | source { 73 | address_definition = "0.0.0.0/0" 74 | } 75 | destination { 76 | address_definition = "0.0.0.0/0" 77 | } 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | 86 | # Stateful Rule Group 1 - Allowing ICMP traffic 87 | resource "aws_networkfirewall_rule_group" "allow_icmp" { 88 | capacity = 100 89 | name = "allow-icmp-${var.identifier}" 90 | type = "STATEFUL" 91 | rule_group { 92 | rules_source { 93 | rules_string = <
"cidr_block": "10.129.0.0/16",
"inspection_subnet_cidrs": [
"10.129.0.0/24",
"10.129.1.0/24"
],
"number_azs": 2,
"private_subnet_cidrs": [
"10.129.4.0/24",
"10.129.5.0/24"
],
"protected_subnet_cidrs": [
"10.129.2.0/24",
"10.129.3.0/24"
]
}