Create a key vault and store a secret
In this task you'll create a key vault and add a secret.
Azure Key Vault keeps secrets (passwords, connection strings), keys and certificates in one safe place, so they never have to sit in code or settings files. Apps ask the vault for a secret when they need it.
New vaults use Azure role-based access control. Creating the vault doesn't give you access to its contents: you add yourself a data role, such as Key Vault Secrets Officer, before you can add secrets.
Example. Ironbridge Payments keeps the connection string for its payments database in a key vault. Developers never see it, and when the password is changed, only the secret in the vault is updated.
Before you start
You need: Contributor to create the vault; Key Vault Secrets Officer to add secrets. Time: 15 minutes.
Where to look
- Home
- Key vaults
- + Create
Steps
- Open Key vaults and select + Create.
- Choose rg-portal-practice, a unique name such as kv-portal plus your initials, your region and the Standard pricing tier.Region may start on East US, so change it to one near you.
- Soft delete is always on. Leave Purge protection on Disable purge protection for this practice.With purge protection on, the vault's name stays locked for the whole retention period after you delete it.
- On Access configuration, keep Azure role-based access control. Select Review + create, then Create.
- Open the vault and select Secrets. You'll see you aren't authorised yet. Select Access control (IAM), then + Add, Add role assignment, and give yourself Key Vault Secrets Officer.
- Back in Secrets, select + Generate/Import. Name it greeting, enter the value hello-from-key-vault, and select Create.Use a made-up value. Never store a real password in a practice vault.
- Open the secret, select its current version, and use Show Secret Value to read it.
Commands
# Create the vault (role-based access is the default for new vaults)
az keyvault create --resource-group rg-portal-practice --name kv-portal-<initials> --location uksouth
# Give yourself the right to manage secrets
az role assignment create --role "Key Vault Secrets Officer" \
--assignee $(az ad signed-in-user show --query id --output tsv) \
--scope $(az keyvault show --name kv-portal-<initials> --query id --output tsv)
# Add and read a secret
az keyvault secret set --vault-name kv-portal-<initials> --name greeting --value hello-from-key-vault
az keyvault secret show --vault-name kv-portal-<initials> --name greeting --query value --output tsv
Watch out for
- Cost
- The Standard tier charges a tiny amount per 10,000 operations. A practice vault with a few secrets costs a fraction of a penny.
- Security
- Give people and apps the narrowest role: Key Vault Secrets User to read secrets, Secrets Officer to manage them. Turn on purge protection for real vaults so nobody can destroy secrets for good.
- Common mistakes
- Expecting Owner or Contributor to read secrets: they manage the vault, not its contents. Deleting a vault and finding the name still taken because of soft delete.
Clean up
- Key vaults
- kv-portal
- Delete
Keep the vault for the next task. When you delete it, it moves to Manage deleted vaults on the Key vaults page; purge it there if you want the name back straight away.
Words to know
- Secret
- Any small piece of sensitive text, such as a password or connection string.
- Soft delete
- Deleted vaults and secrets are kept for a while (90 days by default) and can be recovered.
- Purge protection
- Stops anyone permanently deleting a vault or secret until the retention period ends. Can't be turned off once on.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On