Set and retrieve secrets from Key vault using Azure CLI

You can create and retrieve secrets stored in Azure Key vault using the Azure Portal or command line tools. In this blog post, we will focus on creating a secret in Azure Key Vault using Azure CLI and demonstrate how to retrieve the secret.

STEP 1 – Install Azure CLI

Before we begin, ensure that you have Azure CLI installed on your local system. If you don’t have it installed yet, you can download Azure CLI by clicking on this link: https://aka.ms/installazurecliwindows.

To learn more about installing Azure CLI on Windows, please refer to the following link: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli.

Once you’ve downloaded the Azure CLI MSI file, double-click on it to launch the installer and follow the wizard’s instructions to complete the installation on your system.

Install Azure CLI
Install Azure CLI
  • After Azure CLI installation is completed, Open the command prompt or Powershell and run az version command to verify if it’s installed and working fine.
Install Azure CLI
Install Azure CLI

STEP 2 – Authenticate with Azure using az login

To use Azure CLI and work with Azure Key Vault, you need to log in and authenticate with your Azure account. You can do this by running the “az login” command in your command prompt or terminal.

This will open a browser session where you can complete the authentication process by logging in with your administrator account.

Authenticate with Azure using az login
Authenticate with Azure using az login

STEP 3 – Create Azure Key Vault

The next step is to create an Azure Key Vault using the “az keyvault create” command. If you already have an Azure Key Vault created and just want to add a secret to it, you can skip this step. Make sure to replace the example values with your desired naming convention.

az keyvault create --name "newKeyVaultUKSouth01" --resource-group "UkSouthRg" --location "UKSouth"
Create Azure KeyVault
Create Azure KeyVault

STEP 4 – Create Azure Key Vault Secret

To create an Azure Key Vault secret, you can use the “az keyvault secret” command. Here is an example of creating an Azure Key Vault secret:

az keyvault secret set --vault-name "newKeyVaultUKSouth01" --name "SqlAdmin" --value "Jhne&(nol@jdn88HHG"
Azure Keyvault secret set
Create Azure KeyVault Secret
  • You can confirm the successful creation of your secret in the Azure Key Vault by checking the Azure Portal.
Create Azure KeyVault Secret
Create Azure KeyVault Secret

STEP 5 – Retrieve Azure KeyVault Secret

To retrieve an Azure Key Vault secret, you can utilize the “az keyvault secret show” command. Refer to the example below to understand how to retrieve secrets from an Azure Key Vault.

az keyvault secret show --name "SqlAdmin" --vault-name "newKeyVaultUKSouth01" --query "value"
Retrieve Azure KeyVault Secret
Retrieve Azure KeyVault Secret

Leave a Comment