Terraform: Required Variable not being set from terraform.tfvars (cont of #12479 ?)

Created on 1 Nov 2017  ยท  2Comments  ยท  Source: hashicorp/terraform

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

Structure

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

Expected Behavior

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

Actual Behavior

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

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. dev> terraform validate -var-file=terraform.tfvars

References

  • #12479

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 -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!

All 2 comments

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 -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.

Was this page helpful?
0 / 5 - 0 ratings