Goal: Importing 2 Azure subscriptions not created with IaC/Terraform
Issue: When importing, the error
Error: failed reading Subscription Alias: subscriptions.SubscriptionsClient#AliasGet: Failure responding to request: StatusCode=401 – Original Error: autorest/azure: Service returned an error. Status=401 Code=“UserNotAuthorized” Message=“User does not have access Microsoft.Subscription/aliases/write over scope providers/Microsoft.Subscription/aliases/sub1”
Error: failed reading Subscription Alias: subscriptions.SubscriptionsClient#AliasGet: Failure responding to request: StatusCode=401 – Original Error: autorest/azure: Service returned an error. Status=401 Code=“UserNotAuthorized” Message=“User does not have access Microsoft.Subscription/aliases/write over scope providers/Microsoft.Subscription/aliases/sub2”
Actions:
terraform import -var-file=“app.tfvars” module.azure_subscription.azurerm_subscription.subscription_app “/providers/Microsoft.Subscription/aliases/sub1”
terraform import -var-file=“app.tfvars” module.azure_subscription.azurerm_subscription.subscription_data “/providers/Microsoft.Subscription/aliases/sub2”
Code:
module “azure_subscription” {
providers = {
azurerm = azurerm.subshared
}
source = “…/…/modules/azure_subscription/”
application = var.application
environment = var.environment#
region_short = var.region_short
tags = local.tags
workload = “DevTest” #DevTest or Production
}
#Create Azure Subscriptions
#Terraform Registry
#Providers
terraform {
required_providers {
azurerm = {
source = “hashicorp/azurerm”
version = “~> 3.108.0”
}
}
required_version = “>= 1.1.0”
}
#Data
data “azurerm_billing_mca_account_scope” “billing_scope” {
billing_account_name = “xxx”
billing_profile_name = “xxx”
invoice_section_name = “xxx”
}
#Variables
variable “application” {
description = “application and/or Product name from tfvars”
}
variable “environment” {
description = “environment, Ex: sbx, from tfvars”
}
variable “region_short” {
description = “region short name, eus, from tfvars”
}
variable “tags” {
description = “tags from TF local”
}
variable “workload” {
description = “DevTest or Production Azure Subscription Offer Type”
}
#Resources
resource “azurerm_subscription” “subscription_app” {
alias = “prefix-{var.region_short}-app-{var.application}-{var.environment}"
subscription_name = "***prefix***-{var.region_short}-app-{var.application}-{var.environment}”
billing_scope_id = data.azurerm_billing_mca_account_scope.billing_scope.id
workload = var.workload
tags = var.tags
}
resource “azurerm_subscription” “subscription_data” {
alias = “prefix-{var.region_short}-data-{var.application}-{var.environment}"
subscription_name = "***prefix***-{var.region_short}-data-{var.application}-{var.environment}”
billing_scope_id = data.azurerm_billing_mca_account_scope.billing_scope.id
workload = var.workload
tags = var.tags
}
Outputs
##App
output “azurerm_subscription_app_id” {
value = azurerm_subscription.subscription_app.id
}
output “azurerm_subscription_app_subscription_name” {
value = azurerm_subscription.subscription_app.subscription_name
}
output “azurerm_subscription_app_billing_scope_id” {
value = azurerm_subscription.subscription_app.billing_scope_id
}
Data
output “azurerm_subscription_data_id” {
value = azurerm_subscription.subscription_data.id
}
output “azurerm_subscription_data_subscription_name” {
value = azurerm_subscription.subscription_data.subscription_name
}
output “azurerm_subscription_data_billing_scope_id” {
value = azurerm_subscription.subscription_data.billing_scope_id
}
Attempted:
Followed, Terraform Registry, adding alias
#Resources
resource “azurerm_subscription” “sub1” {
alias = “sub1”
subscription_name = “sub1”
subscription_id = “xxx”
}
resource “azurerm_subscription” “sub2” {
alias = "sub2
subscription_name = “sub2”
subscription_id = “xxx”
}
Alias is added per, az account alias list
{
"id": "/providers/Microsoft.Subscription/aliases/sub1",
"name": "sub1",
"properties": {
"acceptOwnershipState": null,
"acceptOwnershipUrl": null,
"billingScope": null,
"createdTime": null,
"displayName": null,
"managementGroupId": null,
"provisioningState": "Succeeded",
"resellerId": null,
"subscriptionId": "xxx",
"subscriptionOwnerId": null,
"tags": null,
"workload": null
},
"systemData": null,
"type": "Microsoft.Subscription/aliases"
},
{
"id": "/providers/Microsoft.Subscription/aliases/sub2",
"name": "sub2",
"properties": {
"acceptOwnershipState": null,
"acceptOwnershipUrl": null,
"billingScope": null,
"createdTime": null,
"displayName": null,
"managementGroupId": null,
"provisioningState": "Succeeded",
"resellerId": null,
"subscriptionId": "xxx",
"subscriptionOwnerId": null,
"tags": null,
"workload": null
},
"systemData": null,
"type": "Microsoft.Subscription/aliases"
},
Tried adding role assignment per,
az role assignment create --assignee 12345 --role owner --scope “providers/Microsoft.Subscription/aliases/sub1”
az role assignment create --assignee 12345 --role owner --scope “providers/Microsoft.Subscription/aliases/sub2”
Error;
The command failed with an unexpected error. Here is the traceback:
list index out of range
Traceback (most recent call last):
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/role/custom.py”, line 180, in create_role_assignment
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/role/custom.py”, line 204, in _create_role_assignment
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/role/_multi_api_adaptor.py”, line 57, in create_role_assignment
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/core/tracing/decorator.py”, line 76, in wrapper_use_tracer
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/mgmt/authorization/v2022_04_01/operations/_role_assignments_operations.py”, line 902, in create
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/core/exceptions.py”, line 112, in map_error
azure.core.exceptions.ResourceExistsError: (RoleAssignmentExists) The role assignment already exists.
Code: RoleAssignmentExists
Message: The role assignment already exists.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\knack/cli.py”, line 233, in invoke
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py”, line 664, in execute
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py”, line 731, in _run_jobs_serially
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py”, line 701, in _run_job
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/init.py”, line 334, in call
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/core/commands/command_operation.py”, line 121, in handler
File “D:\a_work\1\s\build_scripts\windows\artifacts\cli\Lib\site-packages\azure/cli/command_modules/role/custom.py”, line 186, in create_role_assignment
IndexError: list index out of range
Azure RBAC;
I was attempting to run as an app registration, tried my global administrator account, both produce the same import error and error with RBAC permissions on “/”. Both are owner on either subscription, however it’s assigned at Management Group.
Links Reviewed;
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subscription
Comments;
This is an issue for us as we are implementing terraform as an enterprise and have many existing subscriptions.