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

Microsoft.App/managedEnvironments: Error: Unsupported attribute This object does not have an attribute named "staticIp" #615

Closed weineran closed 4 days ago

weineran commented 4 days ago

Reference: https://learn.microsoft.com/en-us/azure/templates/microsoft.app/managedenvironments?pivots=deployment-language-terraform

Bug Description: After creating a managed container apps environment using azapi_resource of type Microsoft.App/managedEnvironments@2024-03-01, the staticIp field is not exposed / accessible to other resources.

Steps to reproduce

output "container_app_environment_ip" { value = azapi_resource.container_app_environment.body.properties.staticIp }

* Run `terraform plan`
* **EXPECTED**: It should output the **staticIp** of the managed environment (and of course first create the managed environment if it doesn't already exist)
* **ACTUAL**: The `terraform plan` command fails with this error:

╷ │ Error: Unsupported attribute │ │ on apps.tf line 183, in output "container_app_environment_ip": │ 183: value = azapi_resource.container_app_environment.body.properties.staticIp │ ├──────────────── │ │ azapi_resource.container_app_environment.body.properties is object with 9 attributes │ │ This object does not have an attribute named "staticIp". ╵


**Additional Info**
* In my case, the resource was already created via "ClickOps" in the Azure web portal, and I imported it to Terraform using `terraform import`. If I look at the Resource JSON in the Azure web portal as a sanity check, I do see the "staticIp" attribute in the JSON. So it does exist...

{ ... "type": "Microsoft.App/managedEnvironments", "location": "East US", ... "properties": { "provisioningState": "Succeeded", ... "staticIp": "10.0.4.220", } }

* I also tried adding the "staticIp" attribute explicitly in the terraform for the `resource "azapi_resource" "container_app_environment"`. Then when I run the plan, as you might expect, it fails with this error saying that "staticIp" is read-only:

╷ │ Error: Invalid configuration │ │ with azapi_resource.container_app_environment, │ on apps.tf line 137, in resource "azapi_resource" "container_app_environment": │ 137: resource "azapi_resource" "container_app_environment" { │ │ embedded schema validation failed: the argument "body" is invalid: │ properties.staticIp is not expected here, it's read only │ You can try to update azapi provider to the latest version or disable the validation using the feature flag schema_validation_enabled = false within the resource block ╵

ms-henglu commented 4 days ago

Hi @weineran ,

Thank you for taking time to open this issue!

The staticIp field is a read-only field that only could be accessed by the output. Here's an example:

resource "azapi_resource" "container_app_environment" {
  type      = "Microsoft.App/managedEnvironments@2024-03-01"
  name      = "managedEnvironment-example"
  location  = "eastus"
  parent_id = azurerm_resource_group.resource_group.id
  body = {...}
  response_export_values = ["*"] // it means export everything
}

output "staticIp" {
  value = azapi_resource. container_app_environment.output.properties.staticIp
}

More details: https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource#response_export_values https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource#output

weineran commented 4 days ago

Thanks @ms-henglu but when I try that, and run terraform plan, I get this error:

╷
│ Error: Unsupported attribute
│
│   on \path\to\apps.tf line 195, in output "staticIp":
│  195:   value = azapi_resource.container_app_environment.output.properties.staticIp
│     ├────────────────
│     │ azapi_resource.container_app_environment.output is object with no attributes
│
│ This object does not have an attribute named "properties".
╵
ms-henglu commented 4 days ago

Please check whether response_export_values = ["*"] is specified in the config.

weineran commented 4 days ago

Please check whether response_export_values = ["*"] is specified in the config. Confirmed, yes it is.

when I try that, and run terraform plan, I get this error: I mispoke. It was the terraform import command that gave this error.

I commented out the output block, completed the terraform import, then ran the terraform plan and it seems to have worked!

Changes to Outputs:
  + staticIp = (known after apply)

Thank you 🙏