Terraform init does not run with the same login context as other terraform commands

Hi

This is the pipeline snapshot I try to run:

task: AzureCLI@2                     
      displayName: 'Terraform init'                                 
      inputs:
        azureSubscription: $(SERVICE_CONNECTION)                        
        scriptType: 'bash'                                               
        scriptLocation: 'inlineScript'                           
        inlineScript: |                        
          terraform init \
             -backend-config="resource_group_name=$(REMOTE_STATE_RG)" \
             -backend-config="storage_account_name=$(REMOTE_STATE_SA)" \
             -backend-config="container_name=$(REMOTE_S

My issue

Instead initializing Terraform, I get this message:

> Initializing the backend... ╷ │ Error: Error building ARM Config: Authenticating using the Azure CLI is only supported as a User (not a Service Principal). │ │ To authenticate to Azure using a Service Principal, you can use the separate 'Authenticate using a Service Principal' │ auth method - instructions for which can be found here: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/service_principal_client_secret │ │ Alternatively you can authenticate using the Azure CLI by using a User Account. │ │

But if I try another terraform command everything is fine, for instance:

task: AzureCLI@2                                 
 displayName: 'Terraform apply'                         
 inputs:
   azureSubscription: $(SERVICE_CONNECTION)                        
   scriptType: 'bash'                                               
   scriptLocation: 'inlineScript'                                   
   inlineScript: 'terraform apply --auto-approve terraform.tfplan'

My questions

  • Why is there a difference between init another terraform command ?

  • What I should do for using an Azure Service Principal like other command ?

using:

terraform: 1.5.5
azurerm: 3.68.0