Terraform 的 Azure 虚拟机扩展文件 URL 位置

Azure Virtual Machine Extension file URL location with Terraform

问题有点令人困惑,但我会解释我在做什么。我正在 Terraform 中构建一个新的 Azure VM,并在虚拟机扩展中调用脚本。它是一个基本的 powershell 脚本。问题是,我目前正在从 public GitHub 帐户调用它。

resource "azurerm_virtual_machine_extension" "winrm" {
  name                  = "winrm"
  location              = var.location
  resource_group_name   = var.rg_name
  count                 = length(var.vm_name_suffix)
  virtual_machine_name  = azurerm_virtual_machine.vm[count.index].name
  publisher             = "Microsoft.Compute"
  type                  = "CustomScriptExtension"
  type_handler_version  = "1.9"

  settings = <<SETTINGS
  {
    "fileUris": ["https://raw.githubusercontent.com/<name>/master/winrm.ps1"], 
    "commandToExecute": "powershell.exe -ExecutionPolicy unrestricted -NoProfile -NonInteractive -File \"./winrm.ps1\""
  }
  SETTINGS
}

试图弄清楚是否有一种方法可以从更安全的地方调用它。我在 Azure DevOps 存储库中进行了此设置,但我不确定如何将任何类型的身份验证传递到设置块中。我也可以将它放在私人 GitHub 帐户上,但我还是需要提供一种对文件进行身份验证的方法。

document 开始,您可以将数据包含在 protectedSettings 中,并且 Azure VM 扩展保护设置数据已加密,并且仅在目标虚拟机上解密。

You can store sensitive data in a protected configuration, which is encrypted and only decrypted inside the virtual machine. The protected configuration is useful when the execution command includes secrets such as a password.

在这种情况下,建议将脚本上传到 blob 存储,然后使用存储帐户密钥从该存储帐户调用扩展文件。

例如:

resource "azurerm_virtual_machine_extension" "winrm" {
...

  settings = <<SETTINGS
    {
        "fileUris": ["https://mystorageaccountname.blob.core.windows.net/postdeploystuff/winrm.ps1"]
    }
SETTINGS
  protected_settings = <<PROTECTED_SETTINGS
    {
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File winrm.ps1",
      "storageAccountName": "mystorageaccountname",
      "storageAccountKey": "xxxxx"
    }
  PROTECTED_SETTINGS

  depends_on = ["azurerm_virtual_machine.vm[count.index].name"]


}

有关详细信息,您可以参考blog关于将 Terraform 与 A​​zure VM 扩展一起使用。