From b093046de57a70b1f7be02b3d45ff6c3b022818b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Marmol?= Date: Mon, 27 Jul 2020 09:07:58 +0200 Subject: [PATCH] Add route tables to subnets (#7) * Reset to master upstream and add route tables associations * Fix fmt * Add example --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++++++ main.tf | 6 ++++++ test/fixture/main.tf | 10 ++++++++++ variables.tf | 6 ++++++ 4 files changed, 69 insertions(+) diff --git a/README.md b/README.md index b24757e..e8e5bff 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,53 @@ resource "azurerm_network_security_group" "ssh" { } ``` +## Example adding a route table + +```hcl +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "example" { + name = "my-resources" + location = "West Europe" +} + +module "vnet" { + source = "Azure/vnet/azurerm" + resource_group_name = azurerm_resource_group.example.name + address_space = ["10.0.0.0/16"] + subnet_prefixes = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] + subnet_names = ["subnet1", "subnet2", "subnet3"] + + route_table_ids = { + subnet1 = azurerm_route_table.example.id + subnet2 = azurerm_route_table.example.id + subnet3 = azurerm_roiute_table.example.id + } + + + tags = { + environment = "dev" + costcenter = "it" + } +} + +resource "azurerm_route_table" "example" { + location = azurerm_resource_group.example.location + name = "MyRouteTable" + resource_group_name = azurerm_resource_group.example.name +} + +resource "azurerm_route" "example" { + name = "acceptanceTestRoute1" + resource_group_name = azurerm_resource_group.example.name + route_table_name = azurerm_route_table.example.name + address_prefix = "10.1.0.0/16" + next_hop_type = "vnetlocal" +} + +``` ## Test ### Configurations diff --git a/main.tf b/main.tf index aeadb27..ce0daf1 100644 --- a/main.tf +++ b/main.tf @@ -33,4 +33,10 @@ resource "azurerm_subnet_network_security_group_association" "vnet" { for_each = var.nsg_ids subnet_id = data.azurerm_subnet.import[each.key].id network_security_group_id = each.value +} + +resource "azurerm_subnet_route_table_association" "vnet" { + for_each = var.route_tables_ids + route_table_id = each.value + subnet_id = data.azurerm_subnet.import[each.key].id } \ No newline at end of file diff --git a/test/fixture/main.tf b/test/fixture/main.tf index 6d01723..2259735 100644 --- a/test/fixture/main.tf +++ b/test/fixture/main.tf @@ -17,6 +17,12 @@ resource "azurerm_network_security_group" "nsg1" { location = azurerm_resource_group.test.location } +resource "azurerm_route_table" "rt1" { + location = azurerm_resource_group.test.location + name = "test-${random_id.rg_name.hex}-rt" + resource_group_name = azurerm_resource_group.test.name +} + module "vnet" { source = "../../" resource_group_name = azurerm_resource_group.test.name @@ -28,6 +34,10 @@ module "vnet" { subnet1 = azurerm_network_security_group.nsg1.id } + route_tables_ids = { + subnet1 = azurerm_route_table.rt1.id + } + tags = { environment = "dev" costcenter = "it" diff --git a/variables.tf b/variables.tf index 4efb621..37ff884 100644 --- a/variables.tf +++ b/variables.tf @@ -37,6 +37,12 @@ variable "nsg_ids" { } } +variable "route_tables_ids" { + description = "A map of subnet name to Route table ids" + type = map(string) + default = {} +} + variable "tags" { description = "The tags to associate with your network and subnets." type = map(string)