Kvad
Kvad

Reputation: 115

List to string in Terraform for ARM template use

Trying to use a list(string) to string for subscriptions in my tfvars into an TF ARM deployment.

I want the following:

"scope": [
"/subscriptions/0-1", 
"/subscriptions/0-2"
]

I've attempted the following with no success:

"scope": [
    ${join(", ", each.value.subscriptions)} 
]

I'm getting the following error Error: expanding template_content: invalid character '/' looking for beginning of value

dev.tfvars

schedules = {
  01 = {
    name                   = "01" 
    subscriptions          = ["/subscriptions/0-1", "/subscriptions/0-2"]
  }

  02 = {
    name                   = "02" 
    subscriptions          = ["/subscriptions/0-1", "/subscriptions/0-2"]
  }

}

variables.tf

variable "schedules" {
  type = map(object({
    name   = string
    subscriptions          = list(string)
  }))
}

updates.tf

resource "azurerm_resource_group_template_deployment" "updates" {
      for_each            = var.schedules
      name                = each.key 
      resource_group_name = var.rg_name
      deployment_mode     = "Incremental"
      debug_level         = "requestContent"
      template_content = <<TEMPLATE


{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",        
       

          "resources": [
            {
                     
            
                "apiVersion": "2017-05-15-preview",
                "type": "Microsoft.Automation/automationAccounts/softwareUpdateConfigurations",
                "name": "[concat(parameters('automationAccounts_automation_account_name'), '/test-schedule')]",
                "properties": {
                    "updateConfiguration": {
                        "operatingSystem": "Windows",
                        "duration": "PT2H",
                        "windows": {
                            "excludedKbNumbers": [],
                            "includedUpdateClassifications": "Critical, Security",
                            "rebootSetting": "IfRequired"
                        },
                        "targets": {
                          "azureQueries": [
                            {
                              "scope": [
                                
                                
                                ${join("\", \"", each.value.subscriptions)}
                                

                              ],
                              "tagSettings": {
                                "tags": {
                                  "updates": [
                                    "test"
                                  ]
                                },
                                "filterOperator": "All"
                              }
                            }
                          ]
                        }
                    },
                    "scheduleInfo": {
                        "isEnabled": "true",
                        "frequency": "Month",
                        "interval": "1",
                        "startTime": "2022-01-18T01:01:00+11:00",
                        "timeZone":  "Australia/Sydney",
                        "advancedSchedule": {
                            "monthlyOccurrences": [
                              {
                                  "occurrence": "Saturday",
                                  "day": "2"
                              }
                            ]
                          }
                
                    }
                }
            } ]
    }


TEMPLATE
     
}

Upvotes: 0

Views: 432

Answers (2)

Tobias Thieron
Tobias Thieron

Reputation: 301

You inserted the double quotes between each element, but they should be encasing each element.

${join(", ", [ for sub in each.value.subscriptions: "\"${sub}\"" ])} 

Upvotes: 1

Marcin
Marcin

Reputation: 238209

Usually you would use jsonencode instead of join in cases like yours:

resource "azurerm_resource_group_template_deployment" "updates" {
      for_each            = var.schedules
      name                = each.key 
      resource_group_name = var.rg_name
      deployment_mode     = "Incremental"
      debug_level         = "requestContent"
      template_content = <<TEMPLATE


{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",        
       

          "resources": [
            {
                     
            
                "apiVersion": "2017-05-15-preview",
                "type": "Microsoft.Automation/automationAccounts/softwareUpdateConfigurations",
                "name": "[concat(parameters('automationAccounts_automation_account_name'), '/test-schedule')]",
                "properties": {
                    "updateConfiguration": {
                        "operatingSystem": "Windows",
                        "duration": "PT2H",
                        "windows": {
                            "excludedKbNumbers": [],
                            "includedUpdateClassifications": "Critical, Security",
                            "rebootSetting": "IfRequired"
                        },
                        "targets": {
                          "azureQueries": [
                            {
                              "scope": ${jsonencode(each.value.subscriptions)},
                              "tagSettings": {
                                "tags": {
                                  "updates": [
                                    "test"
                                  ]
                                },
                                "filterOperator": "All"
                              }
                            }
                          ]
                        }
                    },
                    "scheduleInfo": {
                        "isEnabled": "true",
                        "frequency": "Month",
                        "interval": "1",
                        "startTime": "2022-01-18T01:01:00+11:00",
                        "timeZone":  "Australia/Sydney",
                        "advancedSchedule": {
                            "monthlyOccurrences": [
                              {
                                  "occurrence": "Saturday",
                                  "day": "2"
                              }
                            ]
                          }
                
                    }
                }
            } ]
    }


TEMPLATE
     
}

Upvotes: 1

Related Questions