Bryanzab
Bryanzab

Reputation: 15

Azure VM Convert Dynamic IP to Static Using Terraform

My customer would like to convert the private IP addresses that are dynamically assigned to their VM NICs to static using the address that was dynamically assigned using the Azure RM Terraform providers. I've seen examples in ARM where the NIC to static is in a nested ARM template but how can that be done using Terraform?

Our initial attempts at converting the private IP dynamic address to static results in our VM composition creating two NICs even if the previous name of the NIC is used.

Is there a way to update the NIC configuration without running the composition twice?

Appreciate any thoughts on this. Bryan

Upvotes: 1

Views: 1103

Answers (1)

Nancy Xiong
Nancy Xiong

Reputation: 28284

To convert a dynamic private IP address to a static one, we could invoke scripts with terraform to manage it because Azure does not assign a Dynamic IP Address until the Network Interface is attached to a running Virtual Machine (or other resource).

For example, you can use local-exec Provisioner to invoke a local executable after a resource is created.

resource "null_resource" "example" {

    provisioner "local-exec" {

   command = <<EOT

      $Nic = Get-AzNetworkInterface -ResourceGroupName ${azurerm_resource_group.main.name} -Name ${azurerm_network_interface.nic.name}
      $Nic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"
      Set-AzNetworkInterface -NetworkInterface $Nic
   EOT
   
   interpreter = ["PowerShell", "-Command"]
  
  }
}

Here is an answer I post before you may get references.

Upvotes: 2

Related Questions