Quickstart: Set and retrieve a key from Azure Key Vault using Azure CLI
In this quickstart, you create a key vault in Azure Key Vault with Azure CLI. Azure Key Vault is a cloud service that works as a secure secrets store. You can securely store keys, passwords, certificates, and other secrets. For more information on Key Vault, review the Overview. Azure CLI is used to create and manage Azure resources using commands or scripts. Once that you've completed that, you will store a key.
If you don't have an Azure trial subscription, create an Azure trial subscription before you begin.
Prerequisites
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
- This quickstart requires version 2.0.4 or later of the Azure CLI.
Create a resource group
A resource group is a logical container into which Azure resources are deployed and managed. Use the az group create command to create a resource group named myResourceGroup in the chinaeast location.
az group create --name "myResourceGroup" -l "ChinaNorth"
Create a key vault
Use the Azure CLI az keyvault create command to create a Key Vault in the resource group from the previous step. You will need to provide some information:
Key vault name: A string of 3 to 24 characters that can contain only numbers (0-9), letters (a-z, A-Z), and hyphens (-)
Important
Each key vault must have a unique name. Replace <your-unique-keyvault-name> with the name of your key vault in the following examples.
Resource group name: myResourceGroup.
The location: ChinaEast.
az keyvault create --name "<your-unique-keyvault-name>" --resource-group "myResourceGroup"
The output of this command shows properties of the newly created key vault. Take note of these two properties:
- Vault Name: The name you provided to the
--name
parameter. - Vault URI: In this example, the Vault URI is https://<your-unique-keyvault-name>.vault.azure.cn/. Applications that use your vault through its REST API must use this URI.
Give your user account permissions to manage keys in Key Vault
To gain permissions to your key vault through Role-Based Access Control (RBAC), assign a role to your "User Principal Name" (UPN) using the Azure CLI command az role assignment create.
az role assignment create --role "Key Vault Crypto Officer" --assignee "<upn>" --scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.KeyVault/vaults/<your-unique-keyvault-name>"
Replace <upn>, <subscription-id>, <resource-group-name> and <your-unique-keyvault-name> with your actual values. Your UPN will typically be in the format of an email address (e.g., username@domain.com).
Add a key to Key Vault
To add a key to the vault, you just need to take a couple of additional steps. This key could be used by an application.
Type this command to create a key called ExampleKey :
az keyvault key create --vault-name "<your-unique-keyvault-name>" -n ExampleKey --protection software
You can now reference this key that you added to Azure Key Vault by using its URI. Use https://<your-unique-keyvault-name>.vault.azure.cn/keys/ExampleKey
to get the current version.
To view previously stored key:
az keyvault key show --name "ExampleKey" --vault-name "<your-unique-keyvault-name>"
Now, you've created a Key Vault, stored a key, and retrieved it.
Clean up resources
Other quickstarts and tutorials in this collection build upon this quickstart. If you plan to continue on to work with subsequent quickstarts and tutorials, you may wish to leave these resources in place.
When no longer needed, you can use the Azure CLI az group delete command to remove the resource group and all related resources:
az group delete --name "myResourceGroup"
Next steps
In this quickstart, you created a Key Vault and stored a key in it. To learn more about Key Vault and how to integrate it with your applications, continue on to these articles.
- Read an Overview of Azure Key Vault
- See the reference for the Azure CLI az keyvault commands
- Review the Key Vault security overview