Terraform with Azure Key Vault to get secret value Terraform with Azure Key Vault to get secret value azure azure

Terraform with Azure Key Vault to get secret value


Now you can do it with azurerm_key_vault_secret data source.

I'm enjoying without any scripting.

data "azurerm_key_vault_secret" "test" {  name      = "secret-sauce"  vault_uri = "https://rickslab.vault.azure.net/"}output "secret_value" {  value = "${data.azurerm_key_vault_secret.test.value}"}


You first need to create a data resource to the azure key vault to get the key vault resource ID:

data "azurerm_key_vault" "keyvault" {  name                = "${var.keyvault_name}"  resource_group_name = "${var.resourcegroup_name}"}

And then use azurerm_key_vault_secret to get the secret with the key vault resource Id:

data "azurerm_key_vault_secret" "win_admin_pass" {  name         = "${var.secret_name}"  key_vault_id = "${data.azurerm_key_vault.keyvault.id}"}

Please note that the use of vault_uri in azurerm_key_vault_secret is deprecated and not recommended.


Is there any way to get the value of a secret from Azure Key Vault?

As a workaround, we can use PowerShell to get this value, like this:

$a = Get-AzureKeyVaultSecret -VaultName "jasonkey" -Name "jason"$a.SecretValueText

enter image description here