I'm using Azure DevOps Pipeline to deploy an infrastructure. One of Terraform modules use local-exec and it is a Powershell script which enables VM SQL Backup. Everything works fine locally from my PC when I'm connected to Azure as a user. The problem appears only in a pipeline where I connect to Azure via Service Principal. I use a built-in Powershell just to run Connect-AzAccount
hoping that connection will be kept during all tasks. Unfortunately when it comes to Terraform Apply, during my module deployment, there's an error:
Please provide a valid tenant or a valid subscription.
Resource group 'abcd' could not be found.
Does somebody know how to keep connection with Azure during all tasks? I even tried to run Set-AzContext -SubscriptionId "xxxx-xxxx-xxxx-xxxx"
in a module but it doesn't find my subscription.
This is how Powershell task looks like:
$AzureAADClientKey = "***"
$AzureTenantID = " xxxx-xxxx-xxxx-xxxx "
$AzureSubscriptionName = " xxxx-xxxx-xxxx-xxxx "
$AzureEnv = "AzureCloud"
#Start Connection
$securePassword = ConvertTo-SecureString $AzureAADClientKey -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AzureAADClientID,$securePassword
$n = Disable-AzContextAutosave -Scope Process
Clear-AzContext -Scope Process
Connect-AzAccount -Credential $credential -TenantId $AzureTenantID -Environment $AzureEnv -ServicePrincipal -SubscriptionId $AzureSubscriptionName
Any help appreciated!
AzurePowerShell@5
task, you don't need to worry about setting the azcontext, it does this automatically for you. It does require a service connection to the azure environment though, but it seems you already have a principal, so adding a service connection should not be a problem. – PaulVrugt