Let’s get started with creating some secrets in the Azure Key Vault using Terraform. If you prefer to create your secrets in a different way, feel free to skip this step.
First, we’ll need to setup our Azure Terraform Provider.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}
provider "azurerm" {
features {}
}
For the sake of keeping things short, I will assume that you have already created an Azure Key Vault as well as a Resource Group. For more information on how to do this via Terraform, see here.
As we’ll need additional metadata to identify the vault in the following step, let’s now access it.
data "azurerm_key_vault" "keyvault" {
name = "myvault"
resource_group_name = "myresourcegroup"
}
Instead of hardcoding the values here, we could also define input variables.
Let’s get down to the secret creation. We’ll use the default Terraform resource random_password
to create it.
resource "random_password" "mysecret" {
length = 64
}
Now, all that is left is to store the generated secret in our vault.
resource "azurerm_key_vault_secret" "mysecret" {
name = "mysecret"
value = random_password.mysecret.result
key_vault_id = data.azurerm_key_vault.keyvault.id
}