无法通过 terraform 为 Azure 虚拟机设置动态 IP [英] Unable to set dynamic IP to Azure Virtual machines by terraform

查看:25
本文介绍了无法通过 terraform 为 Azure 虚拟机设置动态 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定哪里出了问题,但我无法使以下代码正常工作.

Not sure where things are going wrong but I am unable to get the following code working.

目标:创建两个(或更多)具有公共 IP 的虚拟机.

Aim: To create two (or more) virtual machine with public IP.

问题:遇到 terraform plan 报告错误,如错误代码块所示.

Issue: Stuck with terraform plan reporting erros as indicated in Error code block.

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'

想不通...任何帮助都会很棒.

Couldn't figure it out... Any assistance will be great.

推荐答案

您创建了 2 个公共 ip,而不是一个,但您尝试引用它,就像它是单个 ip 一样,但事实并非如此.它是一个列表.您需要获取个人公共 ip id,如下所示:

you create 2 public ips, not one, but you try and reference it like it was a single ip, but its not. its a list. you need to get individual public ip id, something like this:

"${element(azurerm_public_ip.tf-pubip-cluster-aos.*.id, count.index)}"

这篇关于无法通过 terraform 为 Azure 虚拟机设置动态 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆