You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform中azurerm_virtual_machine的custom_data字段使用及报错排查

解决Terraform azurerm_virtual_machine中custom_data字段报错的问题

你遇到的问题是因为在你使用的azurerm provider v1.1.1版本中,custom_data并不是azurerm_virtual_machine资源的顶级配置字段,而是需要嵌套在os_profile块内使用。

在Terraform 0.11.x搭配azurerm v1.x早期版本的环境下,正确的写法应该是这样的:

resource "azurerm_virtual_machine" "csrVM" {
  name                  = "your-vm-name"
  location              = azurerm_resource_group.your-rg.location
  resource_group_name   = azurerm_resource_group.your-rg.name
  network_interface_ids = [azurerm_network_interface.your-nic.id]
  vm_size               = "Standard_DS1_v2"

  # 镜像、磁盘等其他必要配置...

  os_profile {
    computer_name  = "vm-hostname"
    admin_username = "admin-user"
    # custom_data必须放在这里,且需要base64编码
    custom_data    = base64encode(file("path/to/your/cloud-init-config.txt"))
  }

  # Linux/Windows系统的额外配置(比如SSH密钥、密码禁用等)...
}

还要注意两个关键细节:

  • custom_data的值必须是base64编码后的字符串,推荐用base64encode()函数处理你的配置内容(比如读取本地cloud-init文件),如果直接写文本字符串,也要手动转为base64格式。
  • 这个字段的位置在后续azurerm provider版本(比如v2.x及以上)中有所调整,但针对你当前使用的v1.1.1,必须严格嵌套在os_profile块里。

内容的提问来源于stack exchange,提问作者explorer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:04:45