Blue Clouds
Blue Clouds

Reputation: 8153

Terraform.tf parameterize or template for different environment

This is the terraform.tf file. I want to give different values based on the environment for 'name' field. How can I do it?

provider "azurerm" {
    version = "=2.46.0"
    features {}
}

terraform {
    backend "azurerm" {
        resource_group_name = "rgtstate"
        storage_account_name = "adststorage"
        container_name = "terraform.tfstate"
  #      access_key = ""
    }
}

data "azurerm_client_config" "current" {}

resource "azurerm_resource_group" "resourcegroup" {
    name = "sk-terraform-rg"
    location = "west europe"
}


resource "azurerm_data_factory" "example" {

  name                = "adfSB"
  location            = azurerm_resource_group.resourcegroup.location
  resource_group_name = azurerm_resource_group.resourcegroup.name
}

resource "azurerm_data_factory_integration_runtime_self_hosted" "example" {
  name                = "VMSHIRSB"
  data_factory_name   = azurerm_data_factory.example.name
  resource_group_name = azurerm_resource_group.resourcegroup.name
}

Upvotes: 0

Views: 1146

Answers (1)

Nancy Xiong
Nancy Xiong

Reputation: 28204

To give different values based on the environment for the 'name' field, if you want the same configuration main.tf file for defining multiple environments, you can declare the environment variable "env" {} then use the var.env prefix on the name filed in each resource in different environments. You can create each resource dynamically by passing a different environment variable.

resource "azurerm_resource_group" "resourcegroup" {
    name = "${var.env}-terraform-rg"
    location = "west europe"
}

resource "azurerm_data_factory" "example" {

  name                = "${var.env}-adfSB"
  location            = azurerm_resource_group.resourcegroup.location
  resource_group_name = azurerm_resource_group.resourcegroup.name
}

resource "azurerm_data_factory_integration_runtime_self_hosted" "example" {
  name                = "${var.env}-VMSHIRSB"
  data_factory_name   = azurerm_data_factory.example.name
  resource_group_name = azurerm_resource_group.resourcegroup.name
}

If you want to create separate configurations file for different environments, you can create directories. When you are finished separating these environments into directories, your file structure should look like the one below.

.
├── assets
│   ├── index.html
├── prod
│   ├── main.tf
│   ├── variables.tf
│   ├── terraform.tfstate
│   └── terraform.tfvars
└── dev
   ├── main.tf
   ├── variables.tf
   ├── terraform.tfstate
   └── terraform.tfvars

In this scenario, you will have duplicate Terraform code in each directory.

If you want to use the same Terraform code but have different state files, you can use workspace-separated environments. You could define variable "dev_prefix" {} or variable "prod_prefix" {}

Your directory will look similar to the one below.

.
├── README.md
├── assets
│   └── index.html
├── dev.tfvars
├── main.tf
├── outputs.tf
├── prod.tfvars
├── terraform.tfstate.d
│   ├── dev
│   │   └── terraform.tfstate
│   ├── prod
│   │   └── terraform.tfstate
├── terraform.tfvars
└── variables.tf

In this scenario, if you want to be able to declare variables that give us selection control, you can refer to this. Read here for more details about workspace and modules.

Upvotes: 1

Related Questions