-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
azurerm_role_assignment "The role assignment already exists." #17421
Comments
@justin-chizer thanks for opening this issue here. Unfortunately, I could not reproduce this issue by running terraform apply twice with the following terraform configuration. Could you reproduce with it?
|
@sinbai Your example works, but I can reproduce using the following example, i.e. trying to assign the terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.11.0"
}
}
}
provider "azurerm" {
features {
}
}
data "azurerm_subscription" "this" {
}
data "azurerm_client_config" "test" {
}
resource "azurerm_role_assignment" "example" {
scope = data.azurerm_subscription.this.id
role_definition_name = "Contributor"
# https://portal.azure.com/#view/Microsoft_AAD_IAM/ManagedAppMenuBlade/~/Overview/objectId/788b2cad-5d5b-4046-8167-9cef6f46ac02/appId/ddbf3205-c6bd-46ae-8127-60eb93363864
principal_id = "788b2cad-5d5b-4046-8167-9cef6f46ac02"
} The error is:
|
Same Error.
|
I am having this same error today when trying to run the pipeline in the terraform apply step:
It fails on all these From
Is there any recommended solution to fix this? Edit: I also tried manually going into Azure Portal and removing the Service Principal role assignments from the resource group and then re-running the pipeline, but this did not work. |
Same problem assigning the Managed Identity Operator role to an AKS cluster resource "azurerm_role_assignment" "mi_operator" {
scope = azurerm_user_assigned_identity.iommi_user_api.id
role_definition_name = "Managed Identity Operator"
principal_id = azurerm_kubernetes_cluster.aks.identity[0].principal_id
} Results in
|
I had some time to investigate what's happening with that error: Before executing the API call to create resources, azurerm checks if the role assignment already exists. But it does so only based on Which makes a possible workaround of generating this name yourself and providing it to resource definition, thanks to which you solve the problem. Proper solution should be checking if role assignment with the same I will try to provide that kind of solution in upcoming weeks (depending on my free time unfortunatelly), so people waiting for the fix - hopefully soon it should be resolved. |
Unfortunately I am not able to reproduce the behaviour anymore - probably in the meantime some other changes fixed the issue. My test Terraform declaration:
409 error is still thrown if you create Role Assignment outside of Terraform and then run
In case I created role assignment via Terraform itself, it was properly added to tfstate and processed in further runs via terraform apply/plan/destroy as expected. @justin-chizer, could you verify if it works for you as well now? I used the current |
We are facing the same issue, any updates or fixes for this? |
Seeing these with managing ACL entries to storage account, container registry with hashicorp/azurerm 3.75.0 In terraform we have azurerm_role_assignment as named resources, but it seems they don't map to named resources on azure side to be able to do a terraform import. |
I am having this issue where the assignment is created but then the state file failed to upload due to some unrelated error. Trying to deploy a second time emits this error but it lacks the azure resource id of the role binding that already exists so there is no way for me to import it into the state file. The only option is to delete the existing role binding and try again but without knowing what the resource id is of the role binding it is absolutely excruciating to debug. |
I want to follow up on this because this is an incredibly painful issue that is still plauging us and we would really like to see this issue getting some attention. Specifically the simplest thing that could be done would be to simply modify the error message to contain the resource id. In this case the error message may look something like this:
But for all other resources when the resource already exists it looks like this:
You'll notice in the latter error message it contains both the Resource ID as well as the terraform module path. Using these two data points I can do any of the following:
But lacking the resource id it makes this nearly impossible to do any of the above. So please at the very least alter the error to contain the resource id of the already existing resource. |
Terraform has detailed logs that you can enable by setting the TF_LOG environment variable to any value. Enabling this setting causes detailed logs to appear on stderr. see https://developer.hashicorp.com/terraform/internals/debugging |
Does it print out the resource id in this case and can it be updated to print out the resource id by default in this case? Of course setting the TF_LOG variable has a lot of other side effects. |
Is there an existing issue for this?
Community Note
Terraform Version
1.2.3
AzureRM Provider Version
3.11
Affected Resource(s)/Data Source(s)
azurerm_role_assignment
Terraform Configuration Files
Debug Output/Panic Output
Expected Behaviour
Terraform should know that the Network Contributor role is already assigned to the AKS cluster that was created but it does not even know its there. It exists in the state file.
Actual Behaviour
terraform plan output says a new resource will be created.
terraform apply says resource already exists and throws a 409
Steps to Reproduce
Have a Network Contributor role assignment to a VNet and run terraform apply twice. Once to create the role and the next to throw the error.
Important Factoids
No response
References
There are similar issues but they all show Terraform trying to modify the resource not create a new one.
This is also a role to a VNet.
The text was updated successfully, but these errors were encountered: