【问题标题】:Azure Terraform - Encrypt VM OS DiskAzure Terraform - 加密 VM 操作系统磁盘
【发布时间】:2020-03-14 03:49:50
【问题描述】:

我正在尝试通过 Terraform 加密 Azure VM 上的“storage_os_disk”。 我已在 VM OS 磁盘上设置了托管磁盘类型,因此将对其进行托管,因为我知道必须对磁盘进行托管以允许加密。

我似乎无法弄清楚如何在 terraform 中加密 OS 磁盘

这是我正在尝试的代码:

resource "azurerm_network_interface" "nic" {
  name                = "${var.project_ident}-${var.env_ident}-${var.admin_vm_name}-${var.region_suffix}-encrpytest"
  location            = "${data.azurerm_resource_group.core-rg.location}"
  resource_group_name = "${data.azurerm_resource_group.core-rg.name}"
  depends_on            = ["azurerm_virtual_machine.dns-vm"]

  ip_configuration {
    name                          = "${var.project_ident}-${var.env_ident}-${var.admin_vm_name}-${var.region_suffix}-encrpytest"
    subnet_id                     ="${data.terraform_remote_state.network.sn1_id}"
    private_ip_address_allocation = "static"
    private_ip_address            = "${cidrhost(data.terraform_remote_state.network.sn1_address_prefix, 6 )}"
  }  
}

resource "azurerm_virtual_machine" "admin-vm-encrpytest" {
  name                  = "${var.project_ident}-${var.env_ident}-${var.admin_vm_name}-encrpytest"
  location              = "${data.azurerm_resource_group.core-rg.location}"
  resource_group_name   = "${data.azurerm_resource_group.core-rg.name}"
  network_interface_ids = ["${azurerm_network_interface.nic.id}"]
  vm_size               = "Standard_B2s"
  depends_on            = ["azurerm_virtual_machine.dns-vm"]


  # Requires LRS Storage Account
   boot_diagnostics {
   enabled      = "True"
   storage_uri  = "${data.terraform_remote_state.sa.sa_2_prim_blob_ep}"
   #storage_uri  = "${data.azurerm_storage_account.storage-account-2.primary_blob_endpoint}"
  }

  storage_os_disk {
    name          = "${var.project_ident}-${var.env_ident}-${var.admin_vm_name}-${var.region_suffix}-encrpytest"
    create_option = "FromImage"
    managed_disk_type = "Standard_LRS"

    encryption_settings {
      enabled      = "True"

      key_encryption_key {
        key_url = "${data.terraform_remote_state.kv.vault_key_1_id}"
        source_vault_id = "${data.terraform_remote_state.kv.vault_id}"
      }

      disk_encryption_key {
        secret_url = "${data.terraform_remote_state.kv.vault_key_2_id}"
        source_vault_id = "${data.terraform_remote_state.kv.vault_id}"
      }
    }


  }

  os_profile {
    computer_name  = "encrpytest"
    admin_username = "cactusadmin"
    admin_password = "${var.admin_vm_password}"
  }

  os_profile_windows_config {
    provision_vm_agent        = true
    enable_automatic_upgrades = true
  }

  # Uncomment this line to delete the OS disk automatically when deleting the VM
  delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
}

谢谢

【问题讨论】:

    标签: azure encryption virtual-machine terraform


    【解决方案1】:

    首先,encryption_settings 不存在于storage_os_disk 块中,而是存在于azurerm_managed_disk。因此,您可以创建一个单独的azurerm_managed_disk 资源,然后使用引用here 的平台映像从托管磁盘创建VM。

    或者,您可以尝试使用azurerm_virtual_machine_extension 进行磁盘加密,请参阅this。

    resource "azurerm_virtual_machine_extension" "disk-encryption" {
      name                 = "DiskEncryption"
      location             = "${local.location}"
      resource_group_name  = "${azurerm_resource_group.environment-rg.name}"
      virtual_machine_name = "${azurerm_virtual_machine.server.name}"
      publisher            = "Microsoft.Azure.Security"
      type                 = "AzureDiskEncryption"
      type_handler_version = "2.2"
    
      settings = <<SETTINGS
    {
      "EncryptionOperation": "EnableEncryption",
      "KeyVaultURL": "https://${local.vaultname}.vault.azure.net",
      "KeyVaultResourceId": "/subscriptions/${local.subscriptionid}/resourceGroups/${local.vaultresourcegroup}/providers/Microsoft.KeyVault/vaults/${local.vaultname}",
      "KeyEncryptionKeyURL": "https://${local.vaultname}.vault.azure.net/keys/${local.keyname}/${local.keyversion}",
      "KekVaultResourceId": "/subscriptions/${local.subscriptionid}/resourceGroups/${local.vaultresourcegroup}/providers/Microsoft.KeyVault/vaults/${local.vaultname}",
      "KeyEncryptionAlgorithm": "RSA-OAEP",
      "VolumeType": "All"
    }
    SETTINGS
    }
    

    【讨论】:

    • Ty 表示响应。我注意到我必须对托管磁盘使用加密设置,但是创建托管磁盘不会接受我尝试的任何“image_reference_id”。我什至尝试通过“ $vmImageId = az vm image show --urn $($vmImagePublisher + ":" + $vmImageOffer + ":" + $vmImageSku + ":" + $vmImageVersion) --query 从 powershell 传递 ID 'id'-o json”。我使用“平台映像”方法的问题是我需要使用最新的映像,并且它不提供版本参数。我正在考虑尝试 VM 扩展,但我觉得那是我们的解决方案
    • 该扩展似乎只适用于 Windows VM。在安装时,我收到一个错误,我无法运行用于安装的 exe 文件。
    【解决方案2】:

    我使用了 vm 扩展示例,它运行良好。我新部署的 Windows VM 上的操作系统磁盘立即被加密

    【讨论】:

      猜你喜欢
      • 2017-08-30
      • 1970-01-01
      • 2019-11-01
      • 2023-03-18
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      相关资源
      最近更新 更多