Token expiry best practice in always on azure function

Anonymous
2024-12-04T22:58:05.04+00:00

Hi,

I have an Azure http trigger function in an elastic premium function. This function connects to a mysql flexible server using a managed identity through a DefaultAzureCredential token.

These tokens last 60 to 90mn but my function may stay up for longer than that.

What is the best design practice to renew tokens:

  • wait for an exception and renew the connection pool
  • check at regular intervals, say every 50mn?
  • check at every connection request?

Thank you

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,935 questions
Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

Accepted answer
  1. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2024-12-05T11:03:54.5033333+00:00

    Hi @Jean David Ruvini ,

    Thanks for reaching out.

    The general practice is to refresh the token before it expires.

    However, managed identity tokens are cached by the underlying Azure infrastructure for performance and resiliency purposes: the back-end services for managed identities maintain a cache per resource URI for around 24 hours.

    When you are saying 60 to 90 min, could you please confirm this token has been generated using managed identity?

    Tokens acquired via the App Authentication library currently are refreshed when less than 5 minutes remains until they expire. So, it caches the token for 23 hours 55 minutes in the default case.

    The cache lifetime of the token issued by a Managed Identity endpoint is controlled by the service provider, and unfortunately it appears that there is no way to force the refresh.

    For more information, see Limitation of using managed identities for authorization.

    Hope this will help.

    Thanks,

    Shweta

    Please "Accept the answer" if above answer helped you.


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2024-12-04T22:58:05.07+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To manage token expiry effectively in your Azure HTTP trigger function, the best practice would be to check at every connection request. This approach allows you to ensure that you always have a valid token before making a connection to your MySQL flexible server. By checking the token's validity at the time of the request, you can avoid potential exceptions due to expired tokens and maintain a seamless connection.

    Waiting for an exception to renew the connection pool can lead to downtime and degraded performance, while checking at regular intervals may not be efficient, especially if your function is under varying loads.


    References:

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.