0
votes

Having an issue with Terraform modules and variables and I am at a loss as to what I am doing wrong.

I have a folder structure that looks like this;

Accounts
    |_____Account1
    |        Main.tf
    |        terraform.tfvars
    |_____Account2
    |_____Account3
    |_____Modules
             |________VPC
                      Main.tf
                      Variables.tf

In my modules folder I have my main.tf and variables.tf, under the accounts I also have my main.tf (calling module vpc) and terrform.tfvars.

How can I use my terraform.tfvars to pass secure credentials to my main.tf, within my accounts folder?

Variables in my Variables.tf within the VPC module look like so;

variable "aws_access_key" {
  default = ""
 }

Within my account folders, in the Main.tf I am trying to call tfvars this way;

variable "aws_access_key" {}

module "VPC" {
source = "/Accounts/Modules/VPC"
aws_access_key = "${var.aws_access_key}"
}

Can run terraform init without any issues but trying to run terraform plan, it just comes up in red and fails to run. This does work if I enter the variables into my main.tf within the account folder manually. But I want to strip out anything sensitive into a .tfvars file, that will end up else where.

I hope I am doing something obviously wrong! Have also tried the -var-file=terraform.tfvars switch from within account1's folder.

Any idea's would be great. As everything I read tend to imply this should be working.

Thanks Stephen

enter image description here

1
I recreated the setup you have above and was able to get it to plan without issue. What is the specific error message that terraform is giving you?jstill
Thanks jstill - least I am not crazy! So when I try and run this I just get the terraform help output in red, as if I am entering the wrong syntax - all I am doing is running this in one of the account folders - terraform planStevieHyperB
Can you include a screenshot showing the terraform plan line in your console and the first few lines of the output after? I feel like I've had a client run into something like this before, hoping seeing that might jog my memory.jstill
Yes, please ads the error message, but not as a screenshot but as text, please.StephenKing
Definitely nothing jumps out as wrong. Are you using the latest version? If not, try upgrading and see if that changes anything (warning though: if you're using shared state it'll upgrade the state, so everyone would have to upgrade). Also, make sure you don't have any random non-printing characters floating around in any of the files. That was the issue I'd seen before that I couldn't remember. Maybe just verify character encoding on all the tf files, to be sure.jstill

1 Answers

0
votes

Just ran this from a fresh folder - taking only part of my config and is running fine - so must be something in the file. If anyone else comes across this