Terraform/HCL 在 Azure 问题中

Terraform/HCL in Azure issues

我是 HCL 和 Terraform 的新手,在将安全组和后端地址池关联到网络接口时遇到问题。我在单个网络接口块中创建 2 个网络接口:

#Create network interface for 2 VMs
resource "azurerm_network_interface" "FrontNetworkInterface" {
    count = 2
    name = "niFront${count.index}"
    location = azurerm_resource_group.PWSDevResourceGroup.location
    resource_group_name = azurerm_resource_group.PWSDevResourceGroup.name

    ip_configuration {
        name = "ipconfFrontVM"
        subnet_id = azurerm_subnet.PWSDevSubnet.id
        private_ip_address_allocation = "dynamic"
    }
}

我尝试过以各种方式进行关联,但都产生了不同的错误:

尝试 1:

#Connect security group to the network interface
resource "azurerm_network_interface_security_group_association" "PWSDevSecurityGroupAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface.id
    network_security_group_id = azurerm_network_security_group.PWSDevSecurityGroup.id
}

#Connect 2 backend ips to the load balancer
resource "azurerm_network_interface_backend_address_pool_association" "BackendIPAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface.id
    ip_configuration_name = "bipa"
    backend_address_pool_id = azurerm_lb_backend_address_pool.BackendIpPool.id
}

错误:

错误:缺少资源实例密钥 在 front.tf 第 85 行,资源“azurerm_network_interface_security_group_association”“PWSDevSecurityGroupAssoc”中: 85: network_interface_id = azurerm_network_interface.FrontNetworkInterface.id 因为 azurerm_network_interface.FrontNetworkInterface 设置了“计数”,所以它 必须在特定实例上访问属性。 例如,要与引用资源的索引相关联,请使用: azurerm_network_interface.FrontNetworkInterface[count.index]

错误:缺少资源实例密钥 在 front.tf 第 91 行,资源“azurerm_network_interface_backend_address_pool_association”“BackendIPAssoc”中: 91: network_interface_id = azurerm_network_interface.FrontNetworkInterface.id 因为 azurerm_network_interface.FrontNetworkInterface 设置了“计数”,所以它 必须在特定实例上访问属性。 例如,要与引用资源的索引相关联,请使用: azurerm_network_interface.FrontNetworkInterface[count.index]

ATTEMPT 2/3/4(使用“[count.index]”、“[count.index].id”或“[element(azurerm_network_interface.FrontNetworkInterface.*.id, count.index)]" 如前一个错误所述):

#Connect security group to the network interface
resource "azurerm_network_interface_security_group_association" "PWSDevSecurityGroupAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index]
    network_security_group_id = azurerm_network_security_group.PWSDevSecurityGroup.id
}

#Connect 2 backend ips to the load balancer
resource "azurerm_network_interface_backend_address_pool_association" "BackendIPAssoc" {
    network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index]
    ip_configuration_name = "bipa"
    backend_address_pool_id = azurerm_lb_backend_address_pool.BackendIpPool.id
}

错误([count.index].id 和 [element(azurerm_network_interface.FrontNetworkInterface.*.id, count.index)] 的相同结果):

错误:在非计数上下文中引用“计数” 在 front.tf 第 85 行,资源“azurerm_network_interface_security_group_association”“PWSDevSecurityGroupAssoc”中: 85:network_interface_id=azurerm_network_interface.FrontNetworkInterface[count.index] “计数”对象只能在“模块”、“资源”和“数据”中使用 块,并且仅当设置了“计数”参数时。

错误:在非计数上下文中引用“计数” front.tf 第 91 行,在资源“azurerm_network_interface_backend_address_pool_association”“BackendIPAssoc”中: network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index] “计数”对象只能在“模块”、“资源”和“数据”中使用 块,并且仅当设置了“计数”参数时。

此外,我在 azurerm_virtual_machine 块上收到此错误:

第 162 行,在资源“azurerm_virtual_machine”“FrontEndVirtualMachines”中: 162:admin_ssh_key{ 此处不应包含“admin_ssh_key”类型的块。

我正在关注此处显示的内容:

https://docs.microsoft.com/en-us/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure

如您所见,提供了 admin_ssh_key 块。我尝试使用脚本中使用的 2.0 版;然而,我遇到了同样的结果。

感谢您的帮助!! :)

我承认我没有读完整个故事,但看起来您的尝试 #2/3/4 非常接近。在使用[count.index]的地方,需要指定一个count,否则索引就没有计数了。因此,如果您只是将 count = 2 添加到这两个资源块,它应该可以工作。

更好的是,要么将 2 作为变量,要么使用

count = len(azurerm_network_interface.FrontNetworkInterface)

以确保您以后更改 2 时不会得到不匹配的数字。

引用使用 count 创建的资源时,您仍然需要添加 .id。请参见以下示例。有关详细信息,请参阅此 link

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

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_virtual_network" "example" {
  name                = "vnet"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
  dns_servers         = ["10.0.0.4", "10.0.0.5"]
}

resource "azurerm_subnet" "example" {
  name                 = "example"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "example" {
  count               = 2
  name                = format("int%s", count.index)
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "ip"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "dynamic"
  }
}

resource "azurerm_network_security_group" "example" {
  name                = "acceptanceTestSecurityGroup1"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  security_rule {
    name                       = "test123"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "*"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

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