Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In order to delete the public IP, disassociate/detach the Public IP address from the resource. #107

Closed
mrak- opened this issue Jul 1, 2019 · 11 comments

Comments

@mrak-
Copy link

mrak- commented Jul 1, 2019

  • Terraform Version: 0.12.3
  • OS Version:

Bug description: i create infrastructure, with public ip, now i want to remove public ip
when I change the value nb_public_ip = 1 to 0, I get this error:
Error: Error deleting Public IP "myvm-0-publicIP" (Resource Group "studytask"): network.PublicIPAddressesClient#Delete: Failure sending request: StatusCode=400 -- Original Error: Code="PublicIPAddressCannotBeDeleted" Message="Public IP address /subscriptions/ecfde0ed-ec43-4761-a254-c9bff2890753/resourceGroups/studytask/providers/Microsoft.Network/publicIPAddresses/myvm-0-publicIP can not be deleted since it is still allocated to resource /subscriptions/ecfde0ed-ec43-4761-a254-c9bff2890753/resourceGroups/studytask/providers/Microsoft.Network/networkInterfaces/nic-myvm-0/ipConfigurations/ipconfig0. In order to delete the public IP, disassociate/detach the Public IP address from the resource. To learn how to do this, see aka.ms/deletepublicip." Details=[]

Steps to reproduce:

  1. Create resource with public ip
  2. Remove public ip
@waltherg
Copy link

waltherg commented Jul 30, 2019

Terraform version 0.12.5

I created an Azure kubernetes cluster and public IP for it with depends_on of the public IP pointing at the Azure kubernetes cluster resource.

Removing the depends_on statement in the public IP definition alleviated this issue and allowed me to destroy both resources (Azure kubernetes cluster and public IP address).

I need to run terraform destroy twice for this to succeed though: on the first run the AKS cluster is destroyed with the IP removal failing with the above error. On a second run the IP is removed.

@zodrog
Copy link

zodrog commented Aug 27, 2019

Same issue here (Azure kubernetes cluster resource). I don't have an implicit depends_on (the depends is through output variables on module definitions), but the issue is the same.
Destroying twice does not work for me.

Cheers

@zagr0
Copy link

zagr0 commented Nov 15, 2019

Have same issue with Terraform v0.12.15 and terraform-provider-azurerm_v1.36.1_x4
when azr resources from module (instance, nic and public ip) are destroying terraform fails with dependency error:

module.azr1-p01-oxyaction.azurerm_virtual_machine.vm[0]: Still destroying... [id=/subscriptions/*****-...ute/virtualMachines/azr1-p01-oxyaction, 2m30s elapsed]
module.azr1-p01-oxyaction.azurerm_virtual_machine.vm[0]: Destruction complete after 2m32s

Error: Error deleting Network Interface "azr1-p01-oxyaction-nic" (Resource Group "sandbox"): network.InterfacesClient#Delete: Failure sending request: StatusCode=400 -- Original Error: Code="NicInUse" Message="Network Interface /subscriptions/*****/resourceGroups/sandbox/providers/Microsoft.Network/networkInterfaces/azr1-p01-oxyaction-nic is used by existing resource /subscriptions/*****/resourceGroups/sandbox/providers/Microsoft.Compute/virtualMachines/azr1-p01-oxyaction. In order to delete the network interface, it must be dissociated from the resource. To learn more, see aka.ms/deletenic." Details=[]

Error: Error deleting Public IP "azr1-p01-oxyaction-public-ip" (Resource Group "sandbox"): network.PublicIPAddressesClient#Delete: Failure sending request: StatusCode=400 -- Original Error: Code="PublicIPAddressCannotBeDeleted" Message="Public IP address /subscriptions/*****/resourceGroups/sandbox/providers/Microsoft.Network/publicIPAddresses/azr1-p01-oxyaction-public-ip can not be deleted since it is still allocated to resource /subscriptions/*****/resourceGroups/sandbox/providers/Microsoft.Network/networkInterfaces/azr1-p01-oxyaction-nic/ipConfigurations/azr1-p01-oxyaction-nic. In order to delete the public IP, disassociate/detach the Public IP address from the resource.  To learn how to do this, see aka.ms/deletepublicip." Details=[]

retry finishes with success

@cschipper1
Copy link

same issue on the azure firewall

@rgl
Copy link

rgl commented Mar 7, 2020

This also happens with terraform 0.12.23 and provider.azurerm v1.44.0 and repeating the destroy operation does not work.

@Bessonov
Copy link

Actually, not tested well, but it worked for the last 2 builds. We had the same issue with aks and public ip used for ingress in our automated apply/destroy test workflow. Because public ip must be in the same resource group like aks nodes (which is created by aks on the fly and can't be created through terraform), we are forced to have depens_on on aks.

In our observation the second run of destroy helps. But in our case it was enough to destroy ingress and wait for 30 seconds before run terraform destroy.

@yupwei68
Copy link
Contributor

Hi @mrak- ,thanks for opening this issue. I'd like to encourage you to raise an issue in hashicorp to solve this.

@TechArtistG
Copy link

Would this be the same issue?: hashicorp/terraform#25993

@TechArtistG
Copy link

I get this error with a Public IP associated with a Gateway. Looks like there are several steps involved:
https://social.msdn.microsoft.com/Forums/azure/en-US/3f5dfb96-3731-4349-8283-a18fc2c1db2b/not-able-to-dissociate-and-delete-public-ip-address-from-application-gateway?forum=WAVirtualMachinesVirtualNetwork

"You would have to use PowerShell for that. This support is not available in portal. You will have to delete Rules, Listener (Remove-AzureRmApplicationGatewayRequestRoutingRule, Remove-AzureRmApplicationGatewayHttpListener in this order), which are associated with the frontend IP configuration and finally issue Remove-AzureRmApplicationGatewayFrontendIPConfig followed by Set-AzureRmApplicationGateway to achieve this."

@vanpelt
Copy link

vanpelt commented Feb 18, 2022

The nuclear option is to just tell terraform to replace the resource that is attached to the public IP:

terraform apply -replace=azurerm_application_gateway.foo

lonegunmanb added a commit to lonegunmanb/terraform-azurerm-compute that referenced this issue Nov 25, 2022
jiaweitao001 added a commit that referenced this issue Nov 28, 2022
Solve #107 by adding `create_before_destroy = true` to defer public ip's deletion.
@jpbuecken
Copy link

jpbuecken commented Nov 28, 2022

Hello,
this solution has one downfall. If you need to re-create your resource it is not possible.
In Azure the Resource ID is related to its name. A create-before-destroy will create a resource with the same name and id, but the current one still exists. So this will fail.

EDIT: A new design with a attachment resource similar to disks would be a more robust solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests