An Azure service that provides an event-driven serverless compute platform.
Hi Guy Levi,
I tried to run your code in Azure function App and got 500 Internal Server error as below:
[ "Error retrieving token: An attempt was made to access a socket in a way forbidden by its access permissions. (169.254.169.254:443)" ]
In PowerShell Azure Functions, calling http://169.254.169.254 for managed identity can fail if the function App attempts to route the call via HTTPS instead of HTTP or applies a proxy, even on public network access.
An attempt was made to access a socket in a way forbidden by its access permissions. (169.254.169.254:443)
This error indicates something is redirecting the request to port 443 (HTTPS), even though the Invoke request is using HTTP.
Hence as a workaround, you can use below PowerShell code with Az modules to generate access token in Function App using Managed Identity.
run.ps1:
param($Request, $TriggerMetadata)
Connect-AzAccount -Identity | Out-Null
$scope = "api://b2b532c5XXb537-d507914e592a" //APP ID or Client ID of App registration
try {
$tokenResponse = Get-AzAccessToken -ResourceUrl $scope
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = 200
Body = @{
access_token = $tokenResponse.Token
} | ConvertTo-Json -Depth 3
})
}
catch {
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = 500
Body = @("Error retrieving token: $($_.Exception.Message)")
})
}
requirements.ps1:
@{
'Az.Accounts' = '2.12.1'
}
profile.ps1:
Import-Module Az.Accounts
- `I have assigned below API permission to the Function App's managed identity:
- Able to generate the access token as below:
Hope it helps!
Please do not forget to click "Accept the answer” and Yes wherever the information provided helps you, this can be beneficial to other community members.
If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.