Open sameer-mandaokar opened 6 months ago
Thanks for raising this issue. Seems the dependency-handling should be based on the index.
@neil-yechenwei
Thanks for your response. I tried adding the dependency azurerm_role_assignment.iam_role
resource as below -
resource "azurerm_role_assignment" "iam_role" {
count = local.instance_count
scope = azurerm_resource_group.rg.id
role_definition_name = "Contributor"
principal_id = azurerm_linux_virtual_machine.vm[count.index].identity[0].principal_id
depends_on = [ azurerm_linux_virtual_machine.vm ]
}
and I tried recreating the entire infrastructure after adding the dependency. However, it is marking all the iam_role resources for deletion, which is still the issue.
Is there an existing issue for this?
Community Note
Issue with "terraform plan -destroy -target=<target_id_count[1]>" for count-based resources
Description
We're creating multiple resources using COUNT meta-arg, which reads configuration from JSON array and iterates over JSON objects, and keep on creating resources. We have three VMs (VM[0-2] with their RBAC Role (Contributor) on the resource group.
When we want to delete the VM[1] using command, it marks the RBAC assignment for VM[0] & VM[2] as well, which is not as per expectation
Terraform Version
2.99
AzureRM Provider Version
2.8
Affected Resource(s)/Data Source(s)
azurerm_role_assignment
Terraform Configuration Files
Debug Output/Panic Output
Expected Behaviour
It should have marked only below two resources for deletion
azurerm_role_assignment.iam_role[1] will be destroyed
azurerm_linux_virtual_machine.vm[1] will be destroyed
Actual Behaviour
It is marking below four resources for deletion
azurerm_linux_virtual_machine.vm[1] will be destroyed
azurerm_role_assignment.iam_role[0] will be destroyed
azurerm_role_assignment.iam_role[1] will be destroyed
azurerm_role_assignment.iam_role[2] will be destroyed
Steps to Reproduce
Copy above terraform configuration into issue.tf file then hit below commands
Create one folder name 'count_issue'
Copy/Download JSON content from this file in count_issue folder env_config.json
Copy/download TF.txt configuration in count_issue folder count_issue.tf.txt
Rename above file from count_issue.tf.txt to count_issue.txt
Update tf file section from line 16 to 19.
Make sure Service principal should be able to provide RBAC assignments to VM.
Execute
terraform init
Execute
terraform plan
and review plan for VM/RBAC addition for 3 vmsExecute
terraform apply --auto-approve
Verify all the resources are created as per JSON content
Hit below command for generating destroy plan
terraform plan -destroy -target=azurerm_linux_virtual_machine.vm[1]
You should be able to see the Actual output, which is not as per Expected output. PSA sample output from my console
Important Factoids
No response
References
No response