Error: Reference to undeclared module on modules/site_recovery/main.tf

Error: Reference to undeclared module on modules/site_recovery/main.tf

我已经在 terraform 中创建了资源、网络和计算模块,现在想将 vm_id 的输出传递给站点恢复模块,这里是我当前正在使用的文件。

继续主题:in resource "azurerm_site_recovery_replicated_vm" "vm-replication": source_vm_id= module.compute.vm_id

这是我目前正在关注的目录结构,

.
├── main.tf
└── modules
    ├── compute
    │   ├── main.tf
    │   ├── outputs.tf_bk
    │   ├── variable.tf
    │   └── variable.tfvars
    ├── network
    │   ├── main.tf
    │   ├── variable.tf
    │   └── variable.tfvars
    ├── resource
    │   ├── main.tf
    │   ├── variable.tf
    │   └── variable.tfvars
    └── site_recovery
        ├── main.tf
        ├── variable.tf
        └── variable.tfvars

根模块main.cf文件:

#Select provider
provider "azurerm" {
  subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxx"
  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 = "module.resource.primary_group_name"
}

module "compute" {
  source = "./modules/compute"
  #resource_group_name = "devops_primary"
  #location = "southeastasia"
  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"
  sa_name = "primarysa"
  disk_name = "backenddisk_primary"
}

module "site_recovery" {
  source = "./modules/site_recovery"
  #resource_group_name = "devops_primary"
  #location = "southeastasia"
  sec_resource_group = "devops_secondary"
  recovery_vault_name = "recovery-vault"
  primary_fabric = "devops_primary-fabric"
  seconday_fabric = "devops_secondary-fabric"
  primary_container = "primary-protection-container"
  secondary_container = "secondary-protection-container"
  policy_name = "policy"
  container_mapping = "container-mapping"
  replicated_vm = "backendvm-replication"
}

计算 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"
    }
output "vm_ids" {
  description = "Virtual machine ids created."
  value       = azurerm_virtual_machine.primary.id
  #depends_on = [azurerm_virtual_machine.primary.primary]
}

站点恢复main.cf

#Create Site Recovery Replicated VM
resource "azurerm_site_recovery_replicated_vm" "vm-replication" {
  name                                      = var.replicated_vm
  resource_group_name                       = azurerm_resource_group.secondary.name
  recovery_vault_name                       = azurerm_recovery_services_vault.vault.name
  source_recovery_fabric_name               = azurerm_site_recovery_fabric.primary.name
  #source_vm_id                              = site recovery main.cf

#Create Site Recovery Replicated VM
resource "azurerm_site_recovery_replicated_vm" "vm-replication" {
  name                                      = var.replicated_vm
  resource_group_name                       = azurerm_resource_group.secondary.name
  recovery_vault_name                       = azurerm_recovery_services_vault.vault.name
  source_recovery_fabric_name               = azurerm_site_recovery_fabric.primary.name
  #source_vm_id                              = "module.compute.azurerm_virtual_machine.primary.id"
  source_vm_id                              = module.compute.vm_ids
  recovery_replication_policy_id            = azurerm_site_recovery_replication_policy.policy.id
  source_recovery_protection_container_name = azurerm_site_recovery_protection_container.primary.name
  target_resource_group_id                  = azurerm_resource_group.secondary.id
  target_recovery_fabric_id                 = azurerm_site_recovery_fabric.secondary.id
  target_recovery_protection_container_id   = azurerm_site_recovery_protection_container.secondary.id


  managed_disk {
    disk_id                    = "[module.resource.azurerm_virtual_machine.primary.storage_os_disk[0].managed_disk_id]"
    staging_storage_account_id = "module.resource.azurerm_storage_account.primary.id"
    target_resource_group_id   = azurerm_resource_group.secondary.id
    target_disk_type           = "Premium_LRS"
    target_replica_disk_type   = "Premium_LRS"
  }

  managed_disk {
    disk_id                    = "[module.resource.azurerm_managed_disk.primary.id]"
    staging_storage_account_id = "[module.resource.azurerm_storage_account.primary.id]"
    target_resource_group_id   = azurerm_resource_group.secondary.id
    target_disk_type           = "Premium_LRS"
    target_replica_disk_type   = "Premium_LRS"
  }
depends_on = ["module.compute.vm_ids"]
}

使用 depends_on 作为 site_recovery 模块的输入,请再次提出建议,我如何从计算模块输出托管磁盘 ID 和 Os 磁盘 ID,并在站点中使用输入恢复模块。

对于错误

Error: Reference to undeclared module on modules/site_recovery/main.tf

这意味着引用的模块没有在调用模块中声明。

调用模块意味着将该模块的内容包含到配置中,并为其输入变量指定特定值。使用 module 块从其他模块中调用模块。您需要在要调用该模块的配置 .tf 文件中添加 module 块。参见 calling a child module

你的子站点恢复和计算main.tf好像没有声明模块块,所以你不能调用module.resource.azurerm_resource_group.primary.locationmodule.resource.azurerm_managed_disk.primary.id等资源模块上。

作为您的目录结构,您还可以使用输入变量从另一个模块输出调用模块。正确的表达是module.<MODULE NAME>.<OUTPUT NAME>

要像这样从计算模块输出 VM id 和托管磁盘 id:

output "azurerm_vm_id" {
  value = azurerm_virtual_machine.primary.id
}


output "primary_os_disk_id" {
  value = azurerm_virtual_machine.primary.storage_os_disk[0].managed_disk_id
}

根目录下的main.tf

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"
  nic_ids = module.network.primary_nic_id
  resource_group_name = module.resource.rg_name
  location = module.resource.rg_location
  #ssh_key_path = "/home/terraform/.ssh/authorized_keys"
  #keys_data = "~/.ssh/id_rsa.pub"
}


module "site_recovery" {
  source = "./modules/site_recovery"
  resource_group_name = module.resource.rg_name
  location = module.resource.rg_location
  sec_resource_group = "nancy_secondary"
  sec_location = "eastus"
  recovery_vault_name = "recovery-vault"
  primary_fabric = "devops_primary-fabric"
  seconday_fabric = "devops_secondary-fabric"
  primary_container = "primary-protection-container"
  secondary_container = "secondary-protection-container"
  policy_name = "policy"
  container_mapping = "container-mapping"
  replicated_vm = "backendvm-replication"

  source_vm_id  = module.vm.azurerm_vm_id
  primary_os_disk_id = module.vm.primary_os_disk_id

}

Site Recovery main.tf 文件

#Create Site Recovery Replicated VM
resource "azurerm_site_recovery_replicated_vm" "vm-replication" {
  depends_on                                = [var.vm_depends_on]
  name                                      = var.replicated_vm
  resource_group_name                       = azurerm_resource_group.secondary.name
  recovery_vault_name                       = azurerm_recovery_services_vault.vault.name
  source_recovery_fabric_name               = azurerm_site_recovery_fabric.primary.name

  source_vm_id                              = var.source_vm_id
  recovery_replication_policy_id            = azurerm_site_recovery_replication_policy.policy.id
  source_recovery_protection_container_name = azurerm_site_recovery_protection_container.primary.name
  target_resource_group_id                  = azurerm_resource_group.secondary.id
  target_recovery_fabric_id                 = azurerm_site_recovery_fabric.secondary.id
  target_recovery_protection_container_id   = azurerm_site_recovery_protection_container.secondary.id


  managed_disk {
    disk_id                    = var.primary_os_disk_id
    staging_storage_account_id = azurerm_storage_account.primary.id
    target_resource_group_id   = azurerm_resource_group.secondary.id
    target_disk_type           = "Premium_LRS"
    target_replica_disk_type   = "Premium_LRS"
  }

}

实际上,在azurerm_site_recovery_replicated_vm块中,有一个隐式依赖项source_vm_id,它在源Azure VM上回复。如果你想使用 terraform depends_on 元参数接受带有模块的资源列表。你可以参考这个线程 - and this document.