Azure / aztfexport

A tool to bring existing Azure resources under Terraform's management
https://azure.github.io/aztfexport/
Mozilla Public License 2.0
1.59k stars 184 forks source link

Disk failed to be exported, error says it does not exist (it does) #498

Closed 825i closed 1 month ago

825i commented 8 months ago

I tried to export a resource group and 74/75 resources completed successfully which is amazing. However, one disk I never suspected would be a problem, has failed. The error message below also doesn't explain much, as it is also wrong.

 /subscriptions/a56db8aa-****-****-****-84c0fa3866a1/resourceGroups/REDACTED/providers/Microsoft.Compute/virtualMachines/REDACTEDApp01/dataDisks/appdisk0

  exit status 1

  Error: Cannot import non-existent remote object

  While attempting to import an existing object to
  "azurerm_virtual_machine_data_disk_attachment.res-6", the provider detected
  that no object exists with the given id. Only pre-existing objects can be
  imported; check that the id is correct and that it is associated with the
  provider's configured region or endpoint, or use "terraform apply" to create
  a new remote object for this resource.

This resource absolutely does exist, as shown below:

Screenshot 2024-01-24 134956

Any idea why it failed to be recognized and exported? This should have been business as usual.

I'm using: aztfexport version v0.14.0(fb772ba) and: Terraform v1.7.0 on linux_amd64 with:

azure-cli                         2.56.0

core                              2.56.0
telemetry                          1.1.0

Dependencies:
msal                            1.24.0b2
azure-mgmt-resource             23.1.0b2

Python location '/opt/az/bin/python3'
Extensions directory '/home/REDACTED/.azure/cliextensions'

Python (Linux) 3.11.5 (main, Jan  8 2024, 09:08:13) [GCC 11.4.0]

Your CLI is up-to-date.

I found #440

and tried the instructions there to list the resources via AzureCLI first. Here is the failed appdisk0 resource listed:

  {
    "changedTime": "2023-09-07T13:45:29.570855+00:00",
    "createdTime": "2023-09-07T13:25:57.524618+00:00",
    "extendedLocation": null,
    "id": "/subscriptions/a56db8aa-****-****-****-84c0fa3866a1/resourceGroups/REDACTED/providers/Microsoft.Compute/disks/appdisk0",
    "identity": null,
    "kind": null,
    "location": "northeurope",
    "managedBy": "/subscriptions/a56db8aa-****-****-****-84c0fa3866a1/resourceGroups/REDACTED/providers/Microsoft.Compute/virtualMachines/REDACTEDApp01",
    "name": "appdisk0",
    "plan": null,
    "properties": null,
    "provisioningState": "Succeeded",
    "resourceGroup": "REDACTED",
    "sku": {
      "capacity": null,
      "family": null,
      "model": null,
      "name": "Premium_LRS",
      "size": null,
      "tier": "Premium"
    },
    "tags": null,
    "type": "Microsoft.Compute/disks",
    "zones": [
      "1"
    ]
  },

I tried also --use-azure-cli-cred and this too: https://github.com/Azure/aztfexport/issues/440#issuecomment-1678592219 Sadly neither have helped with this disk.

Could someone please suggest some more things to try?

magodo commented 8 months ago

@825i I've tried to create a VM with a data disk attached, and the tool can successfully export them.

Would you please try run terraform import to import the azurerm_virtual_machine_data_disk_attachment?

If this still gives the same error, then could you try to check the name of the disk appears in the VM's API model and in the resource id are case sensitively identical, as the provider has the following logic when reading (during import) the data disk attachment:

    virtualMachine, err := client.Get(ctx, id.ResourceGroup, id.VirtualMachineName, "")
    if err != nil {
        if utils.ResponseWasNotFound(virtualMachine.Response) {
            log.Printf("[DEBUG] Virtual Machine %q was not found (Resource Group %q) therefore Data Disk Attachment cannot exist - removing from state", id.VirtualMachineName, id.ResourceGroup)
            d.SetId("")
            return nil
        }

        return fmt.Errorf("loading Virtual Machine %q : %+v", id.String(), err)
    }

    var disk *compute.DataDisk
    if profile := virtualMachine.StorageProfile; profile != nil {
        if dataDisks := profile.DataDisks; dataDisks != nil {
            for _, dataDisk := range *dataDisks {
                // since this field isn't (and shouldn't be) case-sensitive; we're deliberately not using `strings.EqualFold`
                if *dataDisk.Name == id.Name {
                    disk = &dataDisk
                    break
                }
            }
        }
    }

(https://github.com/hashicorp/terraform-provider-azurerm/blob/978abcfc6b428dd7fcab6334b4b693d78aef0c8e/internal/services/compute/virtual_machine_data_disk_attachment_resource.go#L207-L229)

825i commented 8 months ago

I have no idea how to use terraform import but it seems like maybe the disk did get added as this resource (res 4)? As there is no other appdisk0 only one.

resource "azurerm_managed_disk" "res-4" {
  create_option        = var.create_option
  location             = var.location
  name                 = "appdisk0"
  resource_group_name  = var.resource_group_name
  storage_account_type = var.storage_account_type
  zone                 = var.zone
}

EDIT: Ok, now I think I understand what the problem was (maybe). There is no VM called appdisk0 there is only a logical disk called appdisk0. Or now I am just really even more confused.

I'll try and learn a bit how to use terraform import and see if that works. It's not such a big deal though because it's just a single disk.

825i commented 7 months ago

Ok so I couldn't really get anywhere with Terraform import but it does seem that the disk was scoped in anyway during the initial run. However, it was given a different resource number for some reason. Instead of res-4 it became res-8. No idea why.

So the error message was actually a false positive.

magodo commented 7 months ago

Please note that this is not the azurerm_managed_disk, but the azurerm_virtual_machine_data_disk_attachment, which is a virtual attachment resource to associate the azurerm_managed_disk and the azurerm_linux_virtual_machine resources.

magodo commented 1 month ago

:wave:

Since we've not heard back here I'm going to close this issue for the moment. Feel free to reopen if still have questions.