Hello Jane Illarionova,
Thank you for posting your question in the Microsoft Q&A forum.
This error indicates a failure in the managed identity authentication flow, specifically a timeout when the Get-AzAccessToken cmdlet attempts to retrieve a token for the Cosmos DB endpoint. The issue is not directly related to your RBAC permissions (Owner, Contributor, etc.), as those are correct for data plane access once you have a token. The problem lies in the network connectivity or configuration between your Azure Automation runbook and the managed identity endpoint.
The primary cause is likely a network-related timeout when your runbook attempts to contact the local metadata service (169.254.169.254) to retrieve the managed identity token. This often occurs in Azure Automation if the runbook is running in a sandbox that has restricted outbound connectivity or if there are explicit network restrictions on the Cosmos DB account.
To resolve this, you may consider the following steps:
- Use Cosmos DB Cmdlets Directly: Instead of manually retrieving a token, use the Az.CosmosDB PowerShell cmdlets (e.g., Get-AzCosmosDBAccount, New-AzCosmosDBAccount) which automatically handle authentication using the managed identity context you established with Connect-AzAccount -Identity. This is the most robust and recommended approach.
- Check Network Restrictions: If your Cosmos DB account has firewall settings enabled, you must allow access from Azure services. Ensure the "Allow access from Azure portal" and "Allow access from my IP" settings are configured appropriately or add the outbound IPs of the Azure Automation service (which can be found in the Automation account's properties under "Outgoing IP Addresses").
- Use Resource-Specific Endpoint: For Get-AzAccessToken, ensure the -ResourceUrl is the correct Cosmos DB resource endpoint. For Cosmos DB, the resource URL should be https://cosmos.azure.com (the global data plane endpoint) or the specific endpoint for your account if using a national cloud. Using the wrong resource URL will cause authentication failures.
- Leverage Azure Private Link: If your Automation account and Cosmos DB are in a virtual network with strict NSG rules, ensure that the managed identity endpoint is reachable. You may need to use Private Link for Cosmos DB and configure proper DNS resolution to avoid public network timeouts.
By addressing these connectivity and configuration issues, you should be able to resolve the timeout and successfully authenticate using the system-assigned managed identity.
Please, let me know the response helps answer your question? If the above answer helped, please do not forget to "Accept Answer" as this may help other community members to refer the info if facing a similar issue. 🙂