使用 Terraform for Azure 创建多个虚拟机的问题 [英] Issues with creating multiple vm's with Terraform for Azure

查看:60
本文介绍了使用 Terraform for Azure 创建多个虚拟机的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Terraform 在 Azure 中创建多个虚拟机时遇到问题.在创建网络接口时,我遇到了关于创建公共 IP 地址 ID 的错误:

I am having issues with creating multiple virtual machines in Azure using Terraform. While creating the network interfaces I run into an error regarding the creation of the public ip address id:

我假设我错误地使用了计数函数,或者完全需要一种不同的方法.

I assume that I am using the count function incorrectly, or need a different approach entirely.

代码:

provider "azurerm" {
  version = "~>2.0"
  features {}

  subscription_id = "XXXX"
  client_id       = "XXXX"
  client_secret   = "XXXX"
  tenant_id       = "XXXX"
}

resource "azurerm_resource_group" "rg" {
    name        = "${var.prefix}test_project"
    location    = var.location
    tags        = var.tags
}

resource "azurerm_virtual_network" "vnet" {
    name                = "${var.prefix}Vnet"
    address_space       = ["10.0.0.0/16"]
    location            = var.location
    resource_group_name = azurerm_resource_group.rg.name
    tags                = var.tags
}

resource "azurerm_subnet" "subnet" {
    name                 = "${var.prefix}Subnet"
    resource_group_name  = azurerm_resource_group.rg.name
    virtual_network_name = azurerm_virtual_network.vnet.name
    address_prefix       = "10.0.1.0/24"
}

resource "azurerm_public_ip" "publicip" {
    name                    = "${var.prefix}PublicIP${count.index}"
    location                = var.location
    resource_group_name     = azurerm_resource_group.rg.name
    allocation_method       = "Dynamic"
    tags                    = var.tags
    count                   = 2
}

resource "azurerm_network_security_group" "nsg" {
    name                = "${var.prefix}NetworkSecurityGroup"
    location            = var.location
    resource_group_name = azurerm_resource_group.rg.name
    tags                = var.tags

    security_rule {
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }
}

resource "azurerm_network_interface" "nic" {
    name                        = "${var.prefix}NIC${count.index}"
    location                    = var.location
    resource_group_name         = azurerm_resource_group.rg.name
    tags                        = var.tags
    count                       = 2

    ip_configuration {
        name                          = "${var.prefix}NICConfig${count.index}"
        subnet_id                     = azurerm_subnet.subnet.id
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = ["${element(azurerm_public_ip.publicip.id, count.index)}"]
    }
}

resource "azurerm_network_interface_security_group_association" "example" {
    count = length(azurerm_network_interface.nic)
    network_interface_id      = "${azurerm_network_interface.nic[count.index]}"
    network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_linux_virtual_machine" "vm" {
    count                   = 2
    name                    = "${var.prefix}VM${count.index}"
    location                = var.location
    resource_group_name     = azurerm_resource_group.rg.name
    network_interface_ids   = azurerm_network_interface.nic[count.index]
    size                    = "Standard_DS1_v2"
    tags                    = var.tags

    os_disk {
        name                    = "${var.prefix}OsDisk${count.index}"
        caching                 = "ReadWrite"
        storage_account_type    = "Premium_LRS"
    }

    source_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = lookup(var.sku, var.location)
        version   = "latest"
    }

    computer_name  = "${var.computer_name}-${count.index}"
    admin_username = var.admin_username
    admin_password = var.admin_password
    disable_password_authentication = false
}

谁能帮我解决这个问题??

Can anyone help me resolve this issue??

推荐答案

我很确定你需要做的就是改变

I'm pretty sure all you need to do is change

        public_ip_address_id          = ["${element(azurerm_public_ip.publicip.id, count.index)}"]

        public_ip_address_id          = ["${azurerm_public_ip.publicip[count.index].id}"]

一般来说,像 azurerm_public_ip.publicip.id 这样的引用适用于单一资源(即那些不使用计数的资源).所以 element 的使用有点像假设一个单一的资源.一旦使用了 count,资源就会开始表现得像列表一样,需要这样对待.

In general, references like azurerm_public_ip.publicip.id work for singular resources (i.e. those that don't use count). So the use of element is kind of assuming a singular resource. As soon as count is used, resources start behaving like lists and need to be treated as such.

这篇关于使用 Terraform for Azure 创建多个虚拟机的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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