diff --git a/main.tf b/main.tf index 09ec309..5171079 100644 --- a/main.tf +++ b/main.tf @@ -198,6 +198,14 @@ resource "azurerm_public_ip" "vm" { tags = var.tags } +// Dynamic public ip address will be got after it's assigned to a vm +data "azurerm_public_ip" "vm" { + count = var.nb_public_ip + name = azurerm_public_ip.vm[count.index].name + resource_group_name = data.azurerm_resource_group.vm.name + depends_on = [azurerm_virtual_machine.vm-linux, azurerm_virtual_machine.vm-windows] +} + resource "azurerm_network_security_group" "vm" { name = "${var.vm_hostname}-nsg" resource_group_name = data.azurerm_resource_group.vm.name diff --git a/outputs.tf b/outputs.tf index f15a08f..b21e103 100644 --- a/outputs.tf +++ b/outputs.tf @@ -30,7 +30,7 @@ output "public_ip_id" { output "public_ip_address" { description = "The actual ip address allocated for the resource." - value = azurerm_public_ip.vm.*.ip_address + value = data.azurerm_public_ip.vm.*.ip_address } output "public_ip_dns_name" {