为何我的main.tf文件出现'Unsupported argument errors'错误?
解决Terraform模块调用的Unsupported argument错误
问题根源
你在根模块的variables.tf里定义的变量,和你调用的login_service模块内部的输入变量不匹配。Terraform要求模块调用时传递的参数,必须是目标模块自身声明的输入变量,根模块的变量不能直接作为模块参数传递,除非目标模块已经定义了对应的输入变量。
解决步骤
1. 检查并补全目标模块的输入变量
找到你指定的/path/to/module路径下的variables.tf文件,确认其中是否声明了name、port、command、image这些输入变量。如果没有,需要在该文件中添加对应的变量声明,示例如下:
variable "name" { type = string description = "Name of the login service" } variable "port" { type = number description = "Port number used by the login service" } variable "command" { type = list(string) description = "Command to run the login service" } variable "image" { type = string description = "Docker image for the login service" }
2. 修正根模块的模块调用代码
另外注意你在main.tf中写的image = "python:${var.image_version}",但根模块的variables.tf里并没有定义image_version变量,这里可以直接改用根模块已有的image变量:
module "login_service" { source = "/path/to/module" name = var.name image = var.image port = var.port command = var.command }
3. 验证模块目录结构
确保你的目录结构符合Terraform模块规范,示例如下:
你的项目根目录/ ├── main.tf # 根模块的main.tf ├── variables.tf # 根模块的变量定义 └── login_service/ # 对应/path/to/module的模块目录 ├── main.tf # 模块的资源定义 ├── variables.tf # 模块的输入变量声明(必须有) └── outputs.tf # 可选,模块的输出变量
内容的提问来源于stack exchange,提问作者redmage123
相关产品推荐
相关产品推荐

