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












0















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?










share|improve this question





























    0















    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?










    share|improve this question



























      0












      0








      0








      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?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 10:18









      Vít Kotačka

      512522




      512522










      asked Nov 21 '18 at 20:33









      RafiqRafiq

      14911




      14911
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer


























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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.






          share|improve this answer


























          • 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
















          1














          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.






          share|improve this answer


























          • 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














          1












          1








          1







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory