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

通过CLI传递Terraform变量报错:未声明变量问题求助

解决Terraform未声明变量的错误

Terraform要求所有在配置中用到的var.xxx变量必须提前声明,不管你是通过CLI、tfvars文件还是其他方式传值,变量声明是硬性要求,你之前的错误就是因为没声明这些变量导致的,和main.tf的写法无关。

解决步骤:

  • 创建variables.tf文件,把你用到的三个变量声明进去:
variable "project_name" {
  type        = string
  description = "Lambda函数/项目名称"
}

variable "ecr_repo_uri" {
  type        = string
  description = "存储Lambda镜像的ECR仓库URI"
}

variable "image_tag" {
  type        = string
  description = "Lambda使用的Docker镜像标签"
}
  • 执行命令时别忘了补充project_name的变量值(你的main.tf里用到了这个变量,但之前的命令没传),完整命令如下:
terraform init && terraform apply -var "ecr_repo_uri=<ecr_repo_uri_var>" -var "image_tag=v0.0" -var "project_name=<你的项目名称>"

内容的提问来源于stack exchange,提问作者david backx

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 17:02:37