Terraform继续说变量未定义,尽管其定义

发布于 2025-02-12 18:14:38 字数 2463 浏览 3 评论 0原文

我正在尝试通过Terraform在Azure中创建资源。即使我宣布了所有必需的变量,但它的错误。我正在使用模块用于相同的...

这是我尝试的以下代码:

我的模块的bastion/main.tf它用于创建资源:

resource "azurerm_public_ip" "syn_pip" {
  name                = "pip-${var.prefix}-${var.postfix}"
  location            = var.location
  resource_group_name = var.rg_name
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
}

我的模块的bastion/variables.tf

variable "bastion_allocation_method" {
  type = string
  description = "allocation method for bastion"
}

variable "bastion_sku_type" {
  type = string
  description = "sku to be used for bastion"
}

我的根模块bastion.tf

module "bastion" {
  source = "../modules/bastion"
  rg_name  = module.resource_group.name
  location = module.resource_group.location
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
  prefix  = var.prefix
  postfix = random_string.postfix.result
  subnet_id = azurerm_subnet.bastion_subnet.id
}

root模块的variable.tf

variable "bastion_allocation_method" {
  type = string
}

variable "bastion_sku_type" {
  type = string
}

我的terraform.tfvars传递给Terraform Plan >:

"bastion_allocation_method": "Static",
"bastion_sku_type": "Standard"

我遇到的错误:

│ Error: Missing required argument
│ 
│   on bastion.tf line 1, in module "bastion":
│    1: module "bastion" {
│ 
│ The argument "bastion_allocation_method" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│ 
│   on bastion.tf line 1, in module "bastion":
│    1: module "bastion" {
│ 
│ The argument "bastion_sku_type" is required, but no definition was found.
╵
╷
│ Error: Unsupported argument
│ 
│   on bastion.tf line 7, in module "bastion":
│    7:   allocation_method = var.bastion_allocation_method
│ 
│ An argument named "allocation_method" is not expected here.
╵
╷
│ Error: Unsupported argument
│ 
│   on bastion.tf line 8, in module "bastion":
│    8:   sku = var.bastion_sku_type
│ 
│ An argument named "sku" is not expected here.
╵

Exited with code exit status 1

有人可以建议,我在做什么错误?

I am trying to create a resource in azure via Terraform. Even though I have declared all required variables, its erroring out. I am using modules for the same...

Here is the below code that I tried:

My Module's Bastion/main.tf that used to create resource:

resource "azurerm_public_ip" "syn_pip" {
  name                = "pip-${var.prefix}-${var.postfix}"
  location            = var.location
  resource_group_name = var.rg_name
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
}

My modules's Bastion/variables.tf :

variable "bastion_allocation_method" {
  type = string
  description = "allocation method for bastion"
}

variable "bastion_sku_type" {
  type = string
  description = "sku to be used for bastion"
}

My root module bastion.tf :

module "bastion" {
  source = "../modules/bastion"
  rg_name  = module.resource_group.name
  location = module.resource_group.location
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
  prefix  = var.prefix
  postfix = random_string.postfix.result
  subnet_id = azurerm_subnet.bastion_subnet.id
}

root module's variable.tf:

variable "bastion_allocation_method" {
  type = string
}

variable "bastion_sku_type" {
  type = string
}

my terraform.tfvars which passed to terraform plan :

"bastion_allocation_method": "Static",
"bastion_sku_type": "Standard"

Error that I get:

│ Error: Missing required argument
│ 
│   on bastion.tf line 1, in module "bastion":
│    1: module "bastion" {
│ 
│ The argument "bastion_allocation_method" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│ 
│   on bastion.tf line 1, in module "bastion":
│    1: module "bastion" {
│ 
│ The argument "bastion_sku_type" is required, but no definition was found.
╵
╷
│ Error: Unsupported argument
│ 
│   on bastion.tf line 7, in module "bastion":
│    7:   allocation_method = var.bastion_allocation_method
│ 
│ An argument named "allocation_method" is not expected here.
╵
╷
│ Error: Unsupported argument
│ 
│   on bastion.tf line 8, in module "bastion":
│    8:   sku = var.bastion_sku_type
│ 
│ An argument named "sku" is not expected here.
╵

Exited with code exit status 1

Can someone suggest, what is the mistake I am doing ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2025-02-19 18:14:38

bastion在变量中的模块中

variable "bastion_allocation_method" {
  ...
}

variable "bastion_sku_type" {
  ...
}

在您 模块,您不正确使用变量名称,

module "bastion" {
  source = "../modules/bastion"
  ...
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
  ...
}

而不是

module "bastion" {
  source = "../modules/bastion"
  ...
  bastion_allocation_method = var.bastion_allocation_method
  bastion_sku_type = var.bastion_sku_type
  ...
}

在模块子句中传递的变量分配,应与模块的定义匹配变量.tf

In your Bastion module in the variables.tf you specify the arguments

variable "bastion_allocation_method" {
  ...
}

variable "bastion_sku_type" {
  ...
}

In your case your module expects the vars bastion_allocation_method and bastion_sku_type

But when you call the module, you dont pass the variable names properly

module "bastion" {
  source = "../modules/bastion"
  ...
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
  ...
}

Use instead

module "bastion" {
  source = "../modules/bastion"
  ...
  bastion_allocation_method = var.bastion_allocation_method
  bastion_sku_type = var.bastion_sku_type
  ...
}

The variable assignments that you pass in module clause should match the definitions in the module's variables.tf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文