Azure / terraform-provider-azapi

Terraform provider for Azure Resource Manager Rest API
https://registry.terraform.io/providers/Azure/azapi/latest
Mozilla Public License 2.0
181 stars 48 forks source link

Terraform import fails on AzAPI resource output #525

Closed jchancellor-ms closed 1 month ago

jchancellor-ms commented 4 months ago

I've created an AVS AVM module using AzAPI. Because installs of AVS can take 4-6 hours, it is not uncommon for the install to drop a connection requiring a Terraform import to re-initiate the installation of the rest of the module. Using version 1.13.1 with the HCL features enabled, I get the following errors when running terraform import.

│   on ../../outputs.tf line 10, in output "identity":
│   10:   value = var.managed_identities.system_assigned ? azapi_resource.this_private_cloud.output.identity : null
│     ├────────────────
│     │ azapi_resource.this_private_cloud.output is object with no attributes
│
│ This object does not have an attribute named "identity".

However, if I create a data azapi resource using the same configuration then the output works fine and shows the identity element. image

I suspect this is a bug in how terraform import interacts with AzAPI outputs in the latest version.

Full example of the code can be found here - https://github.com/Azure/terraform-azurerm-avm-res-avs-privatecloud/blob/main/examples/full/main.tf

ms-henglu commented 4 months ago

Hi @jchancellor-ms ,

Thank you for taking time to report this issue.

The terraform doesn't support reading the configuration when import a resource. So the azapi_resource couldn't know the response_export_values and set the output. We're working on improving the default behavior for the output field in the next major release.

Here're some workarounds for this case:

  1. Recommended: Use azapi_resource.this_private_cloud.identity, the identity block will be set during import.

  2. Add response_export_values = ["identity"] in the config, and apply it. Then you could use the values in the output field.

jchancellor-ms commented 3 months ago

@ms-henglu - I was able to use option 1 to bypass the identity challenge, but I'm also getting This object does not have an attribute named "properties" as an error. I tried explicitly setting the response_export_values to ["properties"] but that did not correct the error. Is there a way to suppress import errors on the properties block until the next major release?

ms-henglu commented 3 months ago

Hi @jchancellor-ms

Is there a way to suppress import errors on the properties block until the next major release?

Yes, you could use the try function to provide a default value if error happens. More details: https://developer.hashicorp.com/terraform/language/functions/try

ms-henglu commented 1 month ago

I'll close this issue as it's completed, but feel free to reopen it if there's any question