Azure Terraform – How to Get Public IP of Windows VM

azureazure-networkingterraform

I have been trying to figure out how to get the public IP of a newly provisioned VM in Azure.

I use the example in:
https://www.terraform.io/docs/providers/azurerm/d/public_ip.html

However it doesnt work:

provider "azurerm" {
  version = "= 2.0.0"
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "test-resources"
  location = "West US 2"
}

resource "azurerm_virtual_network" "example" {
  name                = "test-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "acctsub"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefix     = "10.0.2.0/24"
}

resource "azurerm_public_ip" "example" {
  name                    = "test-pip"
  location                = azurerm_resource_group.example.location
  resource_group_name     = azurerm_resource_group.example.name
  allocation_method       = "Dynamic"
  idle_timeout_in_minutes = 30

  tags = {
    environment = "test"
  }
}

resource "azurerm_network_interface" "example" {
  name                = "test-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Static"
    private_ip_address            = "10.0.2.5"
    public_ip_address_id          = azurerm_public_ip.example.id
  }
}

resource "azurerm_windows_virtual_machine" "example" {
  name                  = "myvm1"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.example.id]
  size                  = "Standard_B1s"
  admin_username        = "adminuser"
  admin_password        = "Password123!"

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2019-Datacenter"
    version   = "latest"
  }

  os_disk {
    caching           = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
}

data "azurerm_public_ip" "example" {
  name                = azurerm_public_ip.example.name
  resource_group_name = azurerm_windows_virtual_machine.example.resource_group_name
}

output "public_ip_address" {
  value = data.azurerm_public_ip.example.ip_address
}

yields:

------------------------------------------------------------------------

Error: Error: Public IP "test-pip" (Resource Group "test-resources") was not found

  on Test.tf line 73, in data "azurerm_public_ip" "example":
  73: data "azurerm_public_ip" "example" {

Best Answer

This is really weird, but the example on terraform does not work, however reading: https://learn.hashicorp.com/tutorials/terraform/azure-dependency?in=terraform/azure-get-started

I can see that in this example they add a dependency like this:

data "azurerm_public_ip" "example" {
  name                = azurerm_public_ip.example.name
  resource_group_name = azurerm_windows_virtual_machine.example.resource_group_name
  depends_on          = [azurerm_windows_virtual_machine.example]
}

Doing that solves the problem and the example code now works....

Related Topic