使用Terraform validate时找不到所需的参数

问题描述

我的terraform目录结构类似于:

├── deploy
│      ├── dev.tfvars
│      └── qa.tfvars
├── modules
│   ├── private
│   │   ├── bastion.tf
│       ├── db.tf
│   │   └── variables.tf
│   └── public
│       ├── web.tf
│       └── variables.tf
├── main.tf

bastion.tf 中,我试图像这样从 variables.tf 调用变量:

resource "aws_eip" "bastion" {
  instance = "var.eip"
  vpc = true
}

其中eip = 10.x.x.x设置为 dev.tfvars

main.tf 的配置如下:

provider "aws" {}

terraform {
  backend "s3" {}
}

module "private" {
  source = "./modules/private"
}

运行 terrain validate 时,出现一个错误-需要参数“ eip”,但未找到定义。即使我尝试将eip赋予类似的模块:

module "private" {
  source = "./modules/private"
  eip = var.eip
}

它给了我另一个错误

enter image description here

尚未声明名称为“ eip”的输入变量。 可以使用变量“ eip” {}块

声明此变量

我已经在我的 variables.tf 中定义了variable "eip" {},以便它从 .tfvars文件获取值,但实际上并没有。谁能建议我还想念什么?

解决方法

听起来像是缺少变量声明...

从您的文件结构中:

├── deploy
│      ├── dev.tfvars
│      └── qa.tfvars
├── modules
│   ├── private
│   │   ├── bastion.tf
│       ├── db.tf
│   │   └── variables.tf
│   └── public
│       ├── web.tf
│       └── variables.tf
├── main.tf

variables.tf似乎并没有main.tf的陪伴者,每个级别都需要声明将由资源或子模块使用的变量。

如果您将代码上传到GitHub并发布链接,那么我可以帮助您解除封锁。