4

I am getting this error when trying to run my Azure Function (Which is in PowerShell). PowerShell script works normally when ran locally.

Error:

[Error] ERROR: No subscription found in the context.  Please ensure that the credentials you provided are authorized to access an Azure subscription, then run Connect-AzAccount to login

In requirements.ps1:

@{
    'Az.Accounts' = '1.9.0'
    'Az.Resources' = '1.1.2'
    'AzTable' = '2.0.3'
    'Az.Storage' = '1.7.0'
    'Az.KeyVault' = '1.5.1'
}

In the run.ps1, I added the Import-Module commands:

Import-Module Az.Resources
Import-Module Az.Accounts
Import-Module Az.KeyVault
Import-Module AzTable

First few lines in 'run.ps1':

$activeKey = Get-AzKeyVaultManagedStorageAccount -VaultName $keyVaultName -Name $storageAccountName

# Get Storage Account Key 
$key = (Get-AzStorageAccountKey -ResourceGroupName $rgName -Name $storageAccountName)

In profile.ps1:

if ($env:MSI_SECRET) {
    Disable-AzContextAutosave
    Connect-AzAccount -Identity
}

Thanks!

1 Answer 1

4

Please make sure you:

  1. Enable a managed identity. Using a system-assigned identity is easier, using a user-assigned identity requires one more step.
  2. Assign appropriate roles on the target subscription to the identity.
  3. In your run.ps1 code, invoke Set-AzContext to select the target subscription.

Importing modules explicitly in run.ps1 is usually not necessary.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks. Really close now. Currently getting a different error "[Error] ERROR: Operation returned an invalid status code 'Forbidden'Exception :Type : Microsoft.Azure.KeyVault.Models.KeyVaultErrorExceptionRequest". I tried to give Key Vault rights to the Function App, Access Policies to the Key Vault but still to no avail. Any ieda?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.