terraform plan returns the Error: Unsupported argument
terraform plan returns the Error: Unsupported argument
我有以下三个文件:
main.tf、variables.tf 和 dev.auto.tfvars
片段来自 main.tf
module "sql_vms" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/"
rg_name = var.resource_group_name
location = module.resource_group.external_rg_location
vnet_name = var.virtual_network_name
subnet_name = var.sql_subnet_name
app_nsg = var.application_nsg
vm_count = var.count_vm
base_hostname = var.sql_host_basename
sto_acc_suffix = var.storage_account_suffix
vm_size = var.virtual_machine_size
vm_publisher = var.virtual_machine_image_publisher
vm_offer = var.virtual_machine_image_offer
vm_sku = var.virtual_machine_image_sku
vm_img_version = var.virtual_machine_image_version
username = var.username
password = var.password
}
片段来自 variables.tf
variable "app_subnet_name" {
type = string
}
variable "sql_subnet_name" {
type = string
}
片段来自 dev.auto.tfvars
app_subnet_name = "subnet_1"
sql_subnet_name = "subnet_2"
application_nsg = "test_nsg"
但是,我收到如下错误
Error: Unsupported argument
on main.tf line 7, in module "sql_vms":
7: subnet_name = var.sql_subnet_name
An argument named "subnet_name" is not expected here.
Error: Unsupported argument
on main.tf line 8, in module "sql_vms":
8: app_nsg = var.application_nsg
An argument named "app_nsg" is not expected here.
我的模块目录结构如下所示
$ ls -R terraform-modules/
terraform-modules/:
aws azure gcp
terraform-modules/aws:
alb ec2-instance-rhel
terraform-modules/aws/alb:
terraform-modules/aws/ec2-instance-rhel:
main.tf
terraform-modules/azure:
compute resourcegroup sqlserver
terraform-modules/azure/compute:
main.tf README.md variable.tf
terraform-modules/azure/resourcegroup:
data.tf outputs.tf variables.tf
terraform-modules/azure/sqlserver:
main.tf README.md variables.tf
terraform-modules/gcp:
compute
terraform-modules/gcp/compute:
main.tf
知道这里出了什么问题吗?
它的发生可能有很多原因。
我建议进行一些验证:
- 检查您是否使用了正确的来源 URL、路径或版本 branch/tag。
我不确定你的实现,但你可能想仔细检查你引用的修订是否包含这些变量声明。
GitHub 模块寻址允许 ref 参数。
参考GitHub Module Addressing for Terraform Documentation and how to specify a revision.
- 检查是否在每个模块上都声明了所有必需的变量,包括根模块。
您是否在根目录和模块 context/path 的 variables.tf 文件中声明了这些变量?
我知道这很累而且很重复,但每个模块都应该设计成一个“独立项目”。 每个模块**必须有自己的声明variables.tf**,作为该模块的输入,它也希望有自己的映射outputs.tf、provider.tf、backend.tf 等,尽管最后这些不是必需的。
仅供参考: 这样做可以保证可扩展性、可重用性以及使用不同 tfstate 文件甚至不同存储库的可靠性每个模块以保证原子性和最小权限,从而防止您的基础架构被不需要的代码更改破坏。
强烈推荐this read了解独立模块化设计的重要性
此外,像 Terragrunt、Terratest 这样的工具可以通过 keeping your code DRY ( Don't Repeat Yourself ) 减轻这项工作的痛苦。
- 检查相关变量的**类型约束是否匹配。**
如果您不是这种情况,请尝试查看所有用作参数(在您的根 variables.tf 上)和输入的变量声明之间的类型约束是否匹配(在您的模块级别 variables.tf)。
在不知道有关模块的详细信息的情况下,通常很难说出错误的原因是什么,但在这种特殊情况下,您导入的模块似乎没有要求使用这两个参数(subnet_name
和 app_nsg
),或者更确切地说,您正在使用模块的 版本 要求他们在场。有助于解决此类错误的方法是检查是否存在具有此类要求的模块版本。使用来自 Github 的特定模块版本的语法在 Terraform Module Sources documentation, Selecting a Revision 部分进行了解释:
module "vpc" {
source = "git::https://example.com/vpc.git?ref=v1.2.0"
}
您可能正在使用 SSH 获取模块,所以 recommended way to do that 是:
When using Git over SSH, we recommend using the ssh://-prefixed URL form for consistency with all of the other URL-like git address forms.
在您的示例中,这转换为:
module "sql_vms" {
source = "git::ssh://git@github.com/org/terraform-modules-repo.git//azure/module-name?ref=v1.2.0"
其中 org
是您组织的(或您的私人)Github 帐户,terraform-modules-repo
是模块所在的存储库,module-name
是您正在使用的模块,并且ref=v1.2.0
表示模块修订号。
错误 An argument named "example" is not expected here.
表示模块不希望看到具有该名称的输入参数。将 Terraform 模块视为编程语言中的函数:为了让函数提供结果,您向函数传递一组必需的参数。如果您提供的输入参数比该函数调用所需的多(或少),您将收到错误消息。 (有特殊情况但不在本题讨论范围内)
模块和函数之间的另一个相似之处是,Terraform 模块除了创建指定的资源外,还可以提供 output values。在输出可以用作其他模块或资源的输入的情况下,这会很方便。 module.resource_group.external_rg_location
行正是这样做的:从另一个模块获取输出值并使用它为参数 location
赋值。
我认为问题在于您没有在源代码中引用确切的模块。我看到你在源代码中有三个模块:
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/"
它们是 compute
、resourcegroup
和 sqlserver
。但是您想将它们加载到一个模块中。所以它找不到模块的相关变量。我也不认为像那样加载所有模块是正确的方法。我建议您像下面这样一一加载模块:
module "compute" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/compute"
...
}
module "resourcegroup" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/resourcegroup"
...
}
module "sqlserver" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/sqlserver"
...
}
我也会分担我的痛苦。
像这样写块配置
vpc_config = {
subnet_ids = [aws_subnet.example1.id, aws_subnet.example2.id]
}
而不是(注意 =
等号):
vpc_config {
subnet_ids = [aws_subnet.example1.id, aws_subnet.example2.id]
}
会给出 An argument named "vpc_config" is not expected here
的错误并且会浪费你几个小时。
如果您开始使用 Terraform,如果您的模块参数引用资源属性而不是变量名称,请参见下面的示例:
您想从您的模块调用的 Terraform 模块“example_mod.tf”示例:
variable "sg_name" { } # Usually in a separate file
variable "sg_desc" { } # called variables.tf
resource "example_resource" "example_name" {
name = var.sg_name
description = var.sg_desc
...
}
正确的方法:
module "my_module" {
source = "./modules/example_mod.tf"
sg_name = "whatever" # NOTE the left hand side "sg_name" is the variable name
sg_desc = "whatever"
...
}
不正确的方式:(给出错误“此处不期望名为“name”的参数”)
module "my_module" {
source = "./modules/example_mod.tf"
name = "whatever" # WRONG because the left hand side "name" is a resource property
description = "whatever" # WRONG for the same reason
...
}
我在使用 AWS Eventbridge 和 Terraform.
时遇到了类似的问题
当我 运行 terraform plan
我得到以下错误:
Error: Unsupported argument
│
│ on ../../modules/aws/eventbridge/main.tf line 37, in resource "aws_cloudwatch_event_target" "ecs_cloudwatch_event_target":
│ 37: maximum_age_in_seconds = var.maximum_age_in_seconds
│
│ An argument named "maximum_age_in_seconds" is not expected here.
我是这样解决的:
问题是我没有为 AWS Eventbridge 资源块使用正确的属性。
该属性应该是 maximum_event_age_in_seconds
而不是 maximum_age_in_seconds
。
另一个可能导致此问题的问题是未在您的 terraform 脚本中定义已在模块中定义的变量。
就这些了
我有以下三个文件: main.tf、variables.tf 和 dev.auto.tfvars
片段来自 main.tf
module "sql_vms" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/"
rg_name = var.resource_group_name
location = module.resource_group.external_rg_location
vnet_name = var.virtual_network_name
subnet_name = var.sql_subnet_name
app_nsg = var.application_nsg
vm_count = var.count_vm
base_hostname = var.sql_host_basename
sto_acc_suffix = var.storage_account_suffix
vm_size = var.virtual_machine_size
vm_publisher = var.virtual_machine_image_publisher
vm_offer = var.virtual_machine_image_offer
vm_sku = var.virtual_machine_image_sku
vm_img_version = var.virtual_machine_image_version
username = var.username
password = var.password
}
片段来自 variables.tf
variable "app_subnet_name" {
type = string
}
variable "sql_subnet_name" {
type = string
}
片段来自 dev.auto.tfvars
app_subnet_name = "subnet_1"
sql_subnet_name = "subnet_2"
application_nsg = "test_nsg"
但是,我收到如下错误
Error: Unsupported argument
on main.tf line 7, in module "sql_vms":
7: subnet_name = var.sql_subnet_name
An argument named "subnet_name" is not expected here.
Error: Unsupported argument
on main.tf line 8, in module "sql_vms":
8: app_nsg = var.application_nsg
An argument named "app_nsg" is not expected here.
我的模块目录结构如下所示
$ ls -R terraform-modules/
terraform-modules/:
aws azure gcp
terraform-modules/aws:
alb ec2-instance-rhel
terraform-modules/aws/alb:
terraform-modules/aws/ec2-instance-rhel:
main.tf
terraform-modules/azure:
compute resourcegroup sqlserver
terraform-modules/azure/compute:
main.tf README.md variable.tf
terraform-modules/azure/resourcegroup:
data.tf outputs.tf variables.tf
terraform-modules/azure/sqlserver:
main.tf README.md variables.tf
terraform-modules/gcp:
compute
terraform-modules/gcp/compute:
main.tf
知道这里出了什么问题吗?
它的发生可能有很多原因。 我建议进行一些验证:
- 检查您是否使用了正确的来源 URL、路径或版本 branch/tag。
我不确定你的实现,但你可能想仔细检查你引用的修订是否包含这些变量声明。
GitHub 模块寻址允许 ref 参数。
参考GitHub Module Addressing for Terraform Documentation and how to specify a revision.
- 检查是否在每个模块上都声明了所有必需的变量,包括根模块。
您是否在根目录和模块 context/path 的 variables.tf 文件中声明了这些变量?
我知道这很累而且很重复,但每个模块都应该设计成一个“独立项目”。 每个模块**必须有自己的声明variables.tf**,作为该模块的输入,它也希望有自己的映射outputs.tf、provider.tf、backend.tf 等,尽管最后这些不是必需的。
仅供参考: 这样做可以保证可扩展性、可重用性以及使用不同 tfstate 文件甚至不同存储库的可靠性每个模块以保证原子性和最小权限,从而防止您的基础架构被不需要的代码更改破坏。
强烈推荐this read了解独立模块化设计的重要性
此外,像 Terragrunt、Terratest 这样的工具可以通过 keeping your code DRY ( Don't Repeat Yourself ) 减轻这项工作的痛苦。
- 检查相关变量的**类型约束是否匹配。**
如果您不是这种情况,请尝试查看所有用作参数(在您的根 variables.tf 上)和输入的变量声明之间的类型约束是否匹配(在您的模块级别 variables.tf)。
在不知道有关模块的详细信息的情况下,通常很难说出错误的原因是什么,但在这种特殊情况下,您导入的模块似乎没有要求使用这两个参数(subnet_name
和 app_nsg
),或者更确切地说,您正在使用模块的 版本 要求他们在场。有助于解决此类错误的方法是检查是否存在具有此类要求的模块版本。使用来自 Github 的特定模块版本的语法在 Terraform Module Sources documentation, Selecting a Revision 部分进行了解释:
module "vpc" {
source = "git::https://example.com/vpc.git?ref=v1.2.0"
}
您可能正在使用 SSH 获取模块,所以 recommended way to do that 是:
When using Git over SSH, we recommend using the ssh://-prefixed URL form for consistency with all of the other URL-like git address forms.
在您的示例中,这转换为:
module "sql_vms" {
source = "git::ssh://git@github.com/org/terraform-modules-repo.git//azure/module-name?ref=v1.2.0"
其中 org
是您组织的(或您的私人)Github 帐户,terraform-modules-repo
是模块所在的存储库,module-name
是您正在使用的模块,并且ref=v1.2.0
表示模块修订号。
错误 An argument named "example" is not expected here.
表示模块不希望看到具有该名称的输入参数。将 Terraform 模块视为编程语言中的函数:为了让函数提供结果,您向函数传递一组必需的参数。如果您提供的输入参数比该函数调用所需的多(或少),您将收到错误消息。 (有特殊情况但不在本题讨论范围内)
模块和函数之间的另一个相似之处是,Terraform 模块除了创建指定的资源外,还可以提供 output values。在输出可以用作其他模块或资源的输入的情况下,这会很方便。 module.resource_group.external_rg_location
行正是这样做的:从另一个模块获取输出值并使用它为参数 location
赋值。
我认为问题在于您没有在源代码中引用确切的模块。我看到你在源代码中有三个模块:
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/"
它们是 compute
、resourcegroup
和 sqlserver
。但是您想将它们加载到一个模块中。所以它找不到模块的相关变量。我也不认为像那样加载所有模块是正确的方法。我建议您像下面这样一一加载模块:
module "compute" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/compute"
...
}
module "resourcegroup" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/resourcegroup"
...
}
module "sqlserver" {
source = "git::git@github.com:xxxxxxxxxxxx/terraform-modules//azure/sqlserver"
...
}
我也会分担我的痛苦。
像这样写块配置
vpc_config = {
subnet_ids = [aws_subnet.example1.id, aws_subnet.example2.id]
}
而不是(注意 =
等号):
vpc_config {
subnet_ids = [aws_subnet.example1.id, aws_subnet.example2.id]
}
会给出 An argument named "vpc_config" is not expected here
的错误并且会浪费你几个小时。
如果您开始使用 Terraform,如果您的模块参数引用资源属性而不是变量名称,请参见下面的示例:
您想从您的模块调用的 Terraform 模块“example_mod.tf”示例:
variable "sg_name" { } # Usually in a separate file
variable "sg_desc" { } # called variables.tf
resource "example_resource" "example_name" {
name = var.sg_name
description = var.sg_desc
...
}
正确的方法:
module "my_module" {
source = "./modules/example_mod.tf"
sg_name = "whatever" # NOTE the left hand side "sg_name" is the variable name
sg_desc = "whatever"
...
}
不正确的方式:(给出错误“此处不期望名为“name”的参数”)
module "my_module" {
source = "./modules/example_mod.tf"
name = "whatever" # WRONG because the left hand side "name" is a resource property
description = "whatever" # WRONG for the same reason
...
}
我在使用 AWS Eventbridge 和 Terraform.
时遇到了类似的问题当我 运行 terraform plan
我得到以下错误:
Error: Unsupported argument
│
│ on ../../modules/aws/eventbridge/main.tf line 37, in resource "aws_cloudwatch_event_target" "ecs_cloudwatch_event_target":
│ 37: maximum_age_in_seconds = var.maximum_age_in_seconds
│
│ An argument named "maximum_age_in_seconds" is not expected here.
我是这样解决的:
问题是我没有为 AWS Eventbridge 资源块使用正确的属性。
该属性应该是 maximum_event_age_in_seconds
而不是 maximum_age_in_seconds
。
另一个可能导致此问题的问题是未在您的 terraform 脚本中定义已在模块中定义的变量。
就这些了