Terraform: How to pass a list of managed_disk in RSV module

58 Views Asked by At

In Terraform how do I tell azurerm_site_recovery_replicated_vm that I have more than one disk to replicate?

For simplicity, I have a virtual machine with 3 disks (1 OS disk and 2 Data disks) on the west coast that I want to set up Azure Site Recovery (ASR) for replication to the east coast.

This is what I have so far.

The folder structure to contain the Terraform code.

Modules
  KeyVault
  PublicIP
  RecoverServiceVault
  BunchOfOtherAzureResources
Project01
Project02
Project03
Project0x

In Project01, In main.tf:

data "azurerm_managed_disk" "os_disk" {
  name                = local.osDiskName
  resource_group_name = data.azurerm_virtual_machine.host.resource_group_name
}

module "AddRecoveryServiceVault" {
  source                       = "../modules/RecoveryServiceVault"
  rgDr                         = azurerm_resource_group.dr.name
  rgAsr                        = azurerm_resource_group.asr.name
  .  <bunch of other parameters passed to the module>
  .  <bunch of other parameters passed to the module>
  
  sourceDiskId                 = data.azurerm_managed_disk.os_disk.id

  .  <bunch of other parameters passed to the module>
  .  <bunch of other parameters passed to the module>
  tags                         = local.tags
}

Currently, the module ‘RecoveryServiceVault’ takes one disk. In variables.tf:

variable "sourceDiskId" {
  type = string
  description = "the disk Id of the virtual machine being protected in Source (Primary)"
}

In main.tf:

resource "azurerm_site_recovery_replicated_vm" "vm-replication" {
  name                                      = var.sourceHostName
  resource_group_name                       = var.rgDr
  recovery_vault_name                       = azurerm_recovery_services_vault.vault.name
  source_recovery_fabric_name               = azurerm_site_recovery_fabric.primary.name
  source_vm_id                              = var.sourceHostId
  recovery_replication_policy_id            = azurerm_site_recovery_replication_policy.policy.id
  source_recovery_protection_container_name = azurerm_site_recovery_protection_container.primary.name

  target_resource_group_id                = var.rgAsrId
  target_recovery_fabric_id               = azurerm_site_recovery_fabric.secondary.id
  target_recovery_protection_container_id = azurerm_site_recovery_protection_container.secondary.id

  managed_disk {
    disk_id                    = var.sourceDiskId
    staging_storage_account_id = var.storageAccountId
    target_resource_group_id   = var.rgAsrId
    target_disk_type           = "Premium_LRS"
    target_replica_disk_type   = "Premium_LRS"
  }

  network_interface {
    source_network_interface_id   = var.sourceNicId
    target_subnet_name            = azurerm_subnet.secondary.name
    recovery_public_ip_address_id = azurerm_public_ip.secondary.id
  }

  depends_on = [
    azurerm_site_recovery_protection_container_mapping.container-mapping,
    azurerm_site_recovery_network_mapping.network-mapping,
  ]
}

My guess is that you’ll need to do this. Create a list of Object of (managed_disk) in Project01 main.tf

module "AddRecoveryServiceVault" {
  source = "../modules/RecoveryServiceVault"
  rgDr   = azurerm_resource_group.dr.name
  rgAsr  = azurerm_resource_group.asr.name
  .  <bunch of other parameters passed to the module>
  .  <bunch of other parameters passed to the module>

  managed_disk = [{
    disk_id                    = "osDisk01Id"
    staging_storage_account_id = "stId"
    target_resource_group_id   = "rgId"
    target_disk_type           = "Premium_LRS"
    target_replica_disk_type   = "Premium_LRS"
    },
    {
      disk_id                    = "dataDisk01Id"
      staging_storage_account_id = "stId"
      target_resource_group_id   = "rgId"
      target_disk_type           = "Premium_LRS"
      target_replica_disk_type   = "Premium_LRS"
    },
    {
      disk_id                    = "dataDisk02Id"
      staging_storage_account_id = "stId"
      target_resource_group_id   = "rgId"
      target_disk_type           = "Premium_LRS"
      target_replica_disk_type   = "Premium_LRS"
  }]
  .  <bunch of other parameters passed to the module>
  .  <bunch of other parameters passed to the module>

  tags = local.tags
}

Passed the list of objects (managed_disk) to the module ‘RecoveryServiceVault’ of azurerm_site_recovery_replicated_vm

In variables.tf of the RecoverServiceVault module

variable "managed_disk" {
  type =  list(object({
    disk_id                    = string
    staging_storage_account_id = string
    target_resource_group_id   = string
    target_disk_type           = string
    target_replica_disk_type   = string
  }))
}

In the main.tf of the RecoverServiceVault module, I am unable to pass the list of objects to managed_disk

UnableToPassListOfManaged_Disks

Any help is greatly appreciated!

0

There are 0 best solutions below