Problem
When you attempt to import an Azure Subscription resource into Terraform state, the operation may fail with a cannot parse resource id error.
$ terraform import azurerm_subscription.test /providers/Microsoft.Subscriptions/<REDACTED> azurerm_subscription.test: Importing from ID "/providers/Microsoft.Subscriptions/<REDACTED>" Error: cannot parse resource id: "/providers/Microsoft.Subscriptions/<REDACTED>"
Cause
Due to Azure Subscription API limitations, Terraform requires an alias to correctly manage azurerm_subscription resources. This error occurs when you try to import a subscription that was created without an alias, which is common for subscriptions created through the Azure Portal.
Solutions
You must add an alias to the existing subscription before you can import it. You can achieve this using one of the following methods.
Solution 1: Add an Alias with a Terraform Configuration
You can add an alias to an existing subscription by defining an azurerm_subscription resource in your configuration and running terraform apply. This action associates the alias with the subscription without creating a new one.
-
Define the resource in your configuration, specifying the
subscription_idand a newalias.resource "azurerm_subscription" "test" { alias = "test-sub" subscription_name = "My Test Subscription" subscription_id = "<YOUR_SUBSCRIPTION_ID>" } -
Apply the configuration.
$ terraform apply
After the apply completes, Terraform will manage the subscription resource.
Solution 2: Add an Alias with the Azure API
Alternatively, you can use the Azure REST API to create an alias for the existing subscription.
- Follow the official Microsoft documentation to make an API request that creates the alias.
- After the alias is successfully created, retry the
terraform importcommand using the subscription alias.
Outcome
After applying one of the solutions, Terraform will be able to manage the Azure subscription resource, and future import, plan, and apply operations will succeed.