gpt4 book ai didi

azure - 托管资源 "azurerm_network_interface" "myterraformnic"尚未在 module.vm 中声明。 azure

转载 作者:行者123 更新时间:2023-12-03 05:27:57 25 4
gpt4 key购买 nike

我创建了两个模块:

  1. 网络 2. 虚拟机在network.tf中,我添加了azure的网络配置,在vm.tf中,我尝试创建虚拟机。从 main.tf 调用这两个模块时出现此错误:

    在 ..\modules\vm\vm.tf 第 20 行,资源“azurerm_windows_virtual_machine”“myterraformvm”中:20:network_interface_ids = [azurerm_network_interface.myterraformnic.id]

    托管资源“azurerm_network_interface”“myterraformnic”尚未在 module.vm 中声明。

这是vm.tf的代码

    # Resource Group
resource "azurerm_resource_group" "myterraformgroup" {
name = "myResourceGroup"
location = "eastus"
}
resource "azurerm_image" "example" {
# (resource arguments)

name = "WINDOWS2019C"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
}

# Virtual Machine Properties

resource "azurerm_windows_virtual_machine" "myterraformvm" {
name = "myVM"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
network_interface_ids = [azurerm_network_interface.myterraformnic.id]
size = "Standard_DS1_v2"
admin_username = "XXXXXXXXXXXXX"
admin_password = "XXXXXXXXXXXXXX"

os_disk {
name = "myOsDisk"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_id = "/subscriptions/XXXXXXXXXXXXXXXXXXX/soft.Compute/images/XXXXXXXX"
}

这是network.tf的代码

 #Create virtual network
resource "azurerm_resource_group" "myterraformgroup" {
name = "myResourceGroup"
location = "eastus"
}
resource "azurerm_virtual_network" "myterraformnetwork" {
name = "myVnet"
address_space = ["10.0.0.0/16"]
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name

tags = {
environment = "Terraform Demo"
}
}

# Create subnet
resource "azurerm_subnet" "myterraformsubnet" {
name = "mySubnet"
resource_group_name = azurerm_resource_group.myterraformgroup.name
virtual_network_name = azurerm_virtual_network.myterraformnetwork.name
address_prefixes = ["10.0.1.0/24"]
}

# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
name = "myPublicIP"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
allocation_method = "Dynamic"

tags = {
environment = "Terraform Demo"
}
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "myterraformnsg" {
name = "myNetworkSecurityGroup"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name

security_rule {
name = "HTTP"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}

tags = {
environment = "Terraform Demo"
}
}

# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
name = "myNIC"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name

ip_configuration {
name = "myNicConfiguration"
subnet_id = azurerm_subnet.myterraformsubnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.myterraformpublicip.id
}

tags = {
environment = "Terraform Demo"
}
}

# Connect the security group to the network interface
resource "azurerm_network_interface_security_group_association" "example" {
network_interface_id = azurerm_network_interface.myterraformnic.id
network_security_group_id = azurerm_network_security_group.myterraformnsg.id
}

# Generate random text for a unique storage account name
resource "random_id" "randomId" {
keepers = {
# Generate a new ID only when a new resource group is defined
resource_group = azurerm_resource_group.myterraformgroup.name
}

byte_length = 8
}

main.tf

provider "azurerm" {
# The "feature" block is required for AzureRM provider 2.x.
# If you're using version 1.x, the "features" block is not allowed.
version = "~>2.0"
features {}
}
module "modules" {
source = "../modules/network"
}
module "vm" {
source = "../modules/vm"
}

目录结构:

$ ls
dev/ modules/
$ ls dev/
main.tf
$ ls modules/
network/ vm/

最佳答案

由于您已在 Network 模块中定义了网络接口(interface)资源,因此您希望在另一个模块 VM 中调用网络接口(interface)。您需要声明网络模块(子模块)的输出值,以有选择地导出某些值以供调用模块访问。

在这种情况下,

ma​​in.tf

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

}
module "vm" {
source = "../modules/vm"
vm_nic = module.network.nic
}

vm.tf

...

variable "vm_nic" {} # this variables.tf should be in the same directory as the vm.tf
...
resource "azurerm_windows_virtual_machine" "myterraformvm" {
name = "myVM"
location = "eastus"
resource_group_name = azurerm_resource_group.myterraformgroup.name
network_interface_ids = [var.vm_nic] # change this

网络.tf

output "nic" {
value = azurerm_network_interface.myterraformnic.id
}

有关更多信息,请阅读 https://www.terraform.io/docs/language/modules/syntax.html#calling-a-child-module

关于azure - 托管资源 "azurerm_network_interface" "myterraformnic"尚未在 module.vm 中声明。 azure ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67387465/

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