0

I have below two environments terraform files

  1. devenv_variables.tfvars
  2. testenv_variables.tfvars

Here is devenv_variables.tfvars

 location            = "westeurope"
 resource_group_name = "devenv-cloudresources-rg"

Here is testenv_variables.tfvars

 location            = "westeurope"
 resource_group_name = "testenv-cloudresources-rg"

Here is my main.tf

  # Configure the Microsoft Azure Provider
  provider "azurerm" {
       version = "=2.0.0"

       features {}

       subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

   }

   provider "azurerm" {
       alias  = "testenv"
       subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }


    # Create a resource group
    resource "azurerm_resource_group"  {
        provider = "azurerm.testenv" //How do I pass provider based on variables here?
        name     = "${var.resource_group_name}" 
        location = "${var.location}"
      }

My requirement is, based on passed tfvar file as parameter, it should choose the subscription.

   terraform apply  -var-file="devenv_variables.tfvars"

when I type below command resource shall create in test environment

   terraform apply  -var-file="testenv_variables.tfvars"

I think, I need to define client id, and password to login to respective subscriptions.

kudlatiger
  • 3,028
  • 8
  • 48
  • 98

2 Answers2

1

tfvars files should only contain the values of variables. The declaration of variables should happen in regular tf files.

variables.tf

    variable "location" { 
      type = "string"
      description = "The azure location where the resources is created"
    }

devenv_variables.tfvars

location     = "West Europe"

This tutorial can also help you with some more information and examples.

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • Helped in defining variables, However, could not make it work. I have updated the question. – kudlatiger Apr 12 '20 at 09:53
  • 1
    This is a completely new question now. You should undo the edit and ask it as a separate question. Ideally, you could also accept this answer since it solved the original problem. – Alex AIT Apr 12 '20 at 10:01
  • This tutorial shows you how you can switch between subscriptions easily (altough not with tfvars files): https://azurecitadel.com/automation/terraform/lab5/ – Alex AIT Apr 12 '20 at 10:02
  • do you have any simple article/sample links? The link you are sharing is for expert DevOps professionals, I am new in to terraform. – kudlatiger Apr 12 '20 at 10:06
  • sure, I will undo the question. – kudlatiger Apr 12 '20 at 10:07
1

All you have to do this just make same variable name in both files. when you run the command terraform plan–var-file='variable's_file_name' & terraform apply–var-file='variable's_file_name' on that point terraform will get different value from different file.

for demo

variable.tf (contain all variables)

variable "resource_group" {
type = string
description = "Resource Group"
default = ""
}

dev.tfvars (contain development variable's values)

resource_group="name_of_my_resource_group_dev"

prod.tfvars (contain production variable's values)

resource_group="name_of_my_resource_group_prod"

now when you run command terraform plan–var-file='dev.tfvars' it take the value from dev.tfvars file.

Ali Hassan
  • 779
  • 10
  • 14