无法使用地形变换.地形模块内的Tfvars



使用地形时面临一些问题。模块内部的Tfvars。我的文件夹结构是

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

demo的代码。tf是

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

模块文件夹里面我有有关变量内部变量。并将它们的值放入terra .tfvars.

当我运行terraform plan时,它会显示

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"

模块不是这样工作的。如果您定义了一个变量,它会期望您在调用它时提供一个值,除非您已经注意到它定义了一个默认值。为了使其工作,您必须在调用模块时提供值:

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

另一个选项是在调用模块的同一目录中定义一个variables.tf文件,例如:

# 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
}

请注意,我已经将模块的逻辑名称从modules更改为glue,因为它更具描述性,但这不是必需的。

目录的最终外观应该是:

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

在演示中。在"modules"模块中需要提供的输入变量值

例如:

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

相关内容

  • 没有找到相关文章

最新更新