Azure中的Terraform/HCL问题
我是HCL和Terraform的新手,在将安全组和后端地址池关联到网络接口时遇到问题。我正在单个网络接口块中创建2个网络接口:Azure中的Terraform/HCL问题,azure,azure-devops,terraform,terraform-provider-azure,hcl,Azure,Azure Devops,Terraform,Terraform Provider Azure,Hcl,我是HCL和Terraform的新手,在将安全组和后端地址池关联到网络接口时遇到问题。我正在单个网络接口块中创建2个网络接口: #Create network interface for 2 VMs resource "azurerm_network_interface" "FrontNetworkInterface" { count = 2 name = "niFront${count.index}" loca
#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_网络_接口_安全性_组_关联”“PWSDevSecurityGroupAssoc”中:
85:network\u interface\u id=azurerm\u network\u interface.FrontNetworkInterface.id
由于azurerm_network_interface.FrontNetworkInterface已设置“计数”,因此其
必须在特定实例上访问属性。
例如,要与引用资源的索引关联,请使用:
azurerm_网络接口.FrontNetworkInterface[计数.索引]
错误:缺少资源实例密钥
在front.tf第91行的资源“azurerm_网络_接口_后端_地址_池_关联”“BackendIPAssoc”中:
91:network\u interface\u id=azurerm\u network\u interface.FrontNetworkInterface.id
由于azurerm_network_interface.FrontNetworkInterface已设置“计数”,因此其
必须在特定实例上访问属性。
例如,要与引用资源的索引关联,请使用:
azurerm_网络接口.FrontNetworkInterface[计数.索引]
尝试2/3/4(使用“[count.index]”、“[count.index].id”或“[element(azurerm_network_interface.FrontNetworkInterface.*.id,count.index]”,如前一个错误中所述):
错误(与[count.index].id和[element(azurerm_网络接口.FrontNetworkInterface.*.id,count.index)]的结果相同):
错误:在非计数上下文中引用“计数”
在front.tf第85行的资源“azurerm_网络_接口_安全性_组_关联”“PWSDevSecurityGroupAssoc”中:
85:network\u interface\u id=azurerm\u network\u interface.FrontNetworkInterface[count.index]
“计数”对象只能在“模块”、“资源”和“数据”中使用
块,并且仅当设置了“count”参数时
错误:在非计数上下文中引用“计数”
front.tf第91行,在资源“azurerm\u网络\u接口\u后端\u地址\u池\u关联”“BackendIPAssoc”中:
network\u interface\u id=azurerm\u network\u interface.FrontNetworkInterface[count.index]
“计数”对象只能在“模块”、“资源”和“数据”中使用
块,并且仅当设置了“count”参数时
此外,我在azurerm_虚拟机块上收到此错误:
第162行,在资源“azurerm_虚拟机”“前端虚拟机”中:
162:管理ssh密钥{
此处不需要“admin\u ssh\u key”类型的块
以下是我在这里展示的内容:
如您所见,提供了admin_ssh_密钥块。我尝试使用脚本中使用的版本2.0;但是,我遇到了相同的结果
谢谢你的帮助!!:)我承认我还没有读完整的故事,但是看起来你的尝试#2/3/4非常接近。你在哪里使用
[count.index]
,您需要指定一个计数
,否则就没有要索引的计数。因此,如果您只是将count=2
添加到这两个资源块中,它应该可以工作
更好的方法是将2
作为变量,或者使用
count = len(azurerm_network_interface.FrontNetworkInterface)
为确保以后更改
2
时不会出现不匹配的数字。引用使用count创建的资源时,仍然需要添加.id。请参见以下示例。有关更多信息,请参见此
非常感谢!非常明显;但是,我的眼睛没有看到!:)关于azurerm_虚拟机块中的第二个问题有什么想法:第162行,在参考资料“azurerm_虚拟机”“FrontendVirtualMachine”:162:admin_ssh_key{类型为“admin_ssh_key”的块这里不需要。我遵循这里显示的内容:如您所见,提供了admin_ssh_密钥块。我尝试使用脚本中使用的版本2.0;但是,我经历了相同的结果。打开一个单独的问题非常感谢!非常明显;但是,我的眼睛没有看到它!:)对azurer中的第二个问题有什么想法吗m_虚拟机块:第162行,在资源“azurerm_虚拟机”“FrontEndVirtualMachine”中:162:admin_ssh_密钥{类型为“admin_ssh_密钥”的块这里不需要。我遵循这里显示的内容:如您所见,提供了admin_ssh_密钥块。我尝试使用脚本中使用的版本2.0;但是,我遇到了相同的结果。
count = len(azurerm_network_interface.FrontNetworkInterface)
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
}