Hi,
I think I am having the same issue this is part of https://github.com/hashicorp/terraform/issues/12479 . I am opening this as the last comment asks to open new if using a new version. It's possible I am doing something wrong, but it looks like I have it correct.
Current version:
dev>terraform -version
Terraform v0.10.8
|--base
| |--s-provider.tf
| |--s-vars.tf
|--dev
| |--dev-setup.tf
| |--terraform.tfvars
s-provider.tf
provider "aws" {
region = "${var.aws["region"]}"
profile = "${var.aws["profile"]}"
version = "~> 0.1"
}
s-vars.tf
variable "aws" {
description = "Description for the AWS variable"
}
dev-setup.tf
module "base" {
source = "../base"
}
terraform.tfvars
aws = {
region = "us-east-1"
profile = "some-profile"
}
Should be able to validate or plan based on variables being set in the terraform.tfvars file as specified in the s-vars.tf file.
From within 'dev' I am running: dev>terraform validate -var-file=terraform.tfvars
There are warnings and/or errors related to your configuration. Please
fix these before continuing.
Errors:
* 1 error(s) occurred:
* module root:
module base: required variable "aws" not set
Please list the full steps required to reproduce the issue, for example:
dev> terraform validate -var-file=terraform.tfvarsHi @BTBP! Sorry this didn't work as expected.
Each Terraform module has its own separate variables, and only the _root_ module's variables can be populated with -var and -var-file. Since your "base" module declares variable "aws" it is required to set a value for it in the module "base" block:
module "base" {
source = "../base"
aws = {
region = "us-east-1"
profile = "some-profile"
}
}
In order to be able to set this from the command line, it's necessary to _also_ declare the aws variable in the root module (in your "dev" directory), and then pass through the value to the child module:
variable "aws" {
type = "map"
}
module "base" {
source = "../base"
aws = "${var.aws}"
}
Modules are conceptually similar to a function in a programming language, with the input variables being similar to a function's arguments. When you run terraform apply you are effectively "calling" the root module, and then it in turn can pass values onto the child modules _it_ calls.
I hope this helps!
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
Hi @BTBP! Sorry this didn't work as expected.
Each Terraform module has its own separate variables, and only the _root_ module's variables can be populated with
-varand-var-file. Since your "base" module declaresvariable "aws"it is required to set a value for it in themodule "base"block:In order to be able to set this from the command line, it's necessary to _also_ declare the
awsvariable in the root module (in your "dev" directory), and then pass through the value to the child module:Modules are conceptually similar to a function in a programming language, with the input variables being similar to a function's arguments. When you run
terraform applyyou are effectively "calling" the root module, and then it in turn can pass values onto the child modules _it_ calls.I hope this helps!