fbpx

Learn Build5Nines Forum

Find answers, ask questions, and connect with our
global community of IT professionals.

Forums HashiCorp Terraform Terraform with Azure Key Vault to get secret value

  • Terraform with Azure Key Vault to get secret value

    Posted by Joshua Falken on March 15, 2024 at 3:22 pm

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

    Doesn’t look like value gets exposed in the key vault secret object.

    • This discussion was modified 1 month, 2 weeks ago by  Joshua Falken.
    Chris Pietschmann replied 3 weeks, 5 days ago 2 Members · 2 Replies
  • 2 Replies
  • Joshua Falken

    Member
    March 15, 2024 at 3:25 pm

    In Terraform, you can indeed retrieve the value of a secret from Azure Key Vault using the azurerm_key_vault_secret data source. However, you are correct that the actual secret value is not directly exposed in the Terraform object due to security concerns. Instead, Terraform stores a reference to the secret.

    Here’s how you can use the azurerm_key_vault_secret data source to retrieve a secret:

    data "azurerm_key_vault_secret" "example" {

    name = "example-secret"

    key_vault_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.KeyVault/vaults/example-key-vault"

    }

    output "secret_value" {

    value = data.azurerm_key_vault_secret.example.value

    }

    In the above example:

    • example-secret is the name of the secret you want to retrieve.
    • example-key-vault is the name of your Azure Key Vault.
    • example-resources is the name of the resource group where your Azure Key Vault resides.

    After running terraform apply, you can use terraform output secret_value to see the secret value. However, note that even though you can see the secret value in the Terraform output, it’s not recommended to store or expose secrets in plain text. It’s best practice to use solutions like Azure Key Vault to manage and securely access your secrets.

  • Chris Pietschmann

    Administrator
    April 2, 2024 at 1:51 pm

    Here’s an article I wrote that shows this with more detail, and also includes referencing the Key Vault secret from an App Service app setting: https://build5nines.com/terraform-deploy-azure-app-service-with-key-vault-secret-integration/

Log in to reply.