Home > Mobile >  Terraform command is not using the values from the tfvars file provide with --var-file
Terraform command is not using the values from the tfvars file provide with --var-file

Time:09-17

I am pretty new in Terraform, trying to create separate backend for each AWS Region under each environment (dev, stg, prod) of my application. So, I am using separate .config files to configure separate backend, and separate .tfvars files to create resources on relevant environmet/regions.

I am detailing everything below:

Folder structure:

.
├── config
│   ├── stg-useast1.config
│   ├── stg-uswest2.config
│   ├── prod-useast1.config
│   └── prod-uswest2.config
├── vars
│   ├── stg-useast1.tfvars
│   ├── stg-uswest2.tfvars
│   ├── prod-useast1.tfvars
│   └── prod-useast1.tfvars
└── modules
    ├── backend.tf
    ├── main.tf
    ├── variables.tf
    └── module-ecs
        ├── main.tf
        └── variables.tf

File contents necessary for this question are showing below (just one region):

./config/stg-useast1.config

profile        = "myapp-stg"
region         = "us-east-1"
bucket         = "myapp-tfstate-stg-useast1"
key            = "myapp-stg-useast1.tfstate"
dynamodb_table = "myapp-tfstate-stg-useast1-lock"

./vars/stg-useast1.tfvars

environment = "stg"
app_name    = "myapp-ecs"
aws_region  = "us-east-1"
aws_profile = "myapp-stg"

./modules/backend.tf

terraform {
  backend "s3" {
  }
}

./modules/main.tf

provider "aws" {
  region                   = var.aws_region
  shared_credentials_files = ["~/.aws/credentials"]
  profile                  = var.aws_profile
}

module "aws-ecr" {
  source = "./ecs"
}

./modules/variables.tf

variable "app_name" {}
variable "environment" {}
variable "aws_region" {}
variable "aws_profile" {}

./modules/module-ecs/main.tf

resource "aws_ecr_repository" "aws-ecr" {
  name = "${var.app_name}-${var.environment}-ecr"
  tags = {
    Name        = "${var.app_name}-ecr"
    Environment = var.environment
  }
}

./modules/module-ecs/variables.tf

variable "app_name" {
    default = "myapp-ecs"
}
variable "environment" {
    default = "stg"
}
variable "aws_region" {
    default = "us-east-1"
}
variable "aws_profile" {
    default = "myapp-stg"
}

The terraform init went well.

$ terraform init --backend-config=../config/stg-useast1.config
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v4.31.0

Terraform has been successfully initialized!

I ran terraform plan as following:

terraform plan --var-file=../vars/stg-useast1.tfvars

But it did not use the values from this .tfvars file. I had to supply them in ./modules/module-ecs/variables.tf as default = <value> for each variables.

How can I make use of the .tfvars file with terraform plan command?

Any restructuring suggestion is welcomed.

CodePudding user response:

The local module you've created doesn't inherit variables. You will need to pass them in. For example:

module "aws-ecr" {
  source = "./ecs" # in your example it looks like the folder is ecs-module?
  app_name = var.app_name
  environment = var.environment
  aws_region = var.region
  aws_profile = var.profile
}
  • Related