I’m struggling to find and obtain the secretURL used for the disk encryption key encryption block for the azurerm_managed_disk resource block in terraform. Anyone know how to obtain this? The docs states this, but not sure how to get the secret of the key in the key vault.
secret_url – (Required) The URL to the Key Vault Secret used as the
Disk Encryption Key. This can be found as id on the
azurerm_key_vault_secret resource.
data "azurerm_key_vault" "kv" {
name = var.disk_encryption_key_vault_name
resource_group_name = var.disk_encryption_key_rg
}
data "azurerm_key_vault_key" "encryption-kv" {
name = var.disk_encryption_key_name
key_vault_id = data.azurerm_key_vault.kv.id
}
resource "azurerm_managed_disk" "data" {
count = var.data_disk_count
name = "${var.vm_name}-DataDisk-${count.index + 1}"
location = var.location
resource_group_name = var.resource_group_name
storage_account_type = var.data_disk_storage_account_type
create_option = "Empty"
disk_size_gb = var.data_disk_size_gb
tags = var.tags
encryption_settings {
enabled = true
disk_encryption_key {
secret_url =
source_vault_id = data.azurerm_key_vault_key.encryption-kv.key_vault_id
}
key_encryption_key {
key_url = "https://${data.azurerm_key_vault.kv.name}.vault.usgovcloudapi.net/keys/${var.disk_encryption_key_name}/${data.azurerm_key_vault_key.encryption-kv.version}"
source_vault_id = data.azurerm_key_vault_key.encryption-kv.key_vault_id
}
}
}
2
Answers
The Blocker you mentioned is due to use of depreciating
encryption_settings
inside disk configuration.In order to use the
Secret URL
fordisk_encryption_key
block you need to use theazurerm_disk_encryption_set
to pass the encryption key from the vault & also the vault should have all necessary permissions.I tried the terraform configuration with the necessary changes so that you can use the key and secret from the vault.
My terraform configuration:
Deployment succeeded:
Make sure you add permission for managed identity created
in
azurerm_disk_encryption_set
follow the SOwhich will help you have better understanding on that permission & also Location of vault and disk should match.
Refer:
Azure portal – Enable customer-managed keys with SSE – managed disks – Azure Virtual Machines | Microsoft Learn
I don’t have azure account, so can’t test it, but terraform is terraform, so it is going to be this:
Now you need to add
azurerm_key_vault_secret
resource:And now you can pull the
id
of theazurerm_key_vault_secret
resource.