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

时间:2019-09-12 15:53:01

标签: azure terraform azure-rm

问题有点令人困惑,但我将解释我在做什么。我正在Terraform中构建新的Azure VM,并在虚拟机扩展内部调用脚本。这是一个基本的powershell脚本。问题是,我目前正在从公共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存储库中进行了设置,但是我不确定如何将任何类型的身份验证传递到settings块中。我也可以将其放在一个私有GitHub帐户上,但再次,我需要提供一种对文件进行身份验证的方法。

1 个答案:

答案 0 :(得分:1)

可以从document中将数据包括在protectedSettings中,并且Azure VM扩展受保护的设置数据被加密,并且仅在目标虚拟机上被解密。

  

您可以将敏感数据存储在受保护的配置中,即   加密且仅在虚拟机内部解密。受保护的   当执行命令包含秘密时,配置很有用   例如密码。

在这种情况下,建议将脚本上传到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与Azure VM扩展一起使用。