Skip to content

Instantly share code, notes, and snippets.

@geekzter
Last active May 24, 2021 09:02
Show Gist options
  • Save geekzter/fbde59a8312d8cb562e50f70283fc4a5 to your computer and use it in GitHub Desktop.
Save geekzter/fbde59a8312d8cb562e50f70283fc4a5 to your computer and use it in GitHub Desktop.
Terraform VNet peering from Azure VM scale set agent
data azurerm_virtual_network peered_network {
name = element(split("/",var.peer_network_id),length(split("/",var.peer_network_id))-1)
resource_group_name = element(split("/",var.peer_network_id),length(split("/",var.peer_network_id))-5)
count = var.peer_network_id == "" ? 0 : 1
}
resource azurerm_virtual_network_peering peer_to_network {
name = "${azurerm_virtual_network.network.name}-from-peer"
resource_group_name = data.azurerm_virtual_network.peered_network.0.resource_group_name
virtual_network_name = data.azurerm_virtual_network.peered_network.0.name
remote_virtual_network_id = azurerm_virtual_network.network.id
allow_forwarded_traffic = true
allow_gateway_transit = var.peer_network_has_gateway
allow_virtual_network_access = true
use_remote_gateways = false
count = var.peer_network_id == "" ? 0 : 1
}
resource azurerm_virtual_network_peering network_to_peer {
name = "${azurerm_virtual_network.network.name}-to-peer"
resource_group_name = azurerm_virtual_network.network.resource_group_name
virtual_network_name = azurerm_virtual_network.network.name
remote_virtual_network_id = data.azurerm_virtual_network.peered_network.0.id
allow_forwarded_traffic = true
allow_gateway_transit = false
allow_virtual_network_access = true
use_remote_gateways = var.peer_network_has_gateway
count = var.peer_network_id == "" ? 0 : 1
depends_on = [azurerm_virtual_network_peering.peer_to_network]
}
resource azurerm_private_dns_zone_virtual_network_link api_server_domain {
name = "${local.peer_network_name}-zone-link"
resource_group_name = azurerm_kubernetes_cluster.aks.node_resource_group
private_dns_zone_name = azurerm_private_dns_zone.api_server_domain.name
virtual_network_id = var.peer_network_id
count = var.peer_network_id != "" ? 1 : 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment