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

terraform destroy fails for azurerm_role_definition if role scope is a Management Group #5125

Closed
cmendible opened this issue Dec 10, 2019 · 3 comments · Fixed by #6107
Closed

Comments

@cmendible
Copy link
Contributor

When using azurerm_role_definition to create a role scoped to a Management Group instead to a Subscription or Resource Group, terraform destroy fails cause the scope sent to the API is empty.

This seems to be casued by the parseRoleDefinitionId function defined here, which asumes that every roleDefinitionId starts with the scope which is not the case for Roles scoped to a Management Group.

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform (and AzureRM Provider) Version

Terraform v0.12.17

  • provider.azurerm v1.38.0

Affected Resource(s)

  • azurerm_role_definition

Terraform Configuration Files

terraform {
  required_providers {
    azurerm = ">= 1.38.0"
  }
}

variable "mg_id" {
  type        = string
  description = "Existing Management Group ID"
}

locals {
  scope = "/providers/Microsoft.Management/managementGroups/${var.mg_id}"
}

resource "azurerm_role_definition" "RoleInManagementGroup" {
  name        = "[REMOVE] Role In Management Group"
  scope       = local.scope
  description = "This is a custom role created via Terraform"

  permissions {
    actions     = ["*"]
    not_actions = []
  }

  assignable_scopes = [
    local.scope
  ]
}

Debug Output

https://gist.github.com/cmendible/e44f10b665ba9c97cb9a9d1167c5bd99#file-debug-log

Panic Output

Expected Behavior

Role must be deleted by terraform destroy

Actual Behavior

terraform destroy results in:

Error deleting Role Definition "00000000-0000-0000-0000-000000000000" at Scope "": authorization.RoleDefinitionsClient#Delete: Failure responding to request: StatusCode=403 -- Original Error: autorest/azure: Service returned an error. Status=403 Code="AuthorizationFailed" Message="The client 'live.com#[email protected]' with object id '00000000-0000-0000-0000-000000000000' does not have authorization to perform action 'Microsoft.Authorization/roleDefinitions/delete' over scope '/providers/Microsoft.Authorization/roleDefinitions/fc4a1caf-aee2-b29b-14e4-11de9aa09cd6' or the scope is invalid. If access was recently granted, please refresh your credentials."

Issue seems to be related to the fact that Terraform is sending en empty scope.

Steps to Reproduce

You'll need to have an existing management group. The config file will ask for the ID of the group.

  1. terraform apply
  2. terraform destroy

Important Factoids

References

cmendible added a commit to cmendible/terraform-provider-azurerm that referenced this issue Feb 10, 2020
@cmendible
Copy link
Contributor Author

cmendible commented Feb 10, 2020

Hi @katbyte, @tombuildsstuff I fixed this in e4a0e56 on a branch based on tag v1.38.0. Since the master branch has changed so much should I make a PR based on that tag?

@ghost
Copy link

ghost commented Sep 10, 2020

This has been released in version 2.27.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example:

provider "azurerm" {
    version = "~> 2.27.0"
}
# ... other configuration ...

@ghost
Copy link

ghost commented Oct 9, 2020

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Oct 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants