托管资源 "azurerm_network_interface" "myterraformnic" 尚未在 module.vm 中声明。蔚蓝
A managed resource "azurerm_network_interface" "myterraformnic" has not been declared in module.vm. Azure
我创建了两个模块:
网络 2.VM
在 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模块中定义了网络接口资源,您希望在另一个模块VM中调用网络接口。您需要声明网络模块(子模块)的输出值,以选择性地导出某些值以供调用模块访问。
在这种情况下,
main.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
network.tf
output "nic" {
value = azurerm_network_interface.myterraformnic.id
}
有关详细信息,请阅读 https://www.terraform.io/docs/language/modules/syntax.html#calling-a-child-module
我创建了两个模块:
网络 2.VM 在 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模块中定义了网络接口资源,您希望在另一个模块VM中调用网络接口。您需要声明网络模块(子模块)的输出值,以选择性地导出某些值以供调用模块访问。
在这种情况下,
main.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
network.tf
output "nic" {
value = azurerm_network_interface.myterraformnic.id
}
有关详细信息,请阅读 https://www.terraform.io/docs/language/modules/syntax.html#calling-a-child-module