0

我已经成功创建了一个 vnet 模块(带有一个资源组)。但我现在正试图在计算模块中创建一个天蓝色的公共 IP 地址,但想将它放在我在 vnet 模块中创建的资源组中。请帮忙。

文件夹结构:

    c:\terraform
       -main
       -terraform.tfvars
       -variables.tf
    c:\terraform\modules\vnet
       -main
       -output
       -variables
    c:\terraform\modules\compute
      -main
      -variables
      -output

c:\terraform\main.tf 的内容

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.68.0"
    }
  }
  backend "azurerm" {
    resource_group_name  = "rg-terraform"
    storage_account_name = "terraform85"
    container_name       = "tfstate"
    key                  = "terraform.tfstate"
  }

}

provider "azurerm" {
  features {}
}


resource "azurerm_resource_group" "compute" {
  name     = "rg-compute"
  location = var.location
}

resource "azurerm_resource_group" "storage" {
  name     = "rg-storage"
  location = var.location
}

#Call vnet child module
module "azure-vnet" {
  source = "./modules/vnet/"

}

#Call compute child module
module "azure-compute" {
  source = "./modules/compute/"

}

c:\terraform\variables.tf 的内容

variable "location" {
  type = string
}

c:\terraform\terraform.tfvars 的内容

location = "UK South"

c:\terraform\modules\vnet\main.tf 的内容:

#This module will create a Azure resource group
#1 virtual network
#3 subnets. 


resource "azurerm_resource_group" "network" {
  name     = "rg-network"
  location = var.location
}

resource "azurerm_virtual_network" "vnet" {
  name                = var.vnet_name
  location            = azurerm_resource_group.network.location
  resource_group_name = azurerm_resource_group.network.name
  address_space       = var.address_space
}

resource "azurerm_subnet" "subnet1" {
  name                 = "AzureBastionSubnet"
  resource_group_name  = azurerm_resource_group.network.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["172.16.0.0/27"]
}

resource "azurerm_subnet" "subnet2" {
  name                 = "subnet-web"
  resource_group_name  = azurerm_resource_group.network.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["172.16.0.32/27"]
}

resource "azurerm_subnet" "subnet3" {
  name                 = "subnet-app"
  resource_group_name  = azurerm_resource_group.network.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["172.16.0.64/27"]
}

resource "azurerm_subnet" "subnet4" {
  name                 = "subnet-core"
  resource_group_name  = azurerm_resource_group.network.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["172.16.0.96/27"]
}

resource "azurerm_subnet" "subnet5" {
  name                 = "subnet-data"
  resource_group_name  = azurerm_resource_group.network.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["172.16.0.128/27"]
}

C:\terraform\modules\vnet\output.tf 的内容

output "ResourceGroupid" {
    description = "id of the resource group provisioned"
    value = azurerm_resource_group.network.name
}

c:\terraform\modules\vnet\variables.tf 的内容:

variable "location" {
    type = string
    default = "UK South"
  
}
variable "vnet_name"{
default = "core-vnet"
}

variable "address_space"{
default = ["172.16.0.0/22"]
}


c:\terraform\modules\compute\main.tf 的内容

#This module will create an Azure VM

module "azure-vnet2" {
 source = "../../modules/vnet"
 name = azurerm_resource_group.network.name
 location = "UK South"
  
}

## Create public IP address. Place in the resource group created in the vnet module block - Network resource group.
resource   "azurerm_public_ip"   "myvm1publicip"   { 
   name   =   "pip-vm-dc01" 
   location   =   "UK South" 
   resource_group_name   =   module.azure-vnet2.name
   allocation_method   =   "Dynamic" 
   sku   =   "Basic" 
 } 

当我运行 Terraform 计划时,我收到以下错误。我知道计算模块非常错误,但我不知道是什么..

PS C:\Users\xxxxxxxxx\xxxx> terraform plan

╷
│ Error: Unsupported argument
│
│   on modules\compute\main.tf line 5, in module "azure-vnet2":
│    5:  name = azurerm_resource_group.network.name
│
│ An argument named "name" is not expected here.
4

0 回答 0