AzureAzure RBACazurerm_role_assignmentPro

How to Import an Azure Role Assignment into Terraform

To import an existing Azure Role Assignment into Terraform, scan it with Terraback and run terraback azure import --method bulk. Terraback writes the matching azurerm_role_assignment resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the scope plus the assignment GUID (<scope>/providers/Microsoft.Authorization/roleAssignments/<guid>), for example /subscriptions/<sub>/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000.

Import Azure Role Assignment with Terraback (recommended)

Terraback reverse-engineers your live infrastructure: it reads the Azure RBAC resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live Azure Role Assignment to managed Terraform.

1

Scan your Azure account

terraback scan all azure --subscription-id YOUR_ID
2

Generate import blocks and import into state

terraback azure import --method bulk

The Terraform import block

Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.

import {
  to = azurerm_role_assignment.reader
  id = "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/11111111-2222-3333-4444-555555555555"
}

Example azurerm_role_assignment configuration

Here is a realistic Azure Role Assignment block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.

resource "azurerm_role_assignment" "reader" {
  scope                = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/prod-rg"
  role_definition_name = "Reader"
  principal_id         = "66666666-7777-8888-9999-aaaaaaaaaaaa"
}

Gotchas when importing a Azure Role Assignment

  • The import ID is the scope concatenated with /providers/Microsoft.Authorization/roleAssignments/<assignment-guid>; the GUID is the assignment's own name, not the role or principal ID.
  • For cross-tenant assignments, append the tenant: <resource-id>|<tenant-id>.
  • Use either role_definition_name or role_definition_id, not both; the human-readable name is easier but the ID is unambiguous.
  • principal_id is an object ID and immutable; changing the principal or scope forces a new assignment.

Doing it manually with terraform import

The native approach is to write the azurerm_role_assignment block by hand, then run terraform import azurerm_role_assignment.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.

Import your whole Azure account in minutes

Terraback scans 80+ Azure resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.