I'm trying to understand how variables are used with modules. Variables from env variables don't seem to be picked up in modules. Is this true, or am I misunderstanding something?
├── nat
------------
provider "aws" {
region = "${var.region}"
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
}
module "nat" {
source = "./nat"
}
-----------------
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "region" {
default = "us-west-2"
}
--------------
resource "aws_vpc" "main" {
enable_dns_hostnames = true
tags {
Name = "${var.environment}Stack"
}
}
--------------------
variable "environment" {}
I would expect environment to get picked up from an environment variable, if it's set...but it doesn't:
brianz@gold$ TF_VAR_aws_secret_key=foo \
TF_VAR_aws_access_key=bar \
TF_VAR_region=abc \
TF_VAR_environment=bz \
terraform plan
There are warnings and/or errors related to your configuration. Please
fix these before continuing.
Errors:
* 1 error(s) occurred:
* module root: module nat: required variable environment not set
The work around I found was to declare "environment" the nat module section and set it to the variable:
module "nat" {
source = "./nat"
environment = "${var.environment}"
}
But now "environment" is declared in two variables files and references in multiple places....seems pretty clunky. Why doesn't the module pick up the env variable setting?
BZ