无法在Terraform模块中使用Terraform.tfvars

发布于 2025-02-13 11:58:57 字数 1971 浏览 0 评论 0原文

面对某些问题,可以在模块内使用Terraform.TFVAR。 我的文件夹结构是

module/
      main.tf
      variable.tf
      terraform.tfvars
demo.tf
provider.tf

emo.tf的代码在

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

模块文件夹内,我在变量内声明了变量,并将其值放在terraform.tfvars中。

当我运行Terraform计划时,它显示出来,

   Error: Missing required argument
  on main.tf line 1, in module "module":
   1: module "module" {
The argument "<variable_name>" is required, but no definition was found.

请让我知道解决方案,谢谢。

(当我将值作为默认变量中的值放置时,它是在工作文件。)

有关更多详细信息,我将在下面添加所有文件

-Main.tf

resource "aws_glue_catalog_database" "glue_database_demo" {
  name = var.database_name  # var
  location_uri = "s3://${var.bucket_location}"  # var
}

resource "aws_glue_catalog_table" "aws_glue_catalog_table" {
  name          = var.table_name  # var
  database_name = aws_glue_catalog_database.glue_database_demo.name

  table_type = "EXTERNAL_TABLE"

  parameters = {
    EXTERNAL              = "TRUE"
    "parquet.compression" = "SNAPPY"
  }

  storage_descriptor {
    location      = "s3://${var.bucket_location}"  # var
    input_format  = "org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat"
    output_format = "org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat"

    ser_de_info {
      name                  = "my-stream"
      serialization_library = "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe"
    }

    columns {
      name = "filekey"
      type = "string"
    }

    columns {
      name = "reead_dt"
      type = "string"
    }
}

partition_keys {
  name = "load_dt"
  type = "string"
}
}

variables.tf

variable "database_name" {
}
variable "bucket_location" {
}
variable "table_name" {
}

terraform.tfvars

database_name = "mydatabase"
bucket_location = "kgvjgfkjhglbg"
table_name = "mytable"

Facing some issue to use terraform.tfvars inside module.
My folder structure is

module/
      main.tf
      variable.tf
      terraform.tfvars
demo.tf
provider.tf

The code of demo.tf is

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

Inside the module folder I have decleared variables inside variable.tf and put their values inside terraform.tfvars.

when I run terraform plan then it is showing

   Error: Missing required argument
  on main.tf line 1, in module "module":
   1: module "module" {
The argument "<variable_name>" is required, but no definition was found.

Please let me know the solution, Thanks in advance.

( When I put values as default inside variables.tf then it is working file.)

For more details, I am adding all files below -

main.tf

resource "aws_glue_catalog_database" "glue_database_demo" {
  name = var.database_name  # var
  location_uri = "s3://${var.bucket_location}"  # var
}

resource "aws_glue_catalog_table" "aws_glue_catalog_table" {
  name          = var.table_name  # var
  database_name = aws_glue_catalog_database.glue_database_demo.name

  table_type = "EXTERNAL_TABLE"

  parameters = {
    EXTERNAL              = "TRUE"
    "parquet.compression" = "SNAPPY"
  }

  storage_descriptor {
    location      = "s3://${var.bucket_location}"  # var
    input_format  = "org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat"
    output_format = "org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat"

    ser_de_info {
      name                  = "my-stream"
      serialization_library = "org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe"
    }

    columns {
      name = "filekey"
      type = "string"
    }

    columns {
      name = "reead_dt"
      type = "string"
    }
}

partition_keys {
  name = "load_dt"
  type = "string"
}
}

variables.tf

variable "database_name" {
}
variable "bucket_location" {
}
variable "table_name" {
}

terraform.tfvars

database_name = "mydatabase"
bucket_location = "kgvjgfkjhglbg"
table_name = "mytable"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聆听风音 2025-02-20 11:58:58

这不是模块的工作方式。如果您定义一个变量,则希望您在调用它时提供一个值,除非它具有按照您已经指出的默认值定义。为了使此工作起作用,调用模块时必须提供值:

module "modules" {
  source          = "./module"
  database_name   = "mydatabase"
  bucket_location = "kgvjgfkjhglbg"
  table_name      = "mytable"
}

另一个选项是在同一目录中定义变量 file,以便从中调用该模块,例如,:

# provide input for the module
variable "database_name" {
  type        = string
  description = "Glue DB name."
}

variable "bucket_location" {
  type        = string
  description = "Bucket region."
}

variable "table_name" {
  type        = string
  description = "Glue catalog table name."
}

然后,将terraform.tfvars复制到同一目录中,您从demo.tf中调用

module "glue" {
  source          = "./module"
  database_name   = var.database_name
  bucket_location = var.bucket_location
  table_name      = var.table_name
}

模块模块的逻辑名称从模块glue,因为它更具描述性,但不是必需的。

目录的最终前景应该是:

module/
      main.tf
      variables.tf
demo.tf
provider.tf
terraform.tfvars
variables.tf

This is not how modules work. If you define a variable, it is expecting you to provide a value when you are calling it unless it has a default value defined as you have already noted. In order for this to work, you would have to provide values when calling the module:

module "modules" {
  source          = "./module"
  database_name   = "mydatabase"
  bucket_location = "kgvjgfkjhglbg"
  table_name      = "mytable"
}

The other option would be to define a variables.tf file in the same directory where you are calling the module from, e.g.,:

# provide input for the module
variable "database_name" {
  type        = string
  description = "Glue DB name."
}

variable "bucket_location" {
  type        = string
  description = "Bucket region."
}

variable "table_name" {
  type        = string
  description = "Glue catalog table name."
}

Then, copy the terraform.tfvars to the same directory where you are calling the module from and in the demo.tf do the following:

module "glue" {
  source          = "./module"
  database_name   = var.database_name
  bucket_location = var.bucket_location
  table_name      = var.table_name
}

Note that I have changed the logical name of the module from modules to glue as it is more descriptive, but it's not necessary.

The final outlook of the directories should be:

module/
      main.tf
      variables.tf
demo.tf
provider.tf
terraform.tfvars
variables.tf
一腔孤↑勇 2025-02-20 11:58:58

在您的demo.tf文件中,在“模块”模块中,您需要提供输入变量值。

例如:

module "modules" {
  source          = "./module"
  database_name   = var.database_name
  bucket_location = var.bucket_location
  table_name      = var.table_name
}

Within your demo.tf file, in the "modules" module you need to provide the input variables value.

For example:

module "modules" {
  source          = "./module"
  database_name   = var.database_name
  bucket_location = var.bucket_location
  table_name      = var.table_name
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文