首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure中的Terraform/HCL问题

Azure中的Terraform/HCL问题
EN

Stack Overflow用户
提问于 2020-09-17 02:43:13
回答 2查看 359关注 0票数 0

我刚接触HCL和Terraform,在将安全组和后端地址池关联到网络接口时遇到了问题。我在一个网络接口块中创建了两个网络接口:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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.FrontNetworkInterface设置了“front.tf”,因此必须在特定实例上访问其属性,因此在资源计数"PWSDevSecurityGroupAssoc":85 : network_interface_id =count中的第85行缺少资源实例键。例如,要与引用资源的索引关联,请使用: azurerm_network_interface.FrontNetworkInterfacecount.index

错误:由于azurerm_network_interface.FrontNetworkInterface设置了“front.tf”,因此必须在特定实例上访问其属性,因此在资源计数"BackendIPAssoc":91 : network_interface_id =count中的第91行缺少资源实例键。例如,要与引用资源的索引关联,请使用: azurerm_network_interface.FrontNetworkInterfacecount.index

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

代码语言:javascript
复制
#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”“:85: network_interface_id = azurerm_network_interface.FrontNetworkInterfacecount.index的front.tf行的非计数上下文中对"count”的引用"count“对象只能在”“azurerm_network_interface_security_group_association”“、" resource”和"data“块中使用,并且只有在设置了"count”参数时才能使用。

错误:在资源模块“"azurerm_network_interface_backend_address_pool_association”“中的非计数上下文计数行91中对"count”的引用: network_interface_id = azurerm_network_interface.FrontNetworkInterfacecount.index "count“对象只能在”front.tf“、”BackendIPAssoc“和"data”块中使用,并且只有在设置了"count“参数时才能使用。

另外,我在我的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版;但是,我遇到了相同的结果。

感谢您的帮助!!:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-17 19:31:51

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

代码语言:javascript
复制
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
}
票数 0
EN

Stack Overflow用户

发布于 2020-09-17 04:50:20

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

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

代码语言:javascript
复制
count = len(azurerm_network_interface.FrontNetworkInterface)

以确保以后更改2时不会出现不匹配的数字。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63926172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档