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:terraformmodulesec2-fullstacksmain.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:terraformmodulesec2-fullstacksvariables.tf
contents:
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Under Project (testappapi):
The contents of C:terraformqatestappapimain.tf
:
provider "aws" {
}
terraform {
backend "s3" {
encrypt = true
}
}
module "testappapiqa" {
source = "C:/terraform/modules/ec2-fullstacks"
}
The contents of C:terraformqatestappapiterraform.tfvars
:
aws_account_name = "QA"
aws_region = "us-east-1"
ec2_ami_name = "WinAMI-2016-01-IIS"
The contents of C:terraformqatestappapitestappapi_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:terraformqatestappapi>terraform get
- module.testappapi
Getting source "C:/terraform/modules/ec2-fullstacks"
C:terraformqatestappapi>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:terraformqatestappapi>
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?
variables module terraform terraform-provider-aws
add a comment |
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:terraformmodulesec2-fullstacksmain.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:terraformmodulesec2-fullstacksvariables.tf
contents:
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Under Project (testappapi):
The contents of C:terraformqatestappapimain.tf
:
provider "aws" {
}
terraform {
backend "s3" {
encrypt = true
}
}
module "testappapiqa" {
source = "C:/terraform/modules/ec2-fullstacks"
}
The contents of C:terraformqatestappapiterraform.tfvars
:
aws_account_name = "QA"
aws_region = "us-east-1"
ec2_ami_name = "WinAMI-2016-01-IIS"
The contents of C:terraformqatestappapitestappapi_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:terraformqatestappapi>terraform get
- module.testappapi
Getting source "C:/terraform/modules/ec2-fullstacks"
C:terraformqatestappapi>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:terraformqatestappapi>
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?
variables module terraform terraform-provider-aws
add a comment |
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:terraformmodulesec2-fullstacksmain.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:terraformmodulesec2-fullstacksvariables.tf
contents:
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Under Project (testappapi):
The contents of C:terraformqatestappapimain.tf
:
provider "aws" {
}
terraform {
backend "s3" {
encrypt = true
}
}
module "testappapiqa" {
source = "C:/terraform/modules/ec2-fullstacks"
}
The contents of C:terraformqatestappapiterraform.tfvars
:
aws_account_name = "QA"
aws_region = "us-east-1"
ec2_ami_name = "WinAMI-2016-01-IIS"
The contents of C:terraformqatestappapitestappapi_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:terraformqatestappapi>terraform get
- module.testappapi
Getting source "C:/terraform/modules/ec2-fullstacks"
C:terraformqatestappapi>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:terraformqatestappapi>
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?
variables module terraform terraform-provider-aws
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:terraformmodulesec2-fullstacksmain.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:terraformmodulesec2-fullstacksvariables.tf
contents:
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Under Project (testappapi):
The contents of C:terraformqatestappapimain.tf
:
provider "aws" {
}
terraform {
backend "s3" {
encrypt = true
}
}
module "testappapiqa" {
source = "C:/terraform/modules/ec2-fullstacks"
}
The contents of C:terraformqatestappapiterraform.tfvars
:
aws_account_name = "QA"
aws_region = "us-east-1"
ec2_ami_name = "WinAMI-2016-01-IIS"
The contents of C:terraformqatestappapitestappapi_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:terraformqatestappapi>terraform get
- module.testappapi
Getting source "C:/terraform/modules/ec2-fullstacks"
C:terraformqatestappapi>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:terraformqatestappapi>
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?
variables module terraform terraform-provider-aws
variables module terraform terraform-provider-aws
edited Nov 22 '18 at 10:18


Vít Kotačka
512522
512522
asked Nov 21 '18 at 20:33
RafiqRafiq
14911
14911
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have created a module in c:terraformmodulesec2-fullstacksmain.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:terraformqatestappapimain.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:terraformqatestappapivariables.tf
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Now this should work.
Thank you for your answer. In fact, this much I know. When I copy the variables.tf file from the module folder to the project folder and calling all data using "${var.<varaible_name>}", I won't consider this as full reusable of terraform codes.
– Rafiq
Nov 23 '18 at 5:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420087%2fpassing-terraform-variable-values-from-tfvars-file-in-the-project-folders-to-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have created a module in c:terraformmodulesec2-fullstacksmain.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:terraformqatestappapimain.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:terraformqatestappapivariables.tf
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Now this should work.
Thank you for your answer. In fact, this much I know. When I copy the variables.tf file from the module folder to the project folder and calling all data using "${var.<varaible_name>}", I won't consider this as full reusable of terraform codes.
– Rafiq
Nov 23 '18 at 5:06
add a comment |
You have created a module in c:terraformmodulesec2-fullstacksmain.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:terraformqatestappapimain.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:terraformqatestappapivariables.tf
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Now this should work.
Thank you for your answer. In fact, this much I know. When I copy the variables.tf file from the module folder to the project folder and calling all data using "${var.<varaible_name>}", I won't consider this as full reusable of terraform codes.
– Rafiq
Nov 23 '18 at 5:06
add a comment |
You have created a module in c:terraformmodulesec2-fullstacksmain.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:terraformqatestappapimain.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:terraformqatestappapivariables.tf
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Now this should work.
You have created a module in c:terraformmodulesec2-fullstacksmain.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:terraformqatestappapimain.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:terraformqatestappapivariables.tf
variable "ec2_ami_name" {}
variable "aws_account_name" {}
variable "aws_region" {}
Now this should work.
edited Nov 24 '18 at 7:43
marc_s
578k12911151260
578k12911151260
answered Nov 22 '18 at 8:59


RamRam
385112
385112
Thank you for your answer. In fact, this much I know. When I copy the variables.tf file from the module folder to the project folder and calling all data using "${var.<varaible_name>}", I won't consider this as full reusable of terraform codes.
– Rafiq
Nov 23 '18 at 5:06
add a comment |
Thank you for your answer. In fact, this much I know. When I copy the variables.tf file from the module folder to the project folder and calling all data using "${var.<varaible_name>}", I won't consider this as full reusable of terraform codes.
– Rafiq
Nov 23 '18 at 5:06
Thank you for your answer. In fact, this much I know. When I copy the variables.tf file from the module folder to the project folder and calling all data using "${var.<varaible_name>}", I won't consider this as full reusable of terraform codes.
– Rafiq
Nov 23 '18 at 5:06
Thank you for your answer. In fact, this much I know. When I copy the variables.tf file from the module folder to the project folder and calling all data using "${var.<varaible_name>}", I won't consider this as full reusable of terraform codes.
– Rafiq
Nov 23 '18 at 5:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420087%2fpassing-terraform-variable-values-from-tfvars-file-in-the-project-folders-to-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown