我是 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
}
错误:
错误:在资源“azurerm_network_interface_security_group_association”“PWSDevSecurityGroupAssoc”中的 front.tf 第 85 行缺少资源实例键: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 设置了“count”,所以必须在特定实例上访问其属性。例如,要与引用资源的索引相关联,请使用: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)] 的结果相同):
错误:在资源“azurerm_network_interface_security_group_association”“PWSDevSecurityGroupAssoc”中的 front.tf 第 85 行的非计数上下文中引用“count”:85:network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index] “count”对象只能用于“模块”、“资源”和“数据”块,并且仅当设置了“计数”参数时。
错误:在资源“azurerm_network_interface_backend_address_pool_association”“BackendIPAssoc”中的非计数上下文front.tf第91行中引用“count”:network_interface_id = azurerm_network_interface.FrontNetworkInterface[count.index]“count”对象只能在“module”中使用、“资源”和“数据”块,并且仅当设置了“计数”参数时。
此外,我在我的 azurerm_virtual_machine 块上收到此错误:
第 162 行,在资源 "azurerm_virtual_machine" "FrontEndVirtualMachines": 162: admin_ssh_key { 此处不需要“admin_ssh_key”类型的块。
我正在关注此处显示的内容:
如您所见,提供了 admin_ssh_key 块。我尝试使用脚本中使用的 2.0 版;但是,我遇到了同样的结果。
谢谢你的帮助!!:)