You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If data source of azurerm_key_vault is referenced in a different subscription, key vaults in the current subscription are not detected/refreshed and will attempt be recreated
#19509
Closed
1 task done
lukecalderon opened this issue
Nov 30, 2022
· 3 comments
· Fixed by #19531
## Resource Refresh
2022-11-30T22:35:36.8228391Z azurerm_key_vault.fetch: Refreshing state... [id=/subscriptions/target-resource-sub-id/resourceGroups/rg-name/providers/Microsoft.KeyVault/vaults/kv-comp-dev-dhf-uks-001]
## API Call being made (note subscription ID has changed)
2022-11-30T22:35:36.8269896Z 2022-11-30T22:35:36.826Z [DEBUG] provider.terraform-provider-azurerm_v3.33.0_x5: AzureRM Request:
2022-11-30T22:35:36.8270975Z GET /subscriptions/tfstate-sub-id/resourceGroups/rg-name/providers/Microsoft.KeyVault/vaults/kv-comp-dev-dhf-uks-001?api-version=2021-10-01 HTTP/1.1
## Actual API response
2022-11-30T22:35:36.8463135Z 2022-11-30T22:35:36.845Z [DEBUG] provider.terraform-provider-azurerm_v3.33.0_x5: AzureRM Response for https://management.azure.com/subscriptions/tfstate-sub-id/resourceGroups/rg-name/providers/Microsoft.KeyVault/vaults/kv-comp-dev-dhf-uks-001?api-version=2021-10-01:
2022-11-30T22:35:36.8465331Z HTTP/2.0 404 Not Found
2022-11-30T22:35:36.8466702Z Content-Length: 127
2022-11-30T22:35:36.8467034Z Cache-Control: no-cache
2022-11-30T22:35:36.8467639Z Content-Type: application/json; charset=utf-8
2022-11-30T22:35:36.8467979Z Date: Wed, 30 Nov 2022 22:35:36 GMT
2022-11-30T22:35:36.8469308Z Expires: -1
2022-11-30T22:35:36.8469576Z Pragma: no-cache
2022-11-30T22:35:36.8471021Z Strict-Transport-Security: max-age=31536000; includeSubDomains
2022-11-30T22:35:36.8472477Z X-Content-Type-Options: nosniff
2022-11-30T22:35:36.8474394Z X-Ms-Correlation-Request-Id: cd063cc1-e462-374a-61fe-868cfdbe8de7
2022-11-30T22:35:36.8475049Z X-Ms-Failure-Cause: gateway
2022-11-30T22:35:36.8475629Z X-Ms-Request-Id: b89189b4-61fe-408d-9a02-299ca7916c07
2022-11-30T22:35:36.8476316Z X-Ms-Routing-Request-Id: UKWEST:20221130T223536Z:b89189b4-61fe-408d-9a02-299ca7916c07
## Cannot find resource group (as it's not in the tfstate-sub, it's in the target-resource-sub)
2022-11-30T22:35:36.8500447Z {"error":{"code":"ResourceGroupNotFound","message":"Resource group 'rg-name' could not be found."}}: timestamp=2022-11-30T22:35:36.846Z
## Then TF removes it from the state file
2022-11-30T22:35:36.8540674Z 2022-11-30T22:35:36.848Z [DEBUG] provider.terraform-provider-azurerm_v3.33.0_x5: Vault: (Name "kv-comp-dev-dhf-uks-001" / Resource Group "rg-name") was not found - removing from state!: timestamp=2022-11-30T22:35:36.848Z
Expected Behaviour
On the terraform refresh, the request URI to Azure to get the resource details should retain the same subscription ID that is stored in the state file, and therefore return the resource.
Actual Behaviour
Terraform substitutes the resource's subscription ID for that of the TF State subscription ID. This returns a 404 as the resource group and resource do not exist in that resource group.
This only happens with key vaults - other resources in the same TF project (e.g. storage accounts, data factory) are all fine.
The only potential thing I can see here is that I have other azurerm_key_vaults referenced as data sources in a third subscription, using a provider alias.
Just had time to do further testing - it definitely seems that removing the data looking of another key vault in a different subscription has stopped this issue.
provider "azurerm" {
alias = azurerm.bootstrap
skip_provider_registration = false
features {
key_vault {
purge_soft_delete_on_destroy = true
}
}
subscription_id = "tfstate-sub-id"
}
data "azurerm_key_vault" "spn" {
provider = azurerm.bootstrap
name = var.spn_keyvault_name
resource_group_name = var.spn_keyvault_rg
}
data "azurerm_key_vault_secret" "spn_client_id" {
key_vault_id = data.azurerm_key_vault.spn.id
name = "terraform-spn-clientid"
}
lukecalderon
changed the title
Terraform attempts to recreate destroyed key vault, which hasn't been destroyed
If data source of azurerm_key_vault is referenced in a different subscription, key vaults in the current subscription are not detected/refreshed and will attempt be recreated
Dec 1, 2022
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Is there an existing issue for this?
Community Note
Terraform Version
1.3.5
AzureRM Provider Version
3.33.0
Affected Resource(s)/Data Source(s)
azurerm_key_vault
Terraform Configuration Files
Debug Output/Panic Output
Expected Behaviour
On the terraform refresh, the request URI to Azure to get the resource details should retain the same subscription ID that is stored in the state file, and therefore return the resource.
Actual Behaviour
Terraform substitutes the resource's subscription ID for that of the TF State subscription ID. This returns a 404 as the resource group and resource do not exist in that resource group.
This only happens with key vaults - other resources in the same TF project (e.g. storage accounts, data factory) are all fine.
The only potential thing I can see here is that I have other azurerm_key_vaults referenced as data sources in a third subscription, using a provider alias.
Steps to Reproduce
From an Azure pipeline, the following runs:
Important Factoids
No response
References
No response
The text was updated successfully, but these errors were encountered: