gpt4 book ai didi

azure-virtual-machine - Terraform 报告 'No module called "模块名称“在 "root"模块中声明”

转载 作者:行者123 更新时间:2023-12-04 00:23:03 28 4
gpt4 key购买 nike

我已经进行了探索,我想坚持自定义模块概念,而不是使用 Azure 公共(public)模块注册表。

源代码位置在这里

https://github.com/ameyaagashe/help_me_cross_2

我使用以下参数在命令行上运行 terraform:
terraform plan -var "resource_group_name=nxt-grp-prd-manage-rgp-au-se" -var "virtual_network_name=virtual_network_1" -var "sql_subnet_name=subnet_1" -var "app_subnet_name=subnet_2" -var "application_nsg=test_nsg" -var "count_vm=2" -var "sql_host_basename=sqlvms" -var "app_host_basename=appvms" -var "storage_account_suffix=sta" -var "virtual_machine_size=Standard_B1ms" -var "virtual_machine_image_publisher=MicrosoftWindowsServer" -var "virtual_machine_image_offer=WindowsServer" -var "virtual_machine_image_sku=2012-R2-Datacenter" -var "virtual_machine_image_version=latest" -var "username=devopsadmin" -var "password=Angular12#$%"

但是,我收到如下错误:
Error: Reference to undeclared module

on ../../modules/compute/main.tf line 25, in resource "azurerm_virtual_machine" "tf-vm":
25: location = module.resourcegroup.external_rg_location

No module call named "resourcegroup" is declared in sql_vms.


Error: Reference to undeclared module

on ../../modules/compute/main.tf line 26, in resource "azurerm_virtual_machine" "tf-vm":
26: resource_group_name = module.resourcegroup.external_rg_name

No module call named "resourcegroup" is declared in sql_vms.


Error: Reference to undeclared module

on ../../modules/compute/main.tf line 27, in resource "azurerm_virtual_machine" "tf-vm":
27: network_interface_ids = [element(module.network.network_interface_ids,count.index)]

No module call named "network" is declared in sql_vms.


Error: Reference to undeclared module

on ../../modules/network/data.tf line 5, in data "azurerm_virtual_network" "tf-vn":
5: resource_group_name = module.resource_groups.external_rg_name

No module call named "resource_groups" is declared in networking.


Error: Reference to undeclared module

on ../../modules/nsg/main.tf line 3, in resource "azurerm_network_security_group" "tf-nsg":
3: location = module.resourcegroup.external_rg_location

No module call named "resourcegroup" is declared in network_security_group.


Error: Reference to undeclared module

on ../../modules/nsg/main.tf line 4, in resource "azurerm_network_security_group" "tf-nsg":
4: resource_group_name = module.resourcegroup.external_rg_name

No module call named "resourcegroup" is declared in network_security_group.


Error: Reference to undeclared input variable

on ../../modules/resourcegroup/data.tf line 2, in data "azurerm_resource_group" "tf-rg-external":
2: name = var.rg_name

An input variable with the name "rg_name" has not been declared. This variable
can be declared with a variable "rg_name" {} block.

无法理解根本问题...

我在他们自己的 terraform 配置文件中定义所有模块并在根模块中调用它们?

我衷心感谢您的帮助。

最佳答案

从长远来看,单独解决每个问题可能对您没有帮助,因此我将提供一个一般性的错误认识,并分享引用资料以帮助 future 的 Terraform 配置。

引用 module.resource_group内部输出 module.sql_vms不起作用,因为 Terraform 模块组合不允许兄弟模块直接相互引用输出。

文档中有一段解释它:

  • https://www.terraform.io/docs/modules/composition.html

  • 输出只能通过根模块从一个模块传递到另一个模块。

    以下是 Terraform 文档中的示例:

    module "network" {
    source = "./modules/aws-network"

    base_cidr_block = "10.0.0.0/8"
    }

    module "consul_cluster" {
    source = "./modules/aws-consul-cluster"

    vpc_id = module.network.vpc_id
    subnet_ids = module.network.subnet_ids
    }

    一旦解决了这个问题,下一个问题就是模块没有被正确的名称引用。根据 main.tf在 git repo 中,已经声明了 4 个模块:
    module.sql_vms
    module.resource_group
    module.networking
    module.network_security_group

    由于模块被声明为 resource_group , 它应该被引用而不是 resourcegroup .

    最后,一个名为 rg_name 的变量应该在 modules/resourcegroup/ 中声明目录。请参阅以下内容以了解变量声明:
  • https://www.terraform.io/docs/configuration/variables.html

  • 这可能不是所有问题,但它是一个开始。如果可能,我强烈建议您阅读有关创建模块的 Terraform 文档。它们可以在这里找到:
  • https://www.terraform.io/docs/modules/index.html
  • 关于azure-virtual-machine - Terraform 报告 'No module called "模块名称“在 "root"模块中声明”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59132282/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com