You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform EC2模块报错:instance_type参数不被预期的问题求助

解决Terraform模块调用时的"Unsupported argument"报错

报错详情

Error: Unsupported argument
instance_type = "t2.large"
An argument named "instance_type" is not expected here.

问题根源

你调用EC2模块时传入的参数名instance_type,和模块内部定义的变量名不匹配——模块里用来接收实例类型的变量是instance,而非instance_type,Terraform无法识别未在模块中定义的参数,因此抛出该错误。

解决方案

方案一:修改模块调用代码(推荐)

直接调整调用模块时的参数名,匹配模块内部已定义的instance变量:

module "app-server" {
  source  = "../../modules/ec2"
  instance = "t2.large" # 将instance_type改为instance
}

方案二:修改EC2模块的变量定义

如果希望保持调用时使用instance_type作为参数名,可以修改模块内的变量配置:

  1. 更新模块中的变量定义:
variable "instance_type" { # 变量名从instance改为instance_type
  type    = string
  default = "t2.micro"
}
  1. 同步修改模块内aws_instance资源的变量引用:
resource "aws_instance" "app01" {
  ami           = lookup(var.ami, var.region)
  instance_type = var.instance_type # 引用更新后的变量名
  # 其余配置保持不变
}

内容的提问来源于stack exchange,提问作者Subhajit Das

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:22:47