1

I am trying to run a Terraform deployment via a Shell script where within the Shell script I first dynamically collect the access key for my Azure storage account and assign it to a variable. I then want to use the variable in a -var assignment on the terraform command line. This method works great when configuring the backend for remote state but it is not working for doing a deployment. The other variables used in the template are being pulled from a terraform.tfvars file. Below is my Shell script and Terraform template:

Shell script:

#!/bin/bash
set -eo pipefail

subscription_name="Visual Studio Enterprise with MSDN"
tfstate_storage_resource_group="terraform-state-rg"
tfstate_storage_account="terraformtfstatesa"

az account set --subscription "$subscription_name"
tfstate_storage_access_key=$(
  az storage account keys list \
  --resource-group "$tfstate_storage_resource_group" \
  --account-name "$tfstate_storage_account" \
  --query '[0].value' -o tsv
)

echo $tfstate_storage_access_key

terraform apply \
  -var "access_key=$tfstate_storage_access_key"

Deployment template:

provider "azurerm" {
  subscription_id = "${var.sub_id}"
}

data "terraform_remote_state" "rg" {
  backend = "azurerm"

  config {
    storage_account_name = "terraformtfstatesa"
    container_name       = "terraform-state"
    key                  = "rg.stage.project.terraform.tfstate"
    access_key           = "${var.access_key}"
  }
}

resource "azurerm_storage_account" "my_table" {
  name                     = "${var.storage_account}"
  resource_group_name      = "${data.terraform_remote_state.rg.rgname}"
  location                 = "${var.region}"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

I have tried defining the variable in my terraform.tfvars file:

storage_account = "appastagesa"

les_table_name = "appatable

region = "eastus"

sub_id = "abc12345-099c-1234-1234-998899889988"

access_key = ""

The access_key definition appears to get ignored.

I then tried not using a terraform.tfvars file, and created the variables.tf file below:

variable storage_account {
  description = "Name of the storage account to create"
  default     = "appastagesa"
}

variable les_table_name {
  description = "Name of the App table to create"
  default     = "appatable"
}

variable region {
  description = "The region where resources will be deployed (ex. eastus, eastus2, etc.)"
  default     = "eastus"
}

variable sub_id {
  description = "The ID of the subscription to deploy into"
  default     = "abc12345-099c-1234-1234-998899889988"
}

variable access_key {}

I then modified my deploy.sh script to use the line below to run my terraform deployment:

terraform apply \
  -var "access_key=$tfstate_storage_access_key" \
  -var-file="variables.tf"

This results in the error invalid value "variables.tf" for flag -var-file: multiple map declarations not supported for variables Usage: terraform apply [options] [DIR-OR-PLAN] being thrown.

6
  • sorry, but what is not working? any errors? Commented Feb 18, 2018 at 2:57
  • I'm not sure if that's a reduced example but you still need to define the variables you use in variable "var_name" {} style. Commented Feb 18, 2018 at 10:35
  • @ChandanNayak I am unable to execute the terraform command line where I pass in a variable and use a variables file at the same time. Commented Feb 18, 2018 at 11:59
  • @ydaetskcoR I forgot about defining the variable as you mentioned but it did not seem to change anything. I edited the original post to reflect how I tried adding the declaration and the outcomes. Commented Feb 18, 2018 at 12:00
  • looks like your access_key is getting the value as a map instead of string, what does your "echo $tfstate_storage_access_key" prints ? Commented Feb 18, 2018 at 13:32

1 Answer 1

1

After playing with this for hours...I am almost embarrassed as to what the problem was but I am also frustrated with Terraform because of the time I wasted on this issue.

I had all of my variables defined in my variables.tf file with all but one having default values. For the one without a default value, I was passing it in as part of the command line. My command line was where the problem was. Because of all of the documentation I read, I thought I had to tell terraform what my variables file was by using the -var-file option. Turns out you don't and when I did it threw the error. Turns out all I had to do was use the -var option for the variable that had no defined default and terraform just automagically saw the variables.tf file. Frustrating. I am in love with Terraform but the one negative I would give it is that the documentation is lacking.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.