azure - Azure 中的 Terraform/HCL 问题
问题描述
我是 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 版;但是,我遇到了同样的结果。
谢谢你的帮助!!:)
解决方案
引用使用 count 创建的资源时,您仍然需要添加 .id。请参阅以下示例。有关详细信息,请参阅此链接。
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
}
推荐阅读
- java - 是否可以使用 Java JRE 11 运行 Java 小程序?
- android - 如何在 MPAndroidChart 中绘制单元格
- java - 在可能的情况下,是否应该消除 SQL 查询以支持使用 Hibernate?
- php - 在codeigniter中上传多个文件并使用数组重命名每个文件
- android - AndroidX - 程序类型已经存在:androidx.annotation.BoolRes
- javascript - 等待所有元素完全使用 Promise.all
- mysql - 使用中间表用相关表的总和值更新一个表的值
- google-cloud-platform - 无法针对 BigQuery 运行查询 - 权限错误 403
- android - 画廊而不是Android中的相机活动(Tensorflow)
- roku - Roku API 获取响应方法不允许