terraformazapi

azapi_resource_action: Invalid value for "str" parameter: string required


I am using azapi_resource_action to generate keys with the following code

resource "azapi_resource_action" "ssh_public_key_gen" {
  type        = "Microsoft.Compute/sshPublicKeys@2022-11-01"
  resource_id = azapi_resource.ssh_public_key.id
  action      = "generateKeyPair"
  method      = "POST"

  response_export_values = ["publicKey", "privateKey"]
}

When trying to show the public key I get the following error

output "key_data" {
  value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}




 Error: Invalid function argument
│
│   on ssh.tf line 23, in output "key_data":
│   23:   value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
│     ├────────────────
│     │ azapi_resource_action.ssh_public_key_gen.output is object with 2 attributes
│
│ Invalid value for "str" parameter: string required.

I am using the following providers version

  required_providers {
    azapi = {
      source  = "azure/azapi"
      version = "~>1.5"
    }
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
}

Solution

  • The documentation for azapi_resource_action says that the output attribute has an object type:

    • output - The HCL object containing the properties specified in response_export_values.

    Since your response_export_values argument has two elements publicKey and privateKey, I would expect this object to have two attributes. That seems to be confirmed by the error message, which says "azapi_resource_action.ssh_public_key_gen.output is object with 2 attributes".

    Since this is already an object, there is no need to perform any decoding of it: you can refer directly to the relevant attributes. For example:

    output "key_data" {
      value = azapi_resource_action.ssh_public_key_gen.output.publicKey
    }