digital-oceandevopsterraformconfiguration-managementinfrastructure

How to pass Variables to Terraform modules via CLI or tfvars file?


(Please note: after receiving initial answers, this issue seems to not be just an issue with passing the variables, but with modularizing my configurations, note at the bottom where I hardcode the values yet the UI prompts me to provide the values)

Code example here

I've got a project I've broken into the following directory structure

master.tf
variables.tfvars
- providers/
-- digital_ocean/
--- digital_ocean.tf
--- variables.tf
-- cloud_flare/
--- cloud_flare.tf
--- variables.tf
- management/
-- jenkins/
--- jenkins-master.tf

I'm attempting to pass my Digital Ocean and Cloudflare tokens as variables, to their respective modules. Everything below the root directory is loaded into master.tf as a module.

I have the following in my varaibles.tfvars file:

cloudflare_email  ="service@email.com"
cloudflare_token  ="TOKEN_STRING"
do_token          ="${DO_PAT}"

The following lines appear in my master.tf

variable "do_token" {}
module "digital_ocean" {
    source          = "./providers/digital_ocean"
    token           = "${var.do_token}"
}


variable "cloudflare_email" {}
variable "cloudflare_token" {}
module "cloud_flare" {
    source          = "./providers/cloud_flare"
    email = "${var.cloudflare_email}"
    token = "${var.cloudflare_token}"
}

My digital_ocean module looks like

variable "token" {}

provider "digitalocean" {
  token = "${var.token}"
}

and the cloudflare provider looks like

variable "email" {}
variable "token" {}

provider "CloudFlare" {
    email = "${var.email}"
    token = "${var.token}"
}

Setting up my jenkins master server on DO

resource "digitalocean_droplet" "jenkins-master" {
...
}

From the command line I'm running terraform apply -var-file="variables.tfvars"

or I've also tried passing them via the CLI like so..

terraform apply \
  -var "cloudflare_email=service@email.com" \
  -var "cloudflare_token=TOKEN_STRING" \
  -var "do_token=${DO_PAT}"

With the above declarations, it will send me into UI mode and prompt me for those variables rather than reading them automatically. I've replicated this behavior on both Terraform v0.9.8 and v0.9.10.

Before I started breaking everything out into separate modules, passing in variables presented no issues.

I've tried pulling the provider declarations into master.tf to see if there were any weird behaviors with modularizing them, with the same behavior.

I also tried hard coding the values into the provider declarations and am experiencing the same behaviors.


Solution

  • Your variables.tfvars file should be named terraform.tfvars.

    Per the docs:

    If a terraform.tfvars file is present in the current directory, Terraform automatically loads it to populate variables. If the file is named something else, you can use the -var-file flag directly to specify a file. These files are the same syntax as Terraform configuration files. And like Terraform configuration files, these files can also be JSON.

    If you want to use your own filenaming convention, you can set an alternative tfvars file with the -var-file flag like this (per the linked docs):

    $ terraform plan \
    -var-file="secret.tfvars" \
    -var-file="production.tfvars"
    

    For the CLI, you should quote only the value of the variable, like so:

    terraform apply \ 
    -var cloudflare_email="service@email.com" \ 
    -var cloudflare_token="TOKEN_STRING" \ 
    -var do_token="${DO_PAT}"