Rotating Secrets with Key Vault
This lesson explains how versions and references support rotation, event-driven rotation, the single- and dual-credential patterns, and automatic rotation of keys and certificates.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how version-less references follow rotation.
- Describe event-driven rotation with SecretNearExpiry.
- Compare single and dual credential rotation.
- Describe automatic rotation policies for keys.
Why and how secrets rotate
A leaked secret is useful to an attacker until it is changed. Rotation limits that window by replacing secrets regularly, for example every 60 or 90 days, and immediately after a suspected exposure. Rotation has two halves that must stay in step: the target service (the database, the third-party API) must accept a new credential, and Key Vault must hold it as a new version that applications pick up.
Version-less references make the second half automatic. Applications that read the current version, through the SDK with a cache refresh or through a version-less platform reference, receive the new value without a code or configuration change. App Service and Functions re-fetch version-less references periodically, within 24 hours, or immediately when the app restarts; Container Apps replicas read secret values at start-up, so a revision restart picks up a new value. References pinned to a version never rotate.
Will it pick up the new version?
Event-driven rotation
Key Vault publishes events to Event Grid for changes to its objects. For rotation, the most useful are Microsoft.KeyVault.SecretNearExpiry, published 30 days before a secret version's expiry date, SecretExpired, and SecretNewVersionCreated. An automated rotation subscribes a function to the near-expiry event.
-
Expiry approaches
Key Vault publishes SecretNearExpiry to Event Grid 30 days before the version expires.
-
Function runs
An Event Grid-triggered function receives the event with the vault and secret name.
-
New credential
The function creates or regenerates a credential in the target service.
-
New version
The function stores the credential as a new secret version with a new expiry date.
-
Applications follow
Version-less references and cache refreshes pick up the new version.
The rotation function's identity needs Key Vault Secrets Officer on the vault and permission to change credentials in the target service. Monitoring should alert if a near-expiry event is not followed by a new version, which indicates a failed rotation.
Automated secret rotation
Single and dual credentials
Rotating a credential that applications are actively using risks an outage: between changing the password in the database and the applications reading the new version, connections fail. Two patterns address this.
Azure Storage accounts, many Azure AI services and some external APIs provide two keys precisely for this. The rotation alternates: this cycle regenerates key2 while applications use key1, stores key2 as the new secret version, and the next cycle regenerates key1.
The following diagram shows the rotation sequence of a storage account key and how its two keys alternate.

Single or dual credential?
Automatic rotation for keys and certificates
Cryptographic keys in Key Vault support an automatic rotation policy, which creates a new key version on a schedule, such as 90 days after creation or a set time before expiry, and can send a near-expiry notification. Services that use customer-managed keys and reference the key without a version pick up the new version automatically.
{
"lifetimeActions": [
{ "trigger": { "timeAfterCreate": "P90D" }, "action": { "type": "Rotate" } },
{ "trigger": { "timeBeforeExpiry": "P30D" }, "action": { "type": "Notify" } }
],
"attributes": { "expiryTime": "P1Y" }
}Certificates have their own lifecycle policy: certificates from an integrated certificate authority can renew automatically at a percentage of their lifetime, and services such as App Service can import renewed certificates from Key Vault. Secrets have no built-in automatic rotation, because Key Vault cannot change a password in an external system; that is why secret rotation uses events and functions.
Automatic rotation
Rotating a storage key without downtime
A legacy component must authenticate to a storage account with an account key, which is stored in Key Vault and read through a version-less App Service Key Vault reference. Security requires rotation every 60 days with no failed requests during rotation.
The design uses the dual-credential pattern with storage's two keys. The secret has an expiry date of 60 days, so Key Vault publishes SecretNearExpiry; an Event Grid-triggered function regenerates the key that is not in use, stores it as a new secret version with a new expiry, and records which key is current in a tag. The App Service app picks up the new version through its version-less reference, and the old key remains valid until the next cycle, so no request fails. Where possible, the longer-term fix is to move the component to a managed identity and remove the key.
Regenerating the key currently in use would break requests until the app refreshed. A reference pinned to a version would never pick up the new key. A key rotation policy applies to Key Vault keys, not to secrets that hold external credentials.
How long before a secret version's expiry does Key Vault publish the SecretNearExpiry event?
Show answer
30 days before the expiry date. A rotation function subscribed through Event Grid can then create the new version.
Exam Tips
Remember for the exam
- Rotation updates the target service and stores a new secret version; version-less references follow it.
- SecretNearExpiry is published to Event Grid 30 days before a version expires.
- Dual credentials (two keys or users) let rotation happen with no authentication failures.
- Keys support automatic rotation policies; secrets need event-driven rotation functions.