1
votes

Terraform newbie here. I've a module with variables.tf which creates a resource 'folder' in Google Cloud. The variables are defined as:

variable "folder_name" {
    type = "string"
    description = "Name of the folder"
}

The calling function of this module is main.tf.

module "folder" {
    source = "../<path>/"
}

When I run 'terraform init', it throws the following error-

$ terraform init
Initializing modules...
- module.folder
- module.project

Error: module "folder": missing required argument "folder_name"

I thought variables can be predetermined in a file or included in the command line options while running 'terraform apply'. I'd prefer CLI options, but then why am I seeing an argument error at the 'init' stage?

1

1 Answers

2
votes

You provide the value of each module variable when calling the module itself:

module "folder" {
    source = "../<path>/"
    folder_name = "xyz"  # add this line to define the folder_name variable
}

If you'd like to specify folder_name on the command line instead, you can create a variable in your main.tf file and provide that via command line instead:

variable "module_folder_name" {
  default = "xyz"
}

module "folder" {
   source = "../<path>/"
   folder_name = "${var.module_folder_name}"
}

And then provide this variable's value via the command line:

terraform apply -var="module_folder_name=abc"