How to merge 2 tuples?

Iā€™m still getting to grips with Terraform so forgive me if this is a dumb question!

I am trying to create a variable which will eventually be used to add multiple disks to a new VM in azure.

I have a yaml file which contains a variable for needed data disks alongside other VM properties:

vms:
  vm1:
    server_data_disks:
       - name: "datadisk1"
         data_disk_size_gb: 128
         data_disk_storage_account_type: "Standard_LRS"
         caching: "ReadWrite"
       - name: "datadisk2"
         data_disk_size_gb: 128
         data_disk_storage_account_type: "Standard_LRS"
         caching: "ReadWrite" 

I then have a variable defined to store these properties:

variable "server_data_disks" {
  type = list(object({
    data_disk_size_gb                           = number
    data_disk_storage_account_type = string
    caching                                             = string
    name                                                 = string
  }))
  description = "Additional drives to be added to the machine."
  default = null
}

There will be cases where additional disks need to be added (such as when a SQL Server is being requested). There will also be occasions when 0 additional disks are required. Due to this I have 2 local variables which define additional disks that need to be added under certain conditions:

locals {
  server_data_disks = var.server_data_disks != null ? [for disk in var.server_data_disks : {
      data_disk_size_gb              = disk.data_disk_size_gb
      data_disk_storage_account_type = disk.data_disk_storage_account_type
      caching                        = disk.caching
      name                           = disk.name
    }
  ] : null

  server_sql_disks = [
    {
      "data_disk_size_gb" = "80",
      "data_disk_storage_account_type" = "Standard_LRS",
      "caching" = "ReadWrite",
      "name" = "datadisk5",
    },
    {
      "data_disk_size_gb" = "25",
      "data_disk_storage_account_type" = "Standard_LRS",
      "caching" = "ReadWrite",
      "name" = "datadisk6",
    }
]

Next, I want to merge these 2 variables into a single variable that can be passed to an azurerm_managed_disk resource.

If I try to merge the variables using the code:

merge(local.server_data_disks,local.server_sql_disks)

I get an error:

Error: Error in function call
ā”‚     ā”‚ while calling merge(maps...)
ā”‚     ā”‚ local.server_data_disks is tuple with 2 elements
ā”‚     ā”‚ local.server_sql_disks is tuple with 2 elements
ā”‚ Call to function "merge" failed: arguments must be maps or objects, got
ā”‚ "tuple".

Can anyone advise on how a can merge these 2 variables together?

Thanks.

The magic you need for this is concat.

PS C:\Development\discuss\68098> terraform console
> concat(local.server_data_disks, local.server_sql_disks)
[
  {
    "caching" = "ReadWrite"
    "data_disk_size_gb" = 128
    "data_disk_storage_account_type" = "Standard_LRS"
    "name" = "datadisk1"
  },
  {
    "caching" = "ReadWrite"
    "data_disk_size_gb" = 128
    "data_disk_storage_account_type" = "Standard_LRS"
    "name" = "datadisk2"
  },
  {
    "caching" = "ReadWrite"
    "data_disk_size_gb" = "80"
    "data_disk_storage_account_type" = "Standard_LRS"
    "name" = "datadisk5"
  },
  {
    "caching" = "ReadWrite"
    "data_disk_size_gb" = "25"
    "data_disk_storage_account_type" = "Standard_LRS"
    "name" = "datadisk6"
  },
]
>

As you have found, merge takes an arbitrary number of maps or objects, and returns a single map or object that contains a merged set of elements from all arguments. Whereas, what you have here is two lists.

Hope that helps!

Happy Terraforming

This worked perfectly, thanks for your help!

1 Like