无法通过 terraform 为 Azure 虚拟机设置动态 IP
Unable to set dynamic IP to Azure Virtual machines by terraform
不确定哪里出了问题,但我无法使以下代码正常工作。
目标:创建两个(或更多)具有public IP 的虚拟机。
问题:卡在错误代码块中指示的 terraform 计划报告错误。
Terraform 代码块如下:
resource "azurerm_public_ip" "tf-pubip-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-PUBIP"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
allocation_method = "Dynamic"
}
resource "azurerm_network_interface" "tf-ni-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-NI"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
ip_configuration {
name = "${var.ax_base_hostname}-${count.index+1}-IP"
subnet_id = "${data.azurerm_subnet.tf-sn-cluster-aos.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.tf-pubip-cluster-aos.id}"
}
}
resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
availability_set_id = "${azurerm_availability_set.tf-as-cluster-aos.id}"
network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index)}"]
vm_size = "${var.ax_vm_size}"
}
错误信息如下:
Error running plan: 1 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos: 2 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos[0]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
azurerm_network_interface.tf-ni-cluster-aos[1]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
想不通...任何帮助都会很棒。
您创建了 2 public 个 ip,而不是一个,但是您尝试引用它就像它是一个 ip,但它不是。它是一个列表。你需要获得个人 public ip id,像这样:
"${element(azurerm_public_ip.tf-pubip-cluster-aos.*.id, count.index)}"
不确定哪里出了问题,但我无法使以下代码正常工作。
目标:创建两个(或更多)具有public IP 的虚拟机。
问题:卡在错误代码块中指示的 terraform 计划报告错误。
Terraform 代码块如下:
resource "azurerm_public_ip" "tf-pubip-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-PUBIP"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
allocation_method = "Dynamic"
}
resource "azurerm_network_interface" "tf-ni-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}-NI"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
ip_configuration {
name = "${var.ax_base_hostname}-${count.index+1}-IP"
subnet_id = "${data.azurerm_subnet.tf-sn-cluster-aos.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.tf-pubip-cluster-aos.id}"
}
}
resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
count = 2
name = "${var.ax_base_hostname}-${count.index+1}"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
availability_set_id = "${azurerm_availability_set.tf-as-cluster-aos.id}"
network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index)}"]
vm_size = "${var.ax_vm_size}"
}
错误信息如下:
Error running plan: 1 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos: 2 error(s) occurred:
azurerm_network_interface.tf-ni-cluster-aos[0]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
azurerm_network_interface.tf-ni-cluster-aos[1]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
想不通...任何帮助都会很棒。
您创建了 2 public 个 ip,而不是一个,但是您尝试引用它就像它是一个 ip,但它不是。它是一个列表。你需要获得个人 public ip id,像这样:
"${element(azurerm_public_ip.tf-pubip-cluster-aos.*.id, count.index)}"