# Azure vm snapshot using terraform throwing error

**URL:** <https://discuss.hashicorp.com/t/azure-vm-snapshot-using-terraform-throwing-error/43007>\
**Category:** Azure\
**Tags:** hcp-terraform, azure\
**Created:** [August 10, 2022, 10:40am UTC](https://discuss.hashicorp.com/t/azure-vm-snapshot-using-terraform-throwing-error/43007 "2022-08-10T10:40:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![amentee](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/amentee/32/29977_2.png) [@amentee](https://discuss.hashicorp.com/u/amentee)\
**Post date:** [August 10, 2022, 10:40am UTC](https://discuss.hashicorp.com/t/azure-vm-snapshot-using-terraform-throwing-error/43007/1 "2022-08-10T10:40:12Z")

</div>

I have written a small terraform script to take snapshot of two VM’s sitting on Azure. I have created two lists with resource group details and OS Disk name. Below is the necessary files.

main.tf

```auto
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~> 3.0.2"
    }
  }

  required_version = ">= 1.1.0"
}

provider "azurerm" {
  features {}
}

data "azurerm_managed_disk" "existing" {
  for_each = zipmap(var.cloud_resource_group_list,var.cloud_vm_os_disk_name)
  name = each.value
  resource_group_name = each.key
}

resource "azurerm_snapshot" "example" {
  name = "snapshot"
  for_each = ([for i in data.azurerm_managed_disk.existing: zipmap(i.resource_group_name, i.name)])
  location = data.azurerm_managed_disk.existing[each.key].location
  resource_group_name = data.azurerm_managed_disk.existing[each.key]
  create_option = "Copy"
  source_uri = data.azurerm_managed_disk.existing[each.value].id
}

```

variables.tf

```auto
variable "cloud_resource_group_list" {
  description = "VM resource group name"
  type = list(string)
}

variable "cloud_vm_os_disk_name" {
  description = "VM OS disk names"
  type = list(string)
}

```

terraform.tfvars

```auto
cloud_resource_group_list = ["rg1", "rg2"]
cloud_vm_os_disk_name = ["disk1", "disk2"]

```

terraform validate runs sucessfully. When I run terraform apply the first resource group is read sucessfully but it fails for second resource group. Below is the error.

```auto
 terraform apply
data.azurerm_managed_disk.existing["rg1"]: Reading...
data.azurerm_managed_disk.existing["rg1"]: Reading...
data.azurerm_managed_disk.existing["disk1"]: Read complete after 1s 
╷
│ Error: Managed Disk: (Disk Name "disk2" / Resource Group "rg2") was not found
│
│ with data.azurerm_managed_disk.existing["rg2"],
│ on main.tf line 22, in data "azurerm_managed_disk" "existing":
│ 22: data "azurerm_managed_disk" "existing" {

```

Both rg2 and disk2 exists on azure portal. Please help me where am I wrong and why its not working.
