多次针对count.index的Azure多区域部署错误

时间:2019-09-30 15:42:22

标签: azure deployment terraform

我正在尝试使用最简单的terraform脚本在6个不同的Azure区域中部署6个linux vm,但是,我的概念似乎不起作用,并且我不确定该怎么去。

我在多个地方收到此错误:

Error: Reference to "count" in non-counted context

  on main.tf line 4, in resource "azurerm_resource_group" "mygroup":
   4:     location = "${var.regions[count.index]}"

The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.

如果我只是在指定位置放置一个字符串,那么我在其他地方只会遇到更多类似的错误。

我编写了一些terraform代码,并尝试使用terraform可执行文件对其进行验证。

我的variables.tf文件:

variable “regions” {
description = “Regions to deploy”
type = list
default = [“canadaeast”, “eastus”, “japaneast”, “ukwest”, “southeastasia”, “germanynorth”]
}

variable “name” {
description = “name”
type = list
default = [“albert”, “ben”, “carol”, “denis”, “eric”, “frank”]
}

和我的main.tf文件:

# Create a resource group
resource "azurerm_resource_group" "mygroup" {
    name     = "RG-MyVMs"
    location = "${var.regions[count.index]}"
}

# Create virtual network
resource "azurerm_virtual_network" "mynetwork" {
    name                = "myVnet-${var.name[count.index]}"
    address_space       = ["10.0.0.0/16"]
    location            = "${var.regions[count.index]}"
    resource_group_name = "${azurerm_resource_group.mygroup.name}"
}

# Create subnet
resource "azurerm_subnet" "mysubnet" {
    name                 = "mySubnet-${var.name[count.index]}"
    resource_group_name  = "${azurerm_resource_group.mygroup.name}"
    virtual_network_name = "${azurerm_virtual_network.mynetwork.name}"
    address_prefix       = "10.0.1.0/24"
}

# Create public IPs
resource "azurerm_public_ip" "mypublicip" {
    name                         = "myPublicIP"
    location                     = "${var.regions[count.index]}"
    resource_group_name          = "${azurerm_resource_group.mygroup.name}"
    allocation_method            = "Dynamic"
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "mynsg" {
    name                = "myNetworkSecurityGroup"
    location            = "${var.regions[count.index]}"
    resource_group_name = "${azurerm_resource_group.mygroup.name}"

    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 = "*"
    }
}

# Create network interface
resource "azurerm_network_interface" "mynic" {
    name                      = "myNIC"
    location                  = "${var.regions[count.index]}"
    resource_group_name       = "${azurerm_resource_group.mygroup.name}"
    network_security_group_id = "${azurerm_network_security_group.mynsg.id}"

    ip_configuration {
        name                          = "myNicConfiguration"
        subnet_id                     = "${azurerm_subnet.mysubnet.id}"
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = "${azurerm_public_ip.mypublicip.id}"
    }
}

# Create virtual machine
resource "azurerm_virtual_machine" "myvm" {
    count                 = "${length(var.regions)}"
    name                  = "${var.name[count.index]}"
    location              = "${var.regions[count.index]}"
    resource_group_name   = "${azurerm_resource_group.mygroup.name}"
    network_interface_ids = ["${azurerm_network_interface.mynic.id}"]
    vm_size               = "Standard_B1ls"

    storage_os_disk {
        name              = "myOsDisk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
    }

    storage_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "18.04.0-LTS"
        version   = "latest"
    }

     os_profile {
    computer_name  = "${var.name[count.index]}"
    admin_username = "admin"
    admin_password = ""
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }

}

在部署之前,我有望实现我的目标并验证此代码。

0 个答案:

没有答案