Search
Join the Technical Preview Program
See how NVMe-oF removes iSCSI
bottlenecks in your HCI
The Best Hyperconverged
Infrastructure
(HCI) for Enterprise
ROBO, SMB & Edge
The Best Virtual SAN
for Enterprise ROBO, SMB & Edge

[Azure] Get a value in a Keyvault with Terraform

  • August 1, 2019
  • 4 min read
Cloud and Virtualization Architect and Microsoft MVP. Florent specializes in public, private, and hybrid cloud ecosystems. An MCSE in Private Cloud, he provides technical leadership in Cloud and Datacenter Management. Florent delivers high-authority insights on Azure deployments, virtualization strategy, and optimizing enterprise-scale cloud infrastructure.
Cloud and Virtualization Architect and Microsoft MVP. Florent specializes in public, private, and hybrid cloud ecosystems. An MCSE in Private Cloud, he provides technical leadership in Cloud and Datacenter Management. Florent delivers high-authority insights on Azure deployments, virtualization strategy, and optimizing enterprise-scale cloud infrastructure.

By default, if you want to deploy a VM on Azure with Terraform, you must give the username and password in clear in the variable file (see my previous article). But a solution exists, to secure all this 🙂 Just use an Azure Keyvault, and store your password in:

Azure Keyvault secrets

Then, you have to add these lines, at the beginning of your code:

// Get Keyvault Data

data "azurerm_resource_group" "rg_keyvault" {

name = "${var.rg_keyvault}"

}

data "azurerm_key_vault" "keyvault" {

name = "${var.keyvault_name}"

resource_group_name = "${data.azurerm_resource_group.rg_keyvault.name}"

}

data "azurerm_key_vault_secret" "secret_Default-Admin-Windows-Linux-VM" {

name = "Default-Admin-Windows-Linux-VM"

vault_uri = "${data.azurerm_key_vault.keyvault.vault_uri}"

}

Here we will get the RG, the Keyvault, and the secret that has the Default-Admin-Windows-Linux-VM name in my keyvault and that contains my default password for my VMs. We must then adapt the code so that our admin_password variable takes the value that is in the keyvault:

admin_password = "${data.azurerm_key_vault_secret.secret_Default-Admin-Windows-Linux-VM.value}"

You can now do a terraform init and terraform plan. You should have the following error:

Default-Admin-Windows-Linux-VM name

This is normal. In fact, you must give the Get and List secrets privileges to the application that is used to deploy the resources in Azure via Terraform in the keyvault:

AzureDevops Permission

You can do again a terraform plan and terraform apply to deploy your resources in a secure way. Note this error message, which will disappear when updating to version 2 of the provider, but where you have to adapt the code:

Terraform plan and terraform apply

This code is available on my Github:

https://github.com/Flodu31/Terraform/tree/master/Deploy_New_Environment_Keyvault

Hey! Found Florent’s insights useful? Looking for a cost-effective, high-performance, and easy-to-use hyperconverged platform?
Taras Shved
Taras Shved StarWind HCI Appliance Product Manager
Look no further! StarWind HCI Appliance (HCA) is a plug-and-play solution that combines compute, storage, networking, and virtualization software into a single easy-to-use hyperconverged platform. It's designed to significantly trim your IT costs and save valuable time. Interested in learning more? Book your StarWind HCA demo now to see it in action!