diff --git a/README.md b/README.md index 455a836..94c1f16 100644 --- a/README.md +++ b/README.md @@ -317,11 +317,15 @@ The following outputs are exported: ### [network\_security\_groups](#output\_network\_security\_groups) -Description: This is the full output for the resource. +Description: A map of all network security groups created. + +### [route\_tables](#output\_route\_tables) + +Description: A map of all route tables created. ### [subnets](#output\_subnets) -Description: This is the full output for the resource. +Description: A map of all subnets created. ## Modules diff --git a/examples/default/_header.md b/examples/default/_header.md deleted file mode 100644 index 9eb0b85..0000000 --- a/examples/default/_header.md +++ /dev/null @@ -1,3 +0,0 @@ -# Default example - -This deploys the module in its simplest form. diff --git a/examples/subnet_only/README.md b/examples/subnet_only/README.md new file mode 100644 index 0000000..4b0b8f7 --- /dev/null +++ b/examples/subnet_only/README.md @@ -0,0 +1,206 @@ + +# Default example + +This deploys the module with a subnet and existing route table & NSG. + +```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.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" "this" { + location = azurerm_resource_group.this.location + name = module.naming.route_table.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_route" "this" { + address_prefix = "10.3.0.0/16" + name = module.naming.route.name_unique + next_hop_type = "VnetLocal" + resource_group_name = azurerm_resource_group.this.name + route_table_name = azurerm_route_table.this.name +} + +resource "azurerm_network_security_group" "this" { + location = azurerm_resource_group.this.location + name = module.naming.network_security_group.name_unique + resource_group_name = azurerm_resource_group.this.name + tags = { + environment = "Demo" + } + + security_rule { + access = "Allow" + destination_address_prefix = "*" + destination_port_range = "*" + direction = "Inbound" + name = "test123" + priority = 100 + protocol = "Tcp" + source_address_prefix = "*" + source_port_range = "*" + } +} + +locals { + subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.1.0.0/24"] + network_security_group = { + id = azurerm_network_security_group.this.id + } + route_table = { + id = azurerm_route_table.this.id + } + }, + snet1 = { + name = "${module.naming.subnet.name_unique}1" + address_prefixes = ["10.1.1.0/24"] + network_security_group = { + id = azurerm_network_security_group.this.id + } + } + snet2 = { + name = "${module.naming.subnet.name_unique}2" + address_prefixes = ["10.1.2.0/24"] + route_table = { + id = azurerm_route_table.this.id + } + } + } +} + +# This is the module call +module "test" { + 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 + + 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_network_security_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group) (resource) +- [azurerm_resource_group.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) (resource) +- [azurerm_route.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/route) (resource) +- [azurerm_route_table.this](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/route_table) (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 + +### [test](#module\_test) + +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/default/_footer.md b/examples/subnet_only/_footer.md similarity index 100% rename from examples/default/_footer.md rename to examples/subnet_only/_footer.md diff --git a/examples/subnet_only/_header.md b/examples/subnet_only/_header.md new file mode 100644 index 0000000..dfd4392 --- /dev/null +++ b/examples/subnet_only/_header.md @@ -0,0 +1,3 @@ +# Default example + +This deploys the module with a subnet and existing route table & NSG. diff --git a/examples/subnet_only/main.tf b/examples/subnet_only/main.tf new file mode 100644 index 0000000..99c63ea --- /dev/null +++ b/examples/subnet_only/main.tf @@ -0,0 +1,128 @@ +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.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" "this" { + location = azurerm_resource_group.this.location + name = module.naming.route_table.name_unique + resource_group_name = azurerm_resource_group.this.name +} + +resource "azurerm_route" "this" { + address_prefix = "10.3.0.0/16" + name = module.naming.route.name_unique + next_hop_type = "VnetLocal" + resource_group_name = azurerm_resource_group.this.name + route_table_name = azurerm_route_table.this.name +} + +resource "azurerm_network_security_group" "this" { + location = azurerm_resource_group.this.location + name = module.naming.network_security_group.name_unique + resource_group_name = azurerm_resource_group.this.name + tags = { + environment = "Demo" + } + + security_rule { + access = "Allow" + destination_address_prefix = "*" + destination_port_range = "*" + direction = "Inbound" + name = "test123" + priority = 100 + protocol = "Tcp" + source_address_prefix = "*" + source_port_range = "*" + } +} + +locals { + subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.1.0.0/24"] + network_security_group = { + id = azurerm_network_security_group.this.id + } + route_table = { + id = azurerm_route_table.this.id + } + }, + snet1 = { + name = "${module.naming.subnet.name_unique}1" + address_prefixes = ["10.1.1.0/24"] + network_security_group = { + id = azurerm_network_security_group.this.id + } + } + snet2 = { + name = "${module.naming.subnet.name_unique}2" + address_prefixes = ["10.1.2.0/24"] + route_table = { + id = azurerm_route_table.this.id + } + } + } +} + +# This is the module call +module "test" { + 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 + + subnets = local.subnets + +} diff --git a/examples/default/README.md b/examples/subnets_nsgs_and_routes/README.md similarity index 85% rename from examples/default/README.md rename to examples/subnets_nsgs_and_routes/README.md index faaf636..ba99a89 100644 --- a/examples/default/README.md +++ b/examples/subnets_nsgs_and_routes/README.md @@ -1,7 +1,46 @@ # Default example -This deploys the module in its simplest form. +This deploys the module illustrating how to use both existing NSG, routes, and ones created within the module. + +An existing route table can be passed like this: + +```terraform +subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.0.0.0/24"] + route_table = { + id = azurerm_route_table.this.id + } + }, +} +``` + +If you want the module to create the route table and associate it, this is done like so: + +```terraform +route_tables = { + rt0 = { + name = "${module.naming.route_table.name_unique}-created" + routes = { + address_prefix = "1.2.3.4/24" + name = "${module.naming.route.name_unique}-created" + next_hop_type = "Internet" + } + } +} +subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}1" + address_prefixes = ["10.0.1.0/24"] + # the route table is referenced by its map key. + route_table_key = "rt0" + } +} +``` + +The same approach applies to network security groups. ```hcl terraform { @@ -101,9 +140,8 @@ locals { } subnets = { snet0 = { - name = "${module.naming.subnet.name_unique}0" - address_prefixes = ["10.0.0.0/24"] - network_security_group_key = "nsg0" + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.0.0.0/24"] route_table = { id = azurerm_route_table.this.id } diff --git a/examples/subnets_nsgs_and_routes/_footer.md b/examples/subnets_nsgs_and_routes/_footer.md new file mode 100644 index 0000000..bc56bcb --- /dev/null +++ b/examples/subnets_nsgs_and_routes/_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_nsgs_and_routes/_header.md b/examples/subnets_nsgs_and_routes/_header.md new file mode 100644 index 0000000..32b79c6 --- /dev/null +++ b/examples/subnets_nsgs_and_routes/_header.md @@ -0,0 +1,42 @@ +# Default example + +This deploys the module illustrating how to use both existing NSG, routes, and ones created within the module. + +An existing route table can be passed like this: + +```terraform +subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.0.0.0/24"] + route_table = { + id = azurerm_route_table.this.id + } + }, +} +``` + +If you want the module to create the route table and associate it, this is done like so: + +```terraform +route_tables = { + rt0 = { + name = "${module.naming.route_table.name_unique}-created" + routes = { + address_prefix = "1.2.3.4/24" + name = "${module.naming.route.name_unique}-created" + next_hop_type = "Internet" + } + } +} +subnets = { + snet0 = { + name = "${module.naming.subnet.name_unique}1" + address_prefixes = ["10.0.1.0/24"] + # the route table is referenced by its map key. + route_table_key = "rt0" + } +} +``` + +The same approach applies to network security groups. diff --git a/examples/default/main.tf b/examples/subnets_nsgs_and_routes/main.tf similarity index 95% rename from examples/default/main.tf rename to examples/subnets_nsgs_and_routes/main.tf index 35b3df3..d4cf5ed 100644 --- a/examples/default/main.tf +++ b/examples/subnets_nsgs_and_routes/main.tf @@ -95,9 +95,8 @@ locals { } subnets = { snet0 = { - name = "${module.naming.subnet.name_unique}0" - address_prefixes = ["10.0.0.0/24"] - network_security_group_key = "nsg0" + name = "${module.naming.subnet.name_unique}0" + address_prefixes = ["10.0.0.0/24"] route_table = { id = azurerm_route_table.this.id } diff --git a/outputs.tf b/outputs.tf index d2825ed..2292d8a 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,9 +1,14 @@ output "network_security_groups" { - description = "This is the full output for the resource." + description = "A map of all network security groups created." value = module.network_security_groups } +output "route_tables" { + description = "A map of all route tables created." + value = azurerm_route_table.this +} + output "subnets" { - description = "This is the full output for the resource." + description = "A map of all subnets created." value = module.subnets }