Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Terraform convert single string to list or set

Below are the few examples of Terraform on to converting single string to array of list or set. This code I was wanted in my one of my Azure automation task. In this example I have defined a variable justice_leage with same default value..

For conversion, In the first scenario I can use split() function to create list. In the second option is to put variable inside toset([]). Lastly you can use simple square bracket [ ]. There are other some of examples for map as well shown to convert keys or values to convert to list in the below code.

variable "justice_leage" {
  type = string
  default = "justice_league"
}

output "superman" {
  value = split(",", var.justice_leage)
}

output "wonder_woman" {
  value = toset([var.justice_leage])
}

output "dc_to_object_or_string" {
  value = jsonencode(var.justice_leage)
}

#######################

variable "avenger" {
  type = map(string)
  default = {
    "Bruce_Banner" = "Hulk"
    "Wanda_Maximoff" = "Scarlet_Witch"
  }
}

output "avengers_to_list" {
  value = values(var.avenger)
}

output "avengers_to_set" {
  value = toset(values(var.avenger))
}

output "avengers_map_to_object_or_string" {
  value = jsonencode(var.avenger)
}

Download Terraform_convert_string_to_map_list.tf here or it is also available on github.com.

Below is the output of the result output after applying terraform configuration.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
terraform apply --auto-approve  

Changes to Outputs:
  + avengers_map_to_object_or_string = jsonencode(
        {
          + Bruce_Banner   = "Hulk"
          + Wanda_Maximoff = "Scarlet_Witch"
        }
    )
  + avengers_to_list                 = [
      + "Hulk",
      + "Scarlet_Witch",
    ]
  + avengers_to_set                  = [
      + "Hulk",
      + "Scarlet_Witch",
    ]
  + dc_to_object_or_string           = "\"justice_league\""
  + superman                         = [
      + "justice_league",
    ]
  + wonder_woman                     = [
      + "justice_league",
    ]

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

avengers_map_to_object_or_string = "{\"Bruce_Banner\":\"Hulk\",\"Wanda_Maximoff\":\"Scarlet_Witch\"}"
avengers_to_list = tolist([
  "Hulk",
  "Scarlet_Witch",
])
avengers_to_set = toset([
  "Hulk",
  "Scarlet_Witch",
])
dc_to_object_or_string = "\"justice_league\""
superman = tolist([
  "justice_league",
])
wonder_woman = toset([
  "justice_league",
])

Useful Articles
Terraform variable type list with for_each for loop examples
Terraform refactoring moved block example
Terraform create Azure Virtual Machines from map of objects
Terraform variable validation example
Configure Azure Storage Account Blob as Terraform backend to store tfstate file Examples of most used general purpose terraform functions
Create storage account and Service Principal using PowerShell for Terraform Azure Backend
Unlocking TF State File on Azure backend with PowerShell and Terraform Force-Unlock Command
Terraform variable precedence and priority
Terraform filter map and list object with if condition in for_each loop examples
Terraform Azure function app with private endpoint and storage account
Terraform module Azure function app with private endpoint and storage account
Terraform passing different credentials to different subscriptions with provider alias
Terraform Azure provider alias passing credentials and configuration in module resources
Terraform Azure provider passing multiple alias environment and credentials in child module

Go Back

Comment

Blog Search

Page Views

12085965

Follow me on Blogarama