Terraform ignoring environment variable

Using terraform 0.12.28,

I have an environment variable called TF_VAR_my_var, it has a value, terraform should be able to read this.

I have a module with a var.tf file which includes:

variable "my_var" {}

In that same module, I use the above variable as such inside a string

"value": "${var.my_var}"

However I get the following message:


  on main.tf line 60, in module "ecs":
  60: module "ecs" {

The argument "my_var" is required, but no definition was found.

What am I doing wrong?

using TF_VAR_my_var works with 0.13.5.
Do you use var.my_var within your module?

and also with 0.12.28

Yes, as I mentioned, I use the variable in the module:

The TF_VAR_... environment variables are only for variables defined in the root module. If you want to pass the value from your environment variable into the child module then you will need to pass it through in the root module:

variable "my_var" {}

module "ecs" {
  # ...

  my_var = var.my_var
}
3 Likes

Yeah, this is exactly what was happening. Thanks