From fe4c51f008dd2b76967dd08ff8e383fcd3fde3a1 Mon Sep 17 00:00:00 2001 From: kewalaka Date: Wed, 29 May 2024 10:21:33 +1200 Subject: [PATCH 1/2] feat: add example illustrating mapping multiple nsgs --- README.md | 4 +- .../README.md | 238 ++++++++++++++++++ .../_footer.md | 4 + .../_header.md | 3 + .../subnets_shared_nsgs_default_route/main.tf | 160 ++++++++++++ main.tf | 6 +- 6 files changed, 409 insertions(+), 6 deletions(-) create mode 100644 examples/subnets_shared_nsgs_default_route/README.md create mode 100644 examples/subnets_shared_nsgs_default_route/_footer.md create mode 100644 examples/subnets_shared_nsgs_default_route/_header.md create mode 100644 examples/subnets_shared_nsgs_default_route/main.tf diff --git a/README.md b/README.md index 94c1f16..06ad44d 100644 --- a/README.md +++ b/README.md @@ -339,9 +339,9 @@ Version: 0.2.0 ### [subnets](#module\_subnets) -Source: git::https://github.com/kewalaka/terraform-azurerm-avm-res-network-virtualnetwork//modules/subnet +Source: Azure/avm-res-network-virtualnetwork/azurerm//modules/subnet -Version: feat/use-azapi-for-subnets +Version: 0.2.1 ## Data Collection diff --git a/examples/subnets_shared_nsgs_default_route/README.md b/examples/subnets_shared_nsgs_default_route/README.md new file mode 100644 index 0000000..6975095 --- /dev/null +++ b/examples/subnets_shared_nsgs_default_route/README.md @@ -0,0 +1,238 @@ + +# Default example + +This deploys the module illustrating how to use NSG across mutiple subnets, with a pre-existing route table which defines the default route. + +```hcl +terraform { + required_version = "~> 1.5" + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 3.74" + } + random = { + source = "hashicorp/random" + version = "~> 3.5" + } + } +} + +provider "azurerm" { + features {} +} + + +## Section to provide a random Azure region for the resource group +# This allows us to randomize the region for the resource group. +module "regions" { + source = "Azure/regions/azurerm" + version = "~> 0.3" +} + +# This allows us to randomize the region for the resource group. +resource "random_integer" "region_index" { + max = length(module.regions.regions) - 1 + min = 0 +} +## End of section to provide a random Azure region for the resource group + +# This ensures we have unique CAF compliant names for our resources. +module "naming" { + source = "Azure/naming/azurerm" + version = "~> 0.3" +} + +# This is required for resource modules +resource "azurerm_resource_group" "this" { + location = module.regions.regions[random_integer.region_index.result].name + name = module.naming.resource_group.name_unique +} + +resource "azurerm_virtual_network" "this" { + address_space = ["10.0.0.0/16"] + location = azurerm_resource_group.this.location + name = module.naming.virtual_network.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_virtual_network" "second_net" { + address_space = ["10.1.0.0/16"] + location = azurerm_resource_group.this.location + name = module.naming.virtual_network.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_route_table" "default_route" { + location = azurerm_resource_group.this.location + name = module.naming.route_table.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_route" "default_route" { + address_prefix = "10.0.0.0/16" + name = module.naming.route.name_unique + next_hop_type = "VirtualAppliance" + next_hop_in_ip_address = "10.0.0.5" + resource_group_name = azurerm_resource_group.this.name + route_table_name = azurerm_route_table.default_route.name +} + +locals { + network_security_groups = { + nsg0 = { + name = "${module.naming.network_security_group.name_unique}0" + security_rules = { + "http_https_outbound" = { + access = "Allow" + name = "httphttpsOutbound" + direction = "Outbound" + priority = 150 + protocol = "Tcp" + source_address_prefix = "*" + source_port_range = "*" + destination_address_prefix = "*" + destination_port_ranges = [80, 443] + }, + "deny_all_outbound" = { + access = "Deny" + direction = "Inbound" + name = "deny_all_outbound" + priority = 4096 + protocol = "Tcp" + destination_address_prefix = "*" + destination_port_range = "*" + source_address_prefix = "*" + source_port_range = "*" + } + } + } + nsg1 = { + name = "${module.naming.network_security_group.name_unique}1" + security_rules = { + "https_inbound" = { + access = "Allow" + name = "httpsInbound" + direction = "Inbound" + priority = 150 + protocol = "Tcp" + source_address_prefix = "*" + source_port_range = "*" + destination_address_prefix = "*" + destination_port_range = 443 + } + } + } + } + subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.0.0.0/24"] + network_security_group_key = "nsg0" + route_table = { + id = azurerm_route_table.default_route.id + } + }, + snet1 = { + name = "${module.naming.subnet.name_unique}1" + address_prefixes = ["10.0.1.0/24"] + network_security_group_key = "nsg0" + route_table = { + id = azurerm_route_table.default_route.id + } + }, + snet2 = { + name = "${module.naming.subnet.name_unique}2" + address_prefixes = ["10.0.2.0/24"] + network_security_group_key = "nsg1" + route_table = { + id = azurerm_route_table.default_route.id + } + } + } +} + +module "this" { + source = "../../" + # source = "Azure/avm-ptn-subnets/azurerm" + # version = "..." + location = azurerm_resource_group.this.location + resource_group_name = azurerm_resource_group.this.name + virtual_network_resource_id = azurerm_virtual_network.this.id + + network_security_groups = local.network_security_groups + subnets = local.subnets + +} +``` + + +## Requirements + +The following requirements are needed by this module: + +- [terraform](#requirement\_terraform) (~> 1.5) + +- [azurerm](#requirement\_azurerm) (~> 3.74) + +- [random](#requirement\_random) (~> 3.5) + +## Providers + +The following providers are used by this module: + +- [azurerm](#provider\_azurerm) (~> 3.74) + +- [random](#provider\_random) (~> 3.5) + +## Resources + +The following resources are used by this module: + +- [azurerm_resource_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) (resource) +- [azurerm_route.default_route](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/route) (resource) +- [azurerm_route_table.default_route](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/route_table) (resource) +- [azurerm_virtual_network.second_net](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) (resource) +- [azurerm_virtual_network.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) (resource) +- [random_integer.region_index](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/integer) (resource) + + +## Required Inputs + +No required inputs. + +## Optional Inputs + +No optional inputs. + +## Outputs + +No outputs. + +## Modules + +The following Modules are called: + +### [naming](#module\_naming) + +Source: Azure/naming/azurerm + +Version: ~> 0.3 + +### [regions](#module\_regions) + +Source: Azure/regions/azurerm + +Version: ~> 0.3 + +### [this](#module\_this) + +Source: ../../ + +Version: + + +## Data Collection + +The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at . You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices. + \ No newline at end of file diff --git a/examples/subnets_shared_nsgs_default_route/_footer.md b/examples/subnets_shared_nsgs_default_route/_footer.md new file mode 100644 index 0000000..bc56bcb --- /dev/null +++ b/examples/subnets_shared_nsgs_default_route/_footer.md @@ -0,0 +1,4 @@ + +## Data Collection + +The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at . You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices. diff --git a/examples/subnets_shared_nsgs_default_route/_header.md b/examples/subnets_shared_nsgs_default_route/_header.md new file mode 100644 index 0000000..dc5916c --- /dev/null +++ b/examples/subnets_shared_nsgs_default_route/_header.md @@ -0,0 +1,3 @@ +# Default example + +This deploys the module illustrating how to use NSG across mutiple subnets, with a pre-existing route table which defines the default route. diff --git a/examples/subnets_shared_nsgs_default_route/main.tf b/examples/subnets_shared_nsgs_default_route/main.tf new file mode 100644 index 0000000..2845289 --- /dev/null +++ b/examples/subnets_shared_nsgs_default_route/main.tf @@ -0,0 +1,160 @@ +terraform { + required_version = "~> 1.5" + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 3.74" + } + random = { + source = "hashicorp/random" + version = "~> 3.5" + } + } +} + +provider "azurerm" { + features {} +} + + +## Section to provide a random Azure region for the resource group +# This allows us to randomize the region for the resource group. +module "regions" { + source = "Azure/regions/azurerm" + version = "~> 0.3" +} + +# This allows us to randomize the region for the resource group. +resource "random_integer" "region_index" { + max = length(module.regions.regions) - 1 + min = 0 +} +## End of section to provide a random Azure region for the resource group + +# This ensures we have unique CAF compliant names for our resources. +module "naming" { + source = "Azure/naming/azurerm" + version = "~> 0.3" +} + +# This is required for resource modules +resource "azurerm_resource_group" "this" { + location = module.regions.regions[random_integer.region_index.result].name + name = module.naming.resource_group.name_unique +} + +resource "azurerm_virtual_network" "this" { + address_space = ["10.0.0.0/16"] + location = azurerm_resource_group.this.location + name = module.naming.virtual_network.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_virtual_network" "second_net" { + address_space = ["10.1.0.0/16"] + location = azurerm_resource_group.this.location + name = module.naming.virtual_network.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_route_table" "default_route" { + location = azurerm_resource_group.this.location + name = module.naming.route_table.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_route" "default_route" { + address_prefix = "10.0.0.0/16" + name = module.naming.route.name_unique + next_hop_type = "VirtualAppliance" + next_hop_in_ip_address = "10.0.0.5" + resource_group_name = azurerm_resource_group.this.name + route_table_name = azurerm_route_table.default_route.name +} + +locals { + network_security_groups = { + nsg0 = { + name = "${module.naming.network_security_group.name_unique}0" + security_rules = { + "http_https_outbound" = { + access = "Allow" + name = "httphttpsOutbound" + direction = "Outbound" + priority = 150 + protocol = "Tcp" + source_address_prefix = "*" + source_port_range = "*" + destination_address_prefix = "*" + destination_port_ranges = [80, 443] + }, + "deny_all_outbound" = { + access = "Deny" + direction = "Inbound" + name = "deny_all_outbound" + priority = 4096 + protocol = "Tcp" + destination_address_prefix = "*" + destination_port_range = "*" + source_address_prefix = "*" + source_port_range = "*" + } + } + } + nsg1 = { + name = "${module.naming.network_security_group.name_unique}1" + security_rules = { + "https_inbound" = { + access = "Allow" + name = "httpsInbound" + direction = "Inbound" + priority = 150 + protocol = "Tcp" + source_address_prefix = "*" + source_port_range = "*" + destination_address_prefix = "*" + destination_port_range = 443 + } + } + } + } + subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.0.0.0/24"] + network_security_group_key = "nsg0" + route_table = { + id = azurerm_route_table.default_route.id + } + }, + snet1 = { + name = "${module.naming.subnet.name_unique}1" + address_prefixes = ["10.0.1.0/24"] + network_security_group_key = "nsg0" + route_table = { + id = azurerm_route_table.default_route.id + } + }, + snet2 = { + name = "${module.naming.subnet.name_unique}2" + address_prefixes = ["10.0.2.0/24"] + network_security_group_key = "nsg1" + route_table = { + id = azurerm_route_table.default_route.id + } + } + } +} + +module "this" { + source = "../../" + # source = "Azure/avm-ptn-subnets/azurerm" + # version = "..." + location = azurerm_resource_group.this.location + resource_group_name = azurerm_resource_group.this.name + virtual_network_resource_id = azurerm_virtual_network.this.id + + network_security_groups = local.network_security_groups + subnets = local.subnets + +} diff --git a/main.tf b/main.tf index 477ae1a..e36c105 100644 --- a/main.tf +++ b/main.tf @@ -1,10 +1,8 @@ module "subnets" { for_each = local.subnets - # TODO revert to Azure org pending fix: https://github.com/Azure/terraform-azurerm-avm-res-network-virtualnetwork/pull/74 - source = "git::https://github.com/kewalaka/terraform-azurerm-avm-res-network-virtualnetwork//modules/subnet?ref=feat/use-azapi-for-subnets" - # source = "Azure/avm-res-network-virtualnetwork/modules/subnet/azurerm" - # version = "0.2.0" + source = "Azure/avm-res-network-virtualnetwork/azurerm//modules/subnet" + version = "0.2.1" virtual_network = { resource_id = var.virtual_network_resource_id From 07ba6550dbbc5629e3eecda6008ef440898a6d18 Mon Sep 17 00:00:00 2001 From: kewalaka Date: Wed, 29 May 2024 10:37:55 +1200 Subject: [PATCH 2/2] fix: plural of security rule, optional params & linting --- README.md | 27 +++++++++---------- .../README.md | 2 +- .../subnets_shared_nsgs_default_route/main.tf | 2 +- variables.nsgs.tf | 25 +++++++++-------- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 06ad44d..7cfe028 100644 --- a/README.md +++ b/README.md @@ -110,8 +110,7 @@ Default: `true` ### [network\_security\_groups](#input\_network\_security\_groups) -Description: - `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created. -- `name` - (Required) Specifies the name of the network security group. Changing this forces a new resource to be created. +Description: - `name` - (Required) Specifies the name of the network security group. Changing this forces a new resource to be created. - `resource_group_name` - (Required) The name of the resource group in which to create the network security group. Changing this forces a new resource to be created. - `tags` - (Optional) A mapping of tags to assign to the resource. @@ -147,23 +146,23 @@ Type: map(object({ name = string tags = optional(map(string)) - security_rule = optional(map(object({ + security_rules = optional(map(object({ access = string - description = string - destination_address_prefix = string - destination_address_prefixes = set(string) - destination_application_security_group_ids = set(string) - destination_port_range = string - destination_port_ranges = set(string) + description = optional(string) + destination_address_prefix = optional(string) + destination_address_prefixes = optional(set(string)) + destination_application_security_group_ids = optional(set(string)) + destination_port_range = optional(string) + destination_port_ranges = optional(set(string)) direction = string name = string priority = number protocol = string - source_address_prefix = string - source_address_prefixes = set(string) - source_application_security_group_ids = set(string) - source_port_range = string - source_port_ranges = set(string) + source_address_prefix = optional(string) + source_address_prefixes = optional(set(string)) + source_application_security_group_ids = optional(set(string)) + source_port_range = optional(string) + source_port_ranges = optional(set(string)) }))) timeouts = optional(object({ create = optional(string) diff --git a/examples/subnets_shared_nsgs_default_route/README.md b/examples/subnets_shared_nsgs_default_route/README.md index 6975095..40f13c0 100644 --- a/examples/subnets_shared_nsgs_default_route/README.md +++ b/examples/subnets_shared_nsgs_default_route/README.md @@ -73,9 +73,9 @@ resource "azurerm_route" "default_route" { address_prefix = "10.0.0.0/16" name = module.naming.route.name_unique next_hop_type = "VirtualAppliance" - next_hop_in_ip_address = "10.0.0.5" resource_group_name = azurerm_resource_group.this.name route_table_name = azurerm_route_table.default_route.name + next_hop_in_ip_address = "10.0.0.5" } locals { diff --git a/examples/subnets_shared_nsgs_default_route/main.tf b/examples/subnets_shared_nsgs_default_route/main.tf index 2845289..6778e69 100644 --- a/examples/subnets_shared_nsgs_default_route/main.tf +++ b/examples/subnets_shared_nsgs_default_route/main.tf @@ -67,9 +67,9 @@ resource "azurerm_route" "default_route" { address_prefix = "10.0.0.0/16" name = module.naming.route.name_unique next_hop_type = "VirtualAppliance" - next_hop_in_ip_address = "10.0.0.5" resource_group_name = azurerm_resource_group.this.name route_table_name = azurerm_route_table.default_route.name + next_hop_in_ip_address = "10.0.0.5" } locals { diff --git a/variables.nsgs.tf b/variables.nsgs.tf index 1acc1a5..453c407 100644 --- a/variables.nsgs.tf +++ b/variables.nsgs.tf @@ -2,23 +2,23 @@ variable "network_security_groups" { type = map(object({ name = string tags = optional(map(string)) - security_rule = optional(map(object({ + security_rules = optional(map(object({ access = string - description = string - destination_address_prefix = string - destination_address_prefixes = set(string) - destination_application_security_group_ids = set(string) - destination_port_range = string - destination_port_ranges = set(string) + description = optional(string) + destination_address_prefix = optional(string) + destination_address_prefixes = optional(set(string)) + destination_application_security_group_ids = optional(set(string)) + destination_port_range = optional(string) + destination_port_ranges = optional(set(string)) direction = string name = string priority = number protocol = string - source_address_prefix = string - source_address_prefixes = set(string) - source_application_security_group_ids = set(string) - source_port_range = string - source_port_ranges = set(string) + source_address_prefix = optional(string) + source_address_prefixes = optional(set(string)) + source_application_security_group_ids = optional(set(string)) + source_port_range = optional(string) + source_port_ranges = optional(set(string)) }))) timeouts = optional(object({ create = optional(string) @@ -28,7 +28,6 @@ variable "network_security_groups" { })) })) description = <<-DESCRIPTION - - `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created. - `name` - (Required) Specifies the name of the network security group. Changing this forces a new resource to be created. - `resource_group_name` - (Required) The name of the resource group in which to create the network security group. Changing this forces a new resource to be created. - `tags` - (Optional) A mapping of tags to assign to the resource.