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
175 stars 47 forks source link

2.0 beta: Error: Missing Resource State After Create #618

Open matt-FFFFFF opened 1 day ago

matt-FFFFFF commented 1 day ago

Hi @ms-henglu I am getting this error on a plan to deploy 378 resources.

This is the sample code: https://registry.terraform.io/modules/Azure/avm-ptn-alz/azurerm/latest/examples/default

Microsoft.Management/managementGroups/landingzones/providers/Microsoft.Authorization/roleAssignments/4eb5e902-cf89-5342-9592-d66dfcfaee49]
╷
│ Error: Missing Resource State After Create
│ 
│   with module.alz_architecture.azapi_resource.role_definitions["alzroot/Security-Operations"],
│   on ../../main.role_definitions.tf line 1, in resource "azapi_resource" "role_definitions":
│    1: resource "azapi_resource" "role_definitions" {
│ 
│ The Terraform Provider unexpectedly returned no resource state after having no errors in the resource creation. This is always an issue in the
│ Terraform Provider and should be reported to the provider developers.
│ 
│ The resource may have been successfully created, but Terraform is not tracking it. Applying the configuration again with no other action may
│ result in duplicate resource errors. Import the resource if the resource was actually created and Terraform should be tracking it.

I also get some other resources getting created but not in state..

╷
│ Error: Resource already exists
│ 
│   with module.alz_architecture.azapi_resource.role_definitions["alzroot/Subscription-Owner"],
│   on ../../main.role_definitions.tf line 1, in resource "azapi_resource" "role_definitions":
│    1: resource "azapi_resource" "role_definitions" {
│ 
│ A resource with the ID
│ "/providers/Microsoft.Management/managementGroups/alzroot/providers/Microsoft.Authorization/roleDefinitions/402344ce-48c4-5ac1-9320-16726050f964"
│ already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for
│ "azapi_resource" for more information.

Should I enable provider debugging and report back?

matt-FFFFFF commented 1 day ago

I suspect that this is causing the issue:

https://github.com/Azure/terraform-provider-azapi/blob/712f9ab0130fc3b7f722682ce9019ec3b675bf37/internal/services/azapi_resource.go#L703

We do not check that the responseBody is not empty, should we retry if this is the case?

ms-henglu commented 8 hours ago

Hi @matt-FFFFFF ,

Thank you for taking time to report this issue.

Would you please share the debug logs to help investigate? Thanks. My guess is the API returns 404 in GET after the resource is created.

matt-FFFFFF commented 44 minutes ago

Local debugging shows different scenarios that the RP returns (depending on the RP):

  1. 404 picked up as error, e.g. XyzResourceNotFound
  2. No error but no body either

In my testing, this issue is fixed by #621

Please could you review