错误引用未声明的资源-试图使地形模块正常工作



这里是新的地形学习者,尝试通过示例和编码进行学习。我在寻找可以创建虚拟机的地形模块,在这里找到了这个例子https://faun.pub/creating-a-windows-vm-in-azure-using-terraform-which-way-is-best-13aff3ed9b74我认为这将使我的地形基础知识更上一层楼,因为创建虚拟机是我熟悉的过程,可以在此基础上进行构建。这次经历令人沮丧。这是我的第三周了,感觉还是好几英里之外。

我读了第一个解决方案,我的文件的结构与描述的完全相同。注意,我试过第一种方法,但错误百出,所以我决定尝试第二种方法,它应该更简单。

我创建了一个名为simple_vm的文件夹,在里面还有另一个文件夹vm。

内容如下。

vm_module_local_example.tf

# Create an Azure VM cluster with Terraform calling a Module. Creates 1 for Windows 10 desktop and 1 for Windows 2019 Server.
module windows_desktop_vm_using_local_module {
source              = "./vm"
resource_group_name = azurerm_resource_group.rg.name
location            = "uksouth"
sloc                = "uks"
vm_subnet_id        = module.network.vnet_subnets[0]
vm_name             = "tfdtlocmod"
vm_size             = var.desktop_vm_size
publisher           = var.desktop_vm_image_publisher
offer               = var.desktop_vm_image_offer
sku                 = var.desktop_vm_image_sku
static_ip_address   = "10.0.1.15"
activity_tag        = "Windows Desktop"
admin_password      = module.vmpassword.secretvalue
}
module windows_server_vm_using_local_module {
source              = "./vm"
resource_group_name = azurerm_resource_group.rg.name
location            = "uksouth"
sloc                = "uks"
vm_subnet_id        = module.network.vnet_subnets[1]
vm_name             = "tfsvlocmod"
vm_size             = var.server_vm_size
publisher           = var.server_vm_image_publisher
offer               = var.server_vm_image_offer
sku                 = var.server_vm_image_sku
static_ip_address   = "10.0.2.15"
activity_tag        = "Windows Server"
admin_password      = module.vmpassword.secretvalue
}

在名为VM的文件夹中。我有以下文件。

主.tf

resource "random_string" "nic_prefix" {
length  = 4
special = false
}
resource "azurerm_network_interface" "vm_nic" {
name                = "${var.vm_name}-nic1"
location            = var.location
resource_group_name = var.resource_group_name
ip_configuration {
name                          = "${var.vm_name}_nic_${random_string.nic_prefix.result}"
subnet_id                     = var.vm_subnet_id
private_ip_address_allocation = "Static"
private_ip_address            = var.static_ip_address
}
tags = var.tags
}
resource "azurerm_network_interface_security_group_association" "vm_nic_sg" {
network_interface_id      = azurerm_network_interface.vm_nic.id
network_security_group_id = var.network_security_group_id
count                     = var.network_security_group_id == "" ? 0 : 1
}
resource "azurerm_virtual_machine" "windows_vm" {
name                = var.vm_name
vm_size             = var.vm_size
location            = var.location
resource_group_name = var.resource_group_name
tags = merge(var.tags, { activityName = "${var.activity_tag} " })
network_interface_ids = [
"${azurerm_network_interface.vm_nic.id}",
]
storage_image_reference {
publisher = var.publisher
offer     = var.offer
sku       = var.sku
version   = "latest"
}
identity {
type = "SystemAssigned"
}
storage_os_disk {
name              = "${var.vm_name}-os-disk"
caching           = "ReadWrite"
create_option     = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
admin_password = module.vmpassword.secretvalue
admin_username = "azureuser"
computer_name  = var.vm_name
}
os_profile_windows_config {
provision_vm_agent = true
}
delete_os_disk_on_termination    = var.vm_os_disk_delete_flag
delete_data_disks_on_termination = var.vm_data_disk_delete_flag
}

输出.tf

output "vm_id" {
value = "${azurerm_virtual_machine.windows_vm.id}"
}
output "vm_name" {
value = "${azurerm_virtual_machine.windows_vm.name}"
}
output "vm_location" {
value = "${azurerm_virtual_machine.windows_vm.location}"
}
output "vm_resource_group_name" {
value = "${azurerm_virtual_machine.windows_vm.resource_group_name}"
}

变量.tf

variable "resource_group_name" {
}
variable "location" {
}
variable "sloc" {
}
variable "vm_size" {
default = "Standard_B1s"
}
variable "vm_subnet_id" {
}
variable "vm_name" {
}
variable "vm_os_disk_delete_flag" {
default = true
}
variable "vm_data_disk_delete_flag" {
default = true
}
variable "network_security_group_id" {
default = ""
}
variable "static_ip_address" {
}
variable "publisher" {
}
variable "offer" {
}
variable "sku" {
}
variable "tags" {
type        = map
description = "All mandatory tags to use on all assets"
default = {
activityName       = "AzureVMWindowsDemo"
automation         = "Terraform"
costCenter1        = "A00000"
dataClassification = "Demo"
managedBy          = "example@test.com"
solutionOwner      = "example@test.com"
}
}
variable "activity_tag" {
}
variable "admin_password" {
}

根据网站上的内容,我得到以下错误。

Error: Reference to undeclared resource
│
│   on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│    4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│    7: vm_subnet_id        = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 9, in module "windows_desktop_vm_using_local_module":
│    9: vm_size             = var.desktop_vm_size
│
│ An input variable with the name "desktop_vm_size" has not been declared. This variable can be declared with a variable "desktop_vm_size" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 10, in module "windows_desktop_vm_using_local_module":
│   10: publisher           = var.desktop_vm_image_publisher
│
│ An input variable with the name "desktop_vm_image_publisher" has not been declared. This variable can be declared with a variable "desktop_vm_image_publisher" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 11, in module "windows_desktop_vm_using_local_module":
│   11: offer               = var.desktop_vm_image_offer
│
│ An input variable with the name "desktop_vm_image_offer" has not been declared. This variable can be declared with a variable "desktop_vm_image_offer" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 12, in module "windows_desktop_vm_using_local_module":
│   12: sku                 = var.desktop_vm_image_sku
│
│ An input variable with the name "desktop_vm_image_sku" has not been declared. This variable can be declared with a variable "desktop_vm_image_sku" {} block.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 15, in module "windows_desktop_vm_using_local_module":
│   15: admin_password      = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│   on vm_module_local_example.tf line 19, in module "windows_server_vm_using_local_module":
│   19: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 22, in module "windows_server_vm_using_local_module":
│   22: vm_subnet_id        = module.network.vnet_subnets[1]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 24, in module "windows_server_vm_using_local_module":
│   24: vm_size             = var.server_vm_size
│
│ An input variable with the name "server_vm_size" has not been declared. This variable can be declared with a variable "server_vm_size" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 25, in module "windows_server_vm_using_local_module":
│   25: publisher           = var.server_vm_image_publisher
│
│ An input variable with the name "server_vm_image_publisher" has not been declared. This variable can be declared with a variable "server_vm_image_publisher" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 26, in module "windows_server_vm_using_local_module":
│   26: offer               = var.server_vm_image_offer
│
│ An input variable with the name "server_vm_image_offer" has not been declared. This variable can be declared with a variable "server_vm_image_offer" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│   on vm_module_local_example.tf line 27, in module "windows_server_vm_using_local_module":
│   27: sku                 = var.server_vm_image_sku
│
│ An input variable with the name "server_vm_image_sku" has not been declared. This variable can be declared with a variable "server_vm_image_sku" {} block.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 30, in module "windows_server_vm_using_local_module":
│   30: admin_password      = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.

我可以看到很多错误都和未声明的变量有关。然后我创建一个变量文件如下(不在vm文件夹中(。

variable "subscription_id" {
}
variable "client_id" {
}
variable "client_secret" {
}
variable "tenant_id" {
}
variable "global_settings" {
}
variable "desktop_vm_image_publisher" {
}
variable "desktop_vm_image_offer" {
}
variable "desktop_vm_image_sku" {
}
variable "desktop_vm_image_version" {
}
variable "desktop_vm_size" {
}
variable "server_vm_image_publisher" {
}
variable "server_vm_image_offer" {
}
variable "server_vm_image_sku" {
}
variable "server_vm_image_version" {
}
variable "server_vm_size" {
}

创建了一个名为terraform.auto.tfvars.的文件

# This file should not be checked into source control (add to .gitignore)
subscription_id = "xxxxxxxxxxxxxx"
client_id       = "xxxxxxxxxxxxxx"
client_secret   = "xxxxxxxxxxxxxx"
tenant_id       = "xxxxxxxxxxxxxx"
## globalsettings
global_settings = {
#Set of tags
tags = {
applicationName = "Windows VM Demo"
businessUnit    = "Technical Solutions"
costCenter      = "MPN Sponsorship"
DR              = "NON-DR-ENABLED"
deploymentType  = "Terraform"
environment     = "Dev"
owner           = "Jack Roper"
version         = "0.1"
}
}
# Desktop VM variables
desktop_vm_image_publisher = "MicrosoftWindowsDesktop"
desktop_vm_image_offer     = "Windows-10"
desktop_vm_image_sku       = "20h1-pro"
desktop_vm_image_version   = "latest"
desktop_vm_size            = "Standard_B1s"
# Server VM Variables
server_vm_image_publisher = "MicrosoftWindowsServer"
server_vm_image_offer     = "WindowsServer"
server_vm_image_sku       = "2019-Datacenter"
server_vm_image_version   = "latest"
server_vm_size            = "Standard_B1s"

这次运行地形计划时,我出现了以下错误。

Error: Reference to undeclared resource
│
│   on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│    4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│    7: vm_subnet_id        = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 15, in module "windows_desktop_vm_using_local_module":
│   15: admin_password      = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│   on vm_module_local_example.tf line 19, in module "windows_server_vm_using_local_module":
│   19: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 22, in module "windows_server_vm_using_local_module":
│   22: vm_subnet_id        = module.network.vnet_subnets[1]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 30, in module "windows_server_vm_using_local_module":
│   30: admin_password      = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵

现在,我在网上的例子中似乎非常不走运,在上面的例子中有很多缺失的变量,作者没有指定是否应该使用不同例子中的某些文件(链接上有很多代码显然是为了实现相同目标而使用的不同技术(。同时,我对此并不熟悉,不确定是否真的可以说发布的解决方案中缺少文件/详细信息。我没有幸联系到作者,也没有github repo来检查我是否有正确的设置,因此我在这里寻求帮助。我想做的就是提高我对地形模块的知识,我更喜欢有解释和例子,这样我也可以练习。我可以看看git-reos,但这只会给我解决方案,而没有解释或学习机会。

正如评论中的人所提到的,看起来你在遵循一个不完整或过时的教程。我想,如果我解释一下如何理解产生的错误,这样你将来就可以克服这个障碍,这可能会对你有所帮助。

Error: Reference to undeclared resource
│
│   on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│    4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵

在上面的消息中Terraform说";在文件vm_module_local_example.tf的第4行,您已经将resource_group_name设置为azurerm_resource_group.rg.name的值,但当我尝试查找资源"时;azure_resource_group";其名称为";rg";我什么也找不到。要修复它,你需要编码一个资源,比如:

resource "azurerm_resource_group" "rg" {
name     = "example"
location = "West Europe"
}

一旦完成,则第4行将把CCD_;例如";。

╷
│ Error: Reference to undeclared module
│
│   on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│    7: vm_subnet_id        = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.

这是一个很好的例子,在名为vm_module_local_example.tf的文件的第7行,有一个对module.network.vnet_subnets[0]的调用。因此Terraform将在继续操作之前查找一个名为network的模块。你需要这样的代码:

module "network" {
# pass in whatever the module needs...
}

根据错误,模块似乎应该包含一个名为vnet_subnets的资源,该资源是用count生成的,因为它依赖于一个基于零的索引(这是[0]部分(。

无论如何,希望这能有所帮助!

最新更新