I have an NIC configured like this
resource "azurerm_network_interface" "nic" {
name = "nic1"
location = "West Europe"
resource_group_name = "MyRG"
enable_ip_forwarding = true
ip_configuration {
name = "ipconfig1"
subnet_id = azurerm_subnet.publicsubnet.id
private_ip_address_allocation = "Static"
private_ip_address = "10.0.0.10"
public_ip_address_id = azurerm_public_ip.ClusterPublicIP.id
}
}
Which has a public IP assigned and is eventually used by a Firewall in Azure. However, I would like to add multiple IP-Addresses to this NIC. The IP-Addresses should be added, when I create an application. So I am able to create the public IP with:
resource "azurerm_public_ip" "pip" {
allocation_method = "Static"
location = "West Europe"
name = "pip-${var.appname}"
resource_group_name = var.rgName
}
However, I could not find any resources in the azurerm provider which would allow me to add it to the previously defined nic. The ip_configuration block must be embedded in azurerm_network_interface.
So I tried it with the azapi provider as follows:
resource "azapi_update_resource" "add_pip_to_forti" {
type = "Microsoft.Network/networkInterfaces@2023-06-01"
resource_id = var.nicResourceId
body = jsonencode({
properties = {
ipConfigurations = [
{
name = "assignedByTF"
etag = "W/\"50b5631e-6e90-4196-a2df-d5c280c41b73\""
type = "Microsoft.Network/networkInterfaces/ipConfigurations"
properties = {
privateIPAddress = "10.0.0.200"
privateIPAllocationMethod = "Static"
publicIPAddress = {
id = azurerm_network_interface.nic.id
}
subnet = {
id = "/subscriptions/my-sub/resourceGroups/my-rg/providers/Microsoft.Network/virtualNetworks/hub-europe/subnets/pubsub"
}
primary = false
privateIPAddressVersion = "IPv4"
}
}
]
}
})
}
However, this would delete the existing configuration on the nic, and fails as the primary ip_configuration cannot be deleted.
The question is:
(How) Can the block resource "azapi_update_resource" "add_pip_to_forti" to extend the array and keep existing values by using terraform?
Thanks.
While not really a nice solution, it's working. What I ended up doing is the following:
What happens is that I load the current resouce state to
nic_before_change, then via a custom script in thenull_resource.assign_ipconfig_to_nicanalyze if the nic needs to be changed. In my usecase, if the ipconfig must be added for which-ever reason. This resource is also responsible for deleting it upon destruction.Eventually, to be able to reference the nics latest state, I import it again to
nic_after_change. As this depends onnull_resource.assign_ipconfig_tonic, the resource shows the latest state.