I want to import an azurerm_role_assignment
(Azure) in Terraform dependent on my environment (dev, test, prod) by using an import block such as
import {
to = azurerm_role_assignment.test_role_assignment
id = "<scope>/.../<GUID>"
}
At the moment I use
az role assignment list --assignee ... --all
which returns a list of role assignments and now based on matching roleDefinitionName
I copy the related id
manually into the import block and it works. But it is different for the other environments. I want to avoid copying a lot of IDs manually.
So I was thinking within the scope I can use a variable "env" in order to import from the correct environment. But the changing GUID is required – for example:
import {
to = azurerm_role_assignment.test_role_assignment
id = "/subscriptions/.../resourcegroups/$(var.env)-rg/$(data.<something>.id)"
}
Is there a way to get the GUID in a data block or similar so that I can put the GUID in the id of the import block? I don’t want to manually import all role assignments for all environments manually. Thank you!
2
Answers
—Not an answer—
Wanted to say I’m currently facing this as well, and haven’t found a way to resolve it.
If there was a data block for
azurerm_role_assignment
, I think that would’ve helped to get the GUID. But it seems Azure doesn’t support thatFor this answer I’ll go with: You have a role definition name you can match with, and you require the GUID for the role assignment to be returned to you as a value so you can use it for your imports.
Probably in this case, if it must be az cli, I would try the
az rest
command.With this you can go over the subscriptions, and get the GUID for the role assignment. Read docs here.
Perhaps, if your logic is something like:
for each subscription:
Perhaps something like will help you out:
Granted this is written for Windows. But the az commands are there for your personal fine-tuning. Hope it helps.