Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 12 of 47
❮Prev Next❯

Manage and Rotate Storage Access Keys

Module 2: Storage ⏱ 9 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    Every storage account has two 512-bit access keys, key1 and key2. Either key gives full access to all data, so treat them like a root password. Two keys exist so you can rotate without downtime: move apps to key2, regenerate key1, then later do the reverse. A key expiration policy (rotation reminder) flags keys older than a set number of days, but doesn't rotate them. Regenerating a key also breaks every SAS signed with it. The safest end state is Microsoft Entra ID with Allow storage account key access set to Disabled.

    Simulate it » No Azure account needed

    Exam objective: 2.1.4 Manage access keys. (Skills measured as of April 17, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Rotate storage access keys and set a rotation reminder Set a rotation reminder, rotate key1 and turn off key access.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    Under <strong>Security + networking</strong> open <strong>Access keys</strong>. You need a role with the listKeys action (Owner, Contributor or Storage Account Key Operator Service Role). Reader can’t see them.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. Under Security + networking open Access keys. You need a role with the listKeys action (Owner, Contributor or Storage Account Key Operator Service Role). Reader can’t see them.
    2. Two keys, both 412 days old. Either one gives full control of every bit of data. Start with Set rotation reminder.
    3. Turn on reminders every 90 days. This sets the key expiration policy: Azure flags old keys, it doesn’t rotate them for you.
    4. Safe order: move apps to key2 first, then Rotate key on key1.
    5. key1 is new. Every connection string and SAS signed with the old key1 now fails. Next time, move apps back to key1 and rotate key2.
    6. The end goal: once every app uses Microsoft Entra ID, set Allow storage account key access to Disabled under Configuration. Then a leaked key is useless.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    The same storage key has sat in five apps for two years.

    😊 THE SOLUTION

    Rotate with two keys, get reminders, then move to Entra ID.

    • Keys never change✓ Key expiration policy
    • Rotation breaks apps✓ Swap to key2 first
    • Keys leak✓ Disable key access
    🛠️ Solved with: Access keys

    Start with a question

    How do you change the locks on a shop without closing it for a day?

    A shop with one front door has a problem: changing the lock means the door is out of use for a while. A shop with two doors doesn’t. You send everyone through the side door, change the front lock, hand out the new key, and next month you do it the other way round.

    A storage account has two access keys for the same reason. Your apps use one while you regenerate the other.

    Explain it like I’m 10

    The technical explanation

    Each account gets two 512-bit keys when it’s created. A request signed with either key (Shared Key authorization) can read, write and delete anything in the account. Connection strings contain a key. Account SAS and service SAS tokens are signed with a key too, so they live and die with it.

    Who can see the keys

    Listing keys uses the Microsoft.Storage/storageAccounts/listkeys/action permission. Owner, Contributor, Storage Account Contributor and Storage Account Key Operator Service Role have it. Reader doesn’t. Anyone who can list keys can read all the data, so treat that permission as a data permission.

    Rotate without downtime

    1. Update every app to use key2 (ideally by changing one secret in Azure Key Vault).
    2. Regenerate key1: portal Access keys > Rotate key on key1, or az storage account keys renew --key primary.
    3. Next rotation, move apps back to key1 and regenerate key2.

    If you regenerate the key an app is still using, that app gets 403 errors until you fix it, and every SAS signed with that key is dead.

    Rotation reminders

    Set rotation reminder on the Access keys blade creates a key expiration policy: keys older than, say, 90 days are flagged in the portal, and Azure Policy can report non-compliant accounts. It’s a reminder, not automation.

    Turn keys off

    Once apps use Microsoft Entra ID, go to Settings > Configuration and set Allow storage account key access to Disabled. Then any request signed with a key, and any account or service SAS, is refused. User delegation SAS and Entra ID keep working. Check first: old tools that only know keys will break.

    az storage account keys list -g rg-storage-prod -n stcontosoprod -o table
    az storage account keys renew -g rg-storage-prod -n stcontosoprod --key primary
    az storage account update -g rg-storage-prod -n stcontosoprod --key-exp-days 90
    az storage account update -g rg-storage-prod -n stcontosoprod --allow-shared-key-access false
    Get-AzStorageAccountKey -ResourceGroupName rg-storage-prod -Name stcontosoprod
    New-AzStorageAccountKey -ResourceGroupName rg-storage-prod -Name stcontosoprod -KeyName key1
    Set-AzStorageAccount -ResourceGroupName rg-storage-prod -Name stcontosoprod -AllowSharedKeyAccess $false
    Architect level Keys in Key Vault, and rotating automatically

    Put the connection string in Key Vault and have apps read it at start-up through a managed identity. Rotation then means updating one secret. To automate it, a common pattern is an Azure Function triggered on a schedule (or by Key Vault’s near-expiry event) that regenerates the inactive key, writes it to Key Vault as a new secret version, then flips which key is active. Even better is dropping keys for Microsoft Entra ID altogether.

    Real-world business scenario

    Logistics

    Northwind Logistics (fictional)

    The problem
    The same storage key had sat in five apps and two scripts for over a year, and nobody dared rotate it.
    What they used
    The team moved the connection string into Azure Key Vault, switched every app to key2, regenerated key1, set a 90-day rotation reminder, and planned the move to managed identities.
    Why that choice
    Rotating through the second key meant zero downtime, and the reminder makes old keys show up before an audit does.
    The result
    Rotation now takes ten minutes, and the three apps already on managed identities don't need keys at all.

    When to use it and when not to

    ✓ Use it when…

    • A tool or legacy app only supports Shared Key, and you keep the key in Key Vault.
    • Admins mount an Azure Files share with the key to set Windows ACLs.
    • You need to kill every key-signed SAS at once by regenerating the key.

    ✕ Think twice when…

    • Your own apps can use managed identities and RBAC data roles.
    • You plan to hand the key to partners (give a SAS instead).
    • Compliance requires no shared secrets (disable key access).

    ↔ Alternatives to consider

    • Microsoft Entra ID with Storage Blob Data roles and managed identities.
    • User delegation SAS for delegated, time-limited access with no key.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AZ-104 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    Applications connect to a storage account by using key1. You must regenerate key1 without causing an outage. What should you do first?

    Question 2

    Question 2 of 5

    You configure a key expiration policy of 90 days on a storage account. Which statement is true?

    Question 3

    Question 3 of 5

    A user is assigned the Reader role on a storage account and reports that the access keys are not visible. You must allow the user to view and regenerate the keys without granting other management permissions. Which role should you assign?

    Question 4

    Question 4 of 5 · Select 2

    You regenerate key1 of a storage account. Which two items stop working? Each correct answer presents part of the solution.

    Question 5

    Question 5 of 5

    All applications that use a storage account now authenticate by using managed identities. You must prevent any future use of the account keys and of SAS tokens signed with them. What should you configure?

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner Why does a storage account have two keys?

    Model answer

    So you can rotate them without downtime. Apps use one key while you regenerate the other. Then you switch the apps over and regenerate the first. If there were only one key, every rotation would be an outage.

    Intermediate A support engineer with the Reader role can't see the access keys. Is that a bug?

    Model answer

    No. Listing keys needs the listKeys action, which Reader doesn't have. Owner, Contributor, Storage Account Contributor and Storage Account Key Operator Service Role have it. Reader and Data Access is another option if they only need to read data through the portal.

    Experienced developer What does a key expiration policy actually do?

    Model answer

    It records how many days a key may live. The portal shows a rotation reminder and Azure Policy or Azure Monitor can flag accounts whose keys are older than that. It doesn't rotate anything for you. For automatic rotation you'd script it, for example with an Azure Automation runbook or an Event Grid triggered function, and keep the current value in Key Vault.

    Scenario A key was pasted into a public chat. Walk me through the next ten minutes.

    Model answer

    Find out which key it was. If the apps use the other key, regenerate the leaked one straight away. If the apps use the leaked key, swap them to the other key in Key Vault or their settings first, then regenerate. Check that SAS tokens signed with that key are reissued. Then review the storage logs for unusual reads and plan the move to Microsoft Entra ID so this can't happen again.

    Quick revision

    Key points

    • Portal path: storage account > Security + networking > Access keys.
    • Two keys, each with full access. Rotate one at a time.
    • Regenerating a key invalidates every connection string and SAS signed with it.
    • Set rotation reminder = key expiration policy (CLI: --key-exp-days).
    • Store keys in Azure Key Vault, never in code.
    • Disabling Shared Key access stops keys, account SAS and service SAS. Entra ID and user delegation SAS keep working.

    Important terminology

    Storage account access key
    One of two 512-bit keys that authorize any request to the account with Shared Key, giving full access to all data.
    Shared Key authorization
    Signing each request with an account key. Account SAS and service SAS tokens are also signed this way.
    Key rotation
    Regenerating an access key so the old value stops working.
    Key expiration policy
    An account setting (keyExpirationPeriodInDays) that marks keys as due for rotation after a number of days, shown as a rotation reminder.
    Allow storage account key access
    An account setting that, when disabled, refuses every request signed with an account key, including account and service SAS.
    Storage Account Key Operator Service Role
    A built-in role that lets a principal list and regenerate keys without other management rights.

    Common mistakes

    • Regenerating the key that apps are still using.
    • Expecting the key expiration policy to rotate keys automatically.
    • Giving Reader and expecting the person to see keys.

    Exam tips

    • "Rotate without downtime" = switch apps to the other key, then regenerate.
    • "Prevent any use of account keys" = set Allow storage account key access to Disabled.
    • "Let a script rotate keys but nothing else" = Storage Account Key Operator Service Role.

    Five quick revision questions

    1. 1.How many access keys does a storage account have?Show

      Two (key1 and key2).

    2. 2.Which CLI command regenerates key1?Show

      az storage account keys renew --key primary

    3. 3.What happens to a SAS signed with key1 after key1 is regenerated?Show

      It stops working.

    4. 4.Does the key expiration policy rotate keys?Show

      No, it only flags keys as due for rotation.

    5. 5.Which authorization still works when Shared Key access is disabled?Show

      Microsoft Entra ID (and user delegation SAS).

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Identity-Based Access for Azure Files

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.