Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing terraform variable values from .tfvars file in the project folders to the module

The following is my folder structure of my terraform project for AWS:

c:\terraform
   ├─modules
   │   └─ec2-fullstacks
   │       ├─main.tf
   │       └─variables.tf
   └─qa
       └─testappapi
           ├─testappapi_backend.tfvars
           ├─main.tf
           └─terraform.tfvars

Under Module:

The contents of c:\terraform\modules\ec2-fullstacks\main.tf:

provider "aws" {
}

terraform {
  backend "s3" {
    encrypt = true
  }
}

data "aws_ami" "ami" {
  most_recent = true
  filter {
    name   = "name"
    values = ["${var.ec2_ami_name}*"]
  }
}

output "ami_id" {
  value = "${data.aws_ami.ami.id}"
}

The c:\terraform\modules\ec2-fullstacks\variables.tf contents:

variable "ec2_ami_name"  {}
variable "aws_account_name" {}
variable "aws_region" {}

Under Project (testappapi):

The contents of C:\terraform\qa\testappapi\main.tf:

provider "aws" {
}

terraform {
  backend "s3" {
    encrypt = true
  }
}

module "testappapiqa" {
  source = "C:/terraform/modules/ec2-fullstacks"
}

The contents of C:\terraform\qa\testappapi\terraform.tfvars:

aws_account_name = "QA"
aws_region = "us-east-1"
ec2_ami_name = "WinAMI-2016-01-IIS"

The contents of C:\terraform\qa\testappapi\testappapi_backend.tfvars:

profile = "qa"
region = "us-east-1"
bucket = "tfstate-123456789012"
key = "qa/testappapi.tfstate"
dynamodb_table = "tfstate"

Here is what happens when I try to initialize:

C:\terraform\qa\testappapi>terraform get
- module.testappapi
  Getting source "C:/terraform/modules/ec2-fullstacks"

C:\terraform\qa\testappapi>terraform init -backend-config=testappapi_backend.tfvars
Initializing modules...
- module.testappapi

Initializing the backend...
Error: module "testappapi": missing required argument "ec2_ami_name"
Error: module "testappapi": missing required argument "aws_account_name"
Error: module "testappapi": missing required argument "aws_region"

C:\terraform\qa\testappapi>

I was expecting that the source in main.tf under project folder (testappapi) would get the values from the terraform.tfvars file under the same project folder, but it is not.

What am I missing here?

like image 907
Rafiq Avatar asked Nov 21 '18 20:11

Rafiq


1 Answers

You have created a module in c:\terraform\modules\ec2-fullstacks\main.tf with following mandatory variables

variable "ec2_ami_name"  {}
variable "aws_account_name" {}
variable "aws_region" {}

So while referring this module terraform expects you to pass these mandatory params as well. You can use it like this in C:\terraform\qa\testappapi\main.tf

module "testappapiqa" {
  source = "C:/terraform/modules/ec2-fullstacks"
  ec2_ami_name = "${var.ec2_ami_name}"
  aws_account_name = "${var.aws_account_name}"
  aws_region = "${var.aws_region}"
}

Now the main file is referring ec2_ami_name, aws_account_name, aws_region variables, which are not defined in testappapi folder. So you can define these variables in C:\terraform\qa\testappapi\variables.tf

variable "ec2_ami_name"  {}
variable "aws_account_name" {}
variable "aws_region" {}

Now this should work.

like image 56
Ram Avatar answered Sep 24 '22 07:09

Ram