Home > Blockchain >  Unable to use terraform.tfvars inside terraform module
Unable to use terraform.tfvars inside terraform module

Time:07-07

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"

CodePudding user response:

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

CodePudding user response:

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
}
  • Related