i am trying to deploy azure resources through terraform and configured below task to run terraform init.
- task: TerraformTaskV4@4
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
commandOptions: '-migrate-state'
backendAzureRmUseEnvironmentVariablesForAuthentication: true
backendAzureRmUseEntraIdForAuthentication: true
backendServiceArm: 'xxxxxxxxxx'
backendAzureRmResourceGroupName: 'rg-terrstore-dev'
backendAzureRmStorageAccountName: 'saterrstordev'
backendAzureRmContainerName: 'terraformstates'
backendAzureRmKey: 'terraform-dev.tfstate'
when i run the pipeline it throwing the following error.
*Error building ARM Config: obtain subscription() from Azure CLI: parsing json result from the Azure CLI: waiting for the Azure CLI: exit status 1: ERROR: Please run ‘az login’ to setup account
the subscription has owner role on subscription and should be able to authenticate subscription. could someone suggest me what i am missing here.
2
Answers
Assuming your Azure Resource manager service connection is created with the underlying service principal (app registration not a managed identity), as far as I have tested to use the
TerraformTaskV4@4
task to initialize terraform backend state in Azure storage account, we only need to make sure the underlying service principal (app registration) of the Azure Resource Manager service connection is assigned with the contributor role to the scope of the subscription.Besides, please remove the options to use
backendAzureRmUseEnvironmentVariablesForAuthentication
andbackendAzureRmUseEntraIdForAuthentication
, since the task will use the service principal information stored in the ARM service connection to authenticate access to Azure subscription and storage account.You may follow the marks in the screenshots below to double check if the underlying service principal (app registration) is granted with sufficient permissions, redirecting from the ARM service connection in your Project Settings (
ARMSvcCnnWIFTerraform
in my case) that is referenced by this task. If your ARM service connection was created with service principal client id and secret rather than wiht workload identity federation authentication, you may also try to Edit and Verify the service connection.If you happen to know the
ARM_CLIENT_ID
andARM_CLIENT_SECRET
etc. information of the service principle, you may consider using a simple script forterraform init
as a possible workaround or as a method to double check principal permissions in your local environment or via pipelines. Here are the samples for your reference.I have the same issue, but am not using Storage account keys, it hangs on:
[DEBUG] Obtaining an MSAL / Microsoft Graph token for Resource Manager..
[DEBUG] Obtaining an MSAL / Microsoft Graph token for Storage..