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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changing Subscription ID causes existing resources to be untracked and state to go missing #11662
Comments
@daviddob Thank you for openning this issue! What happens in your case is that when you run Perhaps, one thing we can/should do is to respect the subscription ID when doing operations on the existing resources, while need to put more considerations. |
I don't want to interfere here, but for this exact reason I created https://github.com/aristosvo/aztfmove. Without the need to recreate, resources are easily moved around. |
Hi @magodo - this is pretty much exactly what I figured was occurring. Just wanted to point out that it is a relatively common issue when working in a multi-subscription environment and the fact that terraform does not check when subscriptions are changing has the potential for unexpected behavior. It would be ideal if terraform could check that the subscription is changing and would trigger a destruction of the old resource and build of the new (with proper diff output) in line with the usual expected terraform behavior when changing a parameter requires recreation. |
I think I've stumbled across the same issue working with Virtual Network Peerings, so didn't want to raise it as a new issue. I have a configuration where, if a value is passed into variable "peered_vnet_resource_id" then it creates a VNET peering from the VNET it is deployment and also creates the matching peering in the remote VNET. This is with 2 separate resources like this:
So that I can deploy the remote vnet peer into the right Azure Subscription, I have an alias on the azurerm provider. I pull the subscription ID out of the peered_vnet_resource_id variable into a local and then use that to configure the "peered_sub" version of the azurerm provider.
The default value for peered_vnet_resource_id is "nopeer" so, if there's nothing passed into that variable, and it is left as the default of "nopeer" then peered_sub_id is set to the current subscription, just so that there's a valid subscription in it otherwise the provider block for "peered_sub" gets unhappy. This works fine and creates the peerings on both side. I run into issues if I subsequently remove the peering. Terraform destroys the resource azurerm_virtual_network_peering.peering_outbound but doesn't destroy the resource azurerm_virtual_network_peering.peering_inbound. It then loses track of the peering_inbound resource so you can an error if you later want to reestablish the peering, as the resource already exists. I think this is because the subscription ID has changed in local.peer_sub_id so the subscription that azurerm_virtual_network_peering.peering_inbound is associated with changed. But it didn't trigger Terraform to destroy and recreate the resource (in this case, I would expect a destroy but not a recreate). |
Community Note
Terraform (and AzureRM Provider) Version
Affected Resource(s)
All azurerm resources appear to be affected by this issue. Thusfar it has been confirmed with:
azurerm_storage_account
azurerm_mysql_server
azurerm_postgresql_server
azurerm_cosmosdb_account
Terraform Configuration Files
Debug Output
Contains sensitive info - reach out if further debug info is required to reproduce.
Panic Output
N/A
Expected Behaviour
When changing the subscription_id from
my-azure-subscription-id-1
tomy-azure-subscription-id-2
after deploying resources I would expect it to show a valid diff stating that the relevant existing resources need to be replaced where necessary and upon apply the existing resources are destroyed and new resources are created in the new subscription.Actual Behaviour
After changing the subscription_id and running plan or apply, terraform loses all reference to the existing azurerm resources and presents to the user that any relevant resources will be created (X to add, 0 to change, 0 to destroy). This makes it appear that it is a new clean deployment without existing state. Upon deploying with the new subscription_id, if there is an error (and there often is due to names that must be unique conflicting with the existing resources in the other subscription), then the terraform errors out and updates the tfstate to reflect the "current state" where there are no resources successfully created. This results in a situation where the existing resources from the previous deployment are no longer tracked in the tfstate and the "new resources" cant be deployed. Changing the subscription_id back to the original after the apply does not restore the state and you either need to recover a previous version, or re-import all of the missing resources.
Steps to Reproduce
terraform apply
using subscription_id Aterraform apply
using subscription_id B (Notice the diff is all new resources and has no reference to the previously deployed resources)Important Factoids
ARM_SUBSCRIPTION_ID
or thesubscription_id
parameter changing to a new subscription.References
The text was updated successfully, but these errors were encountered: