无法将 network_interface_id 解析为资源 id:无法解析 AzureID:解析 module.network.azurerm_network_interface.primary.i

Posted

技术标签:

【中文标题】无法将 network_interface_id 解析为资源 id:无法解析 AzureID:解析 module.network.azurerm_network_interface.primary.id: invalidURIforrequest【英文标题】:Can't parse network_interface_id as a resource id:Cannot parse AzureID:parse module.network.azurerm_network_interface.primary.id: invalidURIforrequest 【发布时间】:2020-07-19 14:00:31 【问题描述】:
on modules/security_group/main.tf line 64, in resource "azurerm_network_interface_security_group_association" "primary":
      64:   resource "azurerm_network_interface_security_group_association" "primary" 

我使用“terraform validate”命令获得了以上输出 以下是我用于 terraform 的配置。 这是我作为模块工作的树

├── main.tf
    └── modules
        ├── network
        │   ├── main.tf
        │   ├── variable.tf
        │   └── variable.tfvars
        ├── resource
        │   ├── main.tf
        │   ├── variable.tf
        │   └── variable.tfvars
        ├── security_group
        │   ├── main.tf
        │   ├── variable.tf
        │   └── variable.tfvars
        ├── storage
        │   ├── main.tf
        │   ├── variable.tf
        │   └── variable.tfvars
        └── vm
            ├── main.tf
            ├── variable.tf
            └── variable.tfvars

main.cf:

#Select provider
    provider "azurerm" 
      subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxx"
      version = "~> 2.4"
      features 
    
  module "resource" 
      source = "./modules/resource"
      resource_group_name = "devops_primary"
      location = "southeastasia"
    
   module "network" 
      source = "./modules/network"
      virtual_network = "primaryvnet"
      subnet = "primarysubnet"
      address_space = "192.168.0.0/16"
      address_prefix = "192.168.1.0/24"
      public_ip = "backendvmpip"
      location = "southeastasia"
      primary_nic = "backendvmnic"
      primary_ip_conf = "backendvm"
      resource_group_name = "devops_primary"
    
    module "vm" 
      source = "./modules/vm"
      vm_name = "backendvm-primary"
      vm_size = "standard_d2s_v3"
      vm_storage_od_disk_name = "backend-vm-os-disk-primary"
      computer_name = "backendserver"
      username = "terraform"
      ssh_key_path = "/home/terraform/.ssh/authorized_keys"
      keys_data = "~/.ssh/id_rsa.pub"
    
    module "security_group" 
       source = "./modules/security_group"
       sg_group_name = "primary_sg"
       primary_nic_id = ["module.network.primary_nic_id"] 
    

这里是资源的 main.cf 文件:

#Select provider
provider "azurerm" 
  subscription_id = "xxxxxxxxxxxxxxxxxxxxxx"
  version = "~> 2.2"
  features 


#Create Primary Resource Group
resource "azurerm_resource_group" "primary" 
  name     = "var.resource_group_name"
  location = "var.location"
  tags = 
        environment = "Test"
    

output "devops_primary" 
  value = "$azurerm_resource_group.primary.name"

output "location" 
    value = "$azurerm_resource_group.primary.location"

这是网络的 main.cf 文件:

#Create public IP address
resource "azurerm_public_ip" "primary" 
    name                         = "var.public_ip"
    location                     = "module.resource.azurerm_resource_group.primary.location"
    resource_group_name          = "module.resource.azurerm_resource_group.primary.name"
    allocation_method            = "Dynamic"
    tags = 
        environment = "Test"
    

output "public_ip_id"
  value = azurerm_public_ip.primary.id
  
#Create Network Interface
resource "azurerm_network_interface" "primary" 
  name                = "var.primary_nic"
  location            = "module.resource.azurerm_resource_group.primary.location"
  resource_group_name = "module.resource.azurerm_resource_group.primary.name"
   resource_group_name = var.resource_group_name
  ip_configuration 
        name                           = "var.primary_ip_conf"
       #subnet_id                       = "$azurerm_subnet.primary.id"
        subnet_id                       = azurerm_subnet.primary.id
        private_ip_address_allocation  = "Dynamic"
        #public_ip_address_id            = "$azurerm_public_ip.primary.id"
        public_ip_address_id           = azurerm_public_ip.primary.id
        #public_ip_address_allocation   = "Dymanic"
    
  tags = 
        environment = "Test"
    
  # depends_on = [var.subnet_id_primary]
    #depends_on                     = [module.resource.azurerm_resource_group.name]

output "primary_nic_id"
  description = "Primary VNET NIC Id "
  value = ["azurerm_network_interface.primary.id"]
  
output "private_ip" 
  description = "private ip addresses of the vm nics"
  value       = "$azurerm_network_interface.primary.private_ip_address"

这是 VM 的 main.cf 文件:

#Create VM in Primary resource
resource "azurerm_virtual_machine" "primary" 
  name                  = "var.vm_name"
  location              = "module.resource.azurerm_resource_group.primary.location"
  resource_group_name   = "module.resource.azurerm_resource_group.primary.name"
  vm_size               = "var.vm_size"
  network_interface_ids = ["module.resource.azurerm_network_interface.primary.id"]
  storage_os_disk 
    name              = "var.vm_storage_od_disk_name"
    os_type           = "Linux"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  
storage_image_reference 
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  
  os_profile 
    computer_name  = "var.computer_name"
    admin_username = "var.username"
  
  os_profile_linux_config 
    disable_password_authentication = true
  ssh_keys 
      path     = "/home/terraform/.ssh/authorized_keys"
      key_data = file("~/.ssh/id_rsa.pub")
    
  
  tags = 
        environment = "Test"
    

这是 security_group 的 main.cf 文件:

#Create Network Security Group
resource "azurerm_network_security_group" "primary" 
    name                = "var.sg_group_name"
    #location            = "module.resource.azurerm_resource_group.primary.location"
    #resource_group_name = "module.resource.azurerm_resource_group.primary.name"
     resource_group_name = "var.resource_group_name"
     location            = "var.location"
    #Security Rules for Security Group
    security_rule 
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    
    security_rule 
        name                       = "AppOut"
        priority                   = 1002
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "8040"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    
    security_rule 
        name                       = "mysql"
        priority                   = 1003
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "3306"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    
    security_rule 
        name                       = "Redis"
        priority                   = 1004
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "6379"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    
    tags = 
        environment = "Test"
    

variable "primary_nic_id" 
# Connect the security group to the network interface
  resource "azurerm_network_interface_security_group_association" "primary" 
    #network_interface_id      = "$module.network.azurerm_network_interface.primary.id"
    network_interface_id      = "module.network.azurerm_network_interface.primary.id"
    network_security_group_id = "$azurerm_network_security_group.primary.id"
    #depends_on                = ["module.network.primary_nic_id"]
    #primary_nic_id               = ["var.primary_nic_id"]

#depends_on                = [module.network.primary_nic_id]
# Generate a new ID only when a new resource group is defined
 resource "random_id" "randomId" 
    keepers = 
        resource_group_name = "module.resource.azurerm_resource_group.primary.name"
    
    byte_length = 8

请查看我目前正在使用的代码,我是 terraform 新手,刚开始还是个学习者。

【问题讨论】:

去掉module.network.azurerm_network_interface.primary.id周围的引号 如何使用depends_on从网络模块输出到安全组模块的输入?? 【参考方案1】:

首先,您可以删除所有非常量表达式的引号并保留内部的内部表达式。要开始升级您的配置,请运行 terraform 0.12upgrade 命令。

Terraform 0.11 及更早版本要求所有非常量表达式 通过插值语法提供,但现在已弃用此模式。 要消除此警告,请从开头删除 "$ 序列并 这个表达式末尾的 " 序列,只留下 内心的表达。

模板插值语法仍然用于从 模板包含多个插值时的表达式 序列或文字字符串和插值的混合。这 弃用仅适用于完全由 单插值序列。

Calling a Child Module

调用模块意味着将该模块的内容包含到 输入变量具有特定值的配置。模块 使用 module 块从其他模块中调用:

module "servers" 
  source = "./app-cluster"

  servers = 5

Accessing Module Output Values

模块中定义的资源被封装,所以调用 模块不能直接访问它们的属性。然而,孩子 模块可以声明输出值以选择性地导出某些值 被调用模块访问。

例如,如果 ./app-cluster 模块导出了一个名为 output 的值 instance_ids 然后调用 模块可以使用表达式引用该结果 module.servers.instance_ids:

resource "aws_elb" "example" 
  # ...

  instances = module.servers.instance_ids

有关引用命名值的更多信息,请参阅Expressions。

例如,在这种情况下,您不能将模块中的值作为代码查询

network_interface_id      = "module.network.azurerm_network_interface.primary.id"

正确的表达式是module.<MODULE NAME>.<OUTPUT NAME>。它是当前模块调用的子模块中指定的output 值的值。您应该像 network_interface_id = module.network.primary_nic_id 一样从 network 模块中查询 azurerm_network_interface.primary.id

此外,由于模块块是在代码的根目录中声明的,因此您不能直接从子模块配置文件中引用它们。您可以使用输入变量将值从根模块传递给您的子模块。见output values。

例如,在根目录下的模块network中,你从模块network中调用模块resource输出devops_primary这样resource_group_name = module.resource.devops_primary

module "network" 
  source = "./modules/network"
  resource_group_name = module.resource.devops_primary
  location = module.resource.location
  virtual_network = "primaryvnet"
  subnet = "primarysubnet"
  address_space = ["192.168.0.0/16"]
  ...

在 ./modules.network 目录中,你有

#Create Virtual Network in Primary Resource Group
resource "azurerm_virtual_network" "primary" 
  name                = var.virtual_network
  resource_group_name = var.resource_group_name
  address_space       = var.address_space
  location            = var.location



variable "resource_group_name" 



variable "location" 


您可以按照上述规则重新编辑您的配置文件。更多示例,您可以搜索azurerm modules。

【讨论】:

以上是关于无法将 network_interface_id 解析为资源 id:无法解析 AzureID:解析 module.network.azurerm_network_interface.primary.i的主要内容,如果未能解决你的问题,请参考以下文章

为啥我无法正确获取图像数据或无法将数据发送到服务器?

无法将 createdAt 和 updatedAt 保存为日期时间值,也无法将后端保存为前端

C# 无法将类型为“System.Byte[]”的对象强制转换为类型“System.Data.DataTable

无法将类型为“System.Collections.Generic.List`1[EPMS.Domain.SingleItem]”的对象强制转换为类型“EPMS

无法将 .json 文件从 CSV 下载到 JSON 转换并且无法将 JSON 转换为 CSV

无法将 ReactiveUI 添加到 NUnit 测试项目