为什么这对我不起作用?我见过这段代码在任何地方都能正常工作,但我一直收到以下错误。真令人沮丧……
Error: Error running plan: 1 error(s) occurred:
* azurerm_virtual_machine.main: 2 error(s) occurred:
* azurerm_virtual_machine.main[0]: Resource 'azurerm_network_interface.main' does not have attribute 'id' for variable 'azurerm_network_interface.main.*.id'
* azurerm_virtual_machine.main[1]: Resource 'azurerm_network_interface.main' does not have attribute 'id' for variable 'azurerm_network_interface.main.*.id'
resource "azurerm_network_interface" "main" {
count = "${length(var.hostname)}"
name = "${var.hostname[count.index]}-nic"
location = "${azurerm_resource_group.main.*.location}"
resource_group_name = "${azurerm_resource_group.main.*.name}"
ip_configuration {
name = "${var.hostname[count.index]}-ipaddress"
subnet_id = "${var.subnet[count.index]}"
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_machine" "main" {
name = "${var.aplication}-${var.region[count.index]}-${var.hostname[count.index]}-vm"
location = "${azurerm_resource_group.main.*.location}"
resource_group_name = "${azurerm_resource_group.main.*.name}"
network_interface_ids = ["${element(azurerm_network_interface.main.*.id, count.index)}"]
vm_size = "Standard_DS1_v2"
# 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 = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "${var.hostname[count.index]}"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "staging"
}
count = "${length(var.hostname)}"
depends_on = ["azurerm_network_interface.main"]
}
答案1
看起来您构造的设置 Count 并未生成预期的资源数组,而是生成了单个资源。
您应该能够通过调用来验证是否已启用调试日志运行TF_LOG=DEBUG terraform plan
。