Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

azure - Terraform, (AzureRM) Can you have if, else, else (conditional situations) for resource deployments?

I am using Azure DevOps with Terraform to deploy multiple environments within my subscriptions and I am leveraging the same main.tf, variables.tf, and tfvars for all my environments (3 environments in total). Within the tf files and tfvars file, I am passing in DevOps variables within Variable Groups (Azure DevOps specific) to identify different variables and values. Say for example, I want to build 3 different subnets with the same resource (1 in each subscription):

Main.tf:

resource "azurerm_subnet" ""example" { 
  for_each             = var.is_env_one ? var.subnet_range_one : var.subnet_range_two : var.subnet_range_three
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name

  name                 = each.value["name"]
  address_prefixes     = each.value["address_prefixes"]
}

Variables.tf:

variable "is_env_one" {
  description = "Boolean on if this is environment 1 or not"
  default = true 
}
variable "is_env_two" {
  description = "Boolean on if this is environment 2 or not"
  default = true 
}
variable "is_env_three" {
  description = "Boolean on if this is environment 3 or not"
  default = true 
}
variable "subnet_range_one" {
  description = "tfvars file will dictate # of subnets and values"
}
variable "subnet_range_two" {
  description = "tfvars file will dictate # of subnets and values"
}
variable "subnet_range_three" {
  description = "tfvars file will dictate # of subnets and values"
}

tfvars information (example of 1 of the ranges configs):

subnet_range_one = {
  subnet_1 = {
    name             = "{[_subnet-devops-name_]}" #Azure DevOps Variable that dictates the value
    address_prefixes = "{[_subnet-devops-address-prefix_]}" #Azure DevOps Variable that dictates the value
  }
}

Is there a way for me to write my code to differentiate between the environments using DevOps variables? Aka, can I use a conditional format to say either pick a, b or c three options)?

question from:https://stackoverflow.com/questions/65909285/terraform-azurerm-can-you-have-if-else-else-conditional-situations-for-re

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the easiest and most scalable way (if you want to add new env later) is to use one variable for it.

Something as the following (example only):

variable "env_subnets" {

  default = {

      env1 = [
          {
              name = name11
              address_prefixes = prefix11
          },
          {
              name = name12
              address_prefixes = prefix12
          }          
      ],
      env2 = [
          {
              name = name21
              address_prefixes = prefix21
          },
          {
              name = name22
              address_prefixes = prefix22
          }          
      ],
      env3 = [
          {
              name = name31
              address_prefixes = prefix31
          },
          {
              name = name32
              address_prefixes = prefix32
          }          
      ]
  }
}


resource "azurerm_subnet" "example" { 

  for_each             = {for idx, val in var.env_subnets["env1"]: idx => val}

  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name

  name                 = each.value["name"]
  address_prefixes     = each.value["address_prefixes"]
}

In the above you just use different key in var.env_subnets["env1"] to create subnets for different environments.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...