# Troubleshoot the Terraform Provider

Each section of this page starts from an error that Terraform prints while it works with the [Azion Terraform Provider](/en/documentation/devtools/terraform/), then gives its cause and its fix. Errors about the personal token and the provider version come first. Errors from a migration to provider v2.0 follow: removed resource types, the state, and imports.

---

## Terraform fails with personal token is required

A Terraform command stops before the provider calls Azion API, with this error:

```text
Error: error configuring Terraform Azion Provider: personal token is required
```

The provider found no personal token. It reads the token from the `AZION_API_TOKEN` environment variable or from its `api_token` argument.

- **Set the environment variable**: export `AZION_API_TOKEN` in the terminal that runs Terraform, and leave the `provider "azion"` block empty.
- **Pass the token in the api\_token argument**: declare a sensitive `api_token` variable, set it in `terraform.tfvars`, and pass `var.api_token` to the provider. For the files, refer to [Provide your personal token](/en/documentation/devtools/terraform/getting-started/#provide-your-personal-token).
- **Create a personal token**: the token needs the permissions that your resources require. For more information, refer to [Personal tokens](/en/documentation/fundamentals/personal-tokens/).

To set the variable, replace `[TOKEN VALUE]` with your personal token and run this command in the terminal that runs Terraform:

```bash
export AZION_API_TOKEN="[TOKEN VALUE]"
```

The provider reads the token, and the command no longer stops at the provider configuration.

---

## Terraform fails with no compatible versions

Terraform finds no version of the Azion provider that it can use and stops with this error:

```text
Error: provider registry.terraform.io/aziontech/azion: no compatible versions
```

The Azion Terraform Provider needs Terraform 1.0 or later. The version of Terraform Core on your machine is the first thing to check.

- **Check the Terraform version**: `terraform version` prints the version of Terraform Core.
- **Install a later version of Terraform**: when the version is earlier than 1.0, install a later one. For the install, refer to [Install Terraform](/en/documentation/devtools/terraform/getting-started/#install-terraform).

To print the version of Terraform Core:

```bash
terraform version
```

`terraform version` reports version 1.0 or later, which the provider needs.

---

## Terraform fails with Failed to query available provider packages

Terraform cannot list the versions of the Azion provider and stops with this error:

```text
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider aziontech/azion
```

Terraform looks the provider up by the source and the version in the `required_providers` block. The source of the Azion Terraform Provider is `aziontech/azion`.

- **Check the source and the version**: set `source` to `aziontech/azion` and `version` to the provider version that you use, such as `2.0.0`.
- **Upgrade the installed provider**: `terraform init -upgrade` downloads the version that the block sets. For the full upgrade to v2.0, refer to [Upgrade the provider to v2.0](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4/#upgrade-the-provider-to-v20).

This `required_providers` block sets the source and pins provider version `2.0.0`:

```hcl
terraform {
  required_providers {
    azion = {
      source  = "aziontech/azion"
      version = "2.0.0"
    }
  }
}
```

To download the provider version that the block sets, run this command in the project directory:

```bash
terraform init -upgrade
```

Terraform installs the provider version that the block sets, such as `2.0.0`, in the project.

---

## Terraform fails with resource not found

A Terraform command fails on a resource of the configuration with this error:

```text
Error: resource not found
```

The configuration refers to a resource that Azion API v4 does not have. Provider v2.0 works only with API v4, and API v4 removed or renamed some API v3 resources.

- **Check the resource on your account**: look for the resource in [Azion Console](https://console.azion.com/). The [Azion API reference](https://api.azion.com/) lists the resources that API v4 serves.
- **Rename the v1.x resources**: a resource type of provider v1.x can have another name in v2.0, or no counterpart. For each v1.x name and its replacement, refer to [Resource changes in v2.0](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4/#resource-changes-in-v20).
- **Stay on provider v1.x for an API v3 account**: an account on API v3 uses provider v1.x. For more information, refer to [Azion Terraform Provider v1.x (API v3)](/en/documentation/devtools/terraform/terraform-provider-v3/).

The configuration names only resources that exist in API v4, and the error no longer appears.

---

## Terraform reports Invalid resource type for azion\_domain

A configuration written for provider v1.x fails under provider v2.0 with this error:

```text
Error: Invalid resource type
on main.tf line 10:
10: resource "azion_domain" "example" {
The provider aziontech/azion does not support resource type "azion_domain".
```

Provider v2.0 removed the `azion_domain` resource. A workload and its deployment take its role.

- **Replace the domain with a workload**: rewrite the block as an `azion_workload` resource and an `azion_workload_deployment` resource. For the v1.x block and the blocks that replace it, refer to [Replace a domain with a workload](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4/#replace-a-domain-with-a-workload).
- **Look up the resource types of the provider**: the [Terraform Registry](https://registry.terraform.io/providers/aziontech/azion/latest/docs) documents every resource type and argument that the provider accepts.

The configuration uses only resource types that provider v2.0 accepts.

---

## Terraform fails with Error refreshing state

During a migration to provider v2.0, Terraform stops on the v1.x origin resource with this error:

```text
Error: Error refreshing state: azion_edge_application_origin.example:
resource not found
```

The state still lists a resource that the provider no longer finds. Provider v2.0 replaces the v1.x origin resource with `azion_connector`.

- **Remove the resource from the state**: `terraform state rm` takes the resource out of the state and leaves the object on your account. For the commands that remove the v1.x domain, origin, and function resources, refer to [Remove the v1.x resources from the state](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4/#remove-the-v1x-resources-from-the-state).
- **Manage the origin as a connector**: declare an `azion_connector` resource for the origin. For the v1.x block and the block that replaces it, refer to [Replace an origin with a connector](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4/#replace-an-origin-with-a-connector).

The state no longer lists the v1.x origin resource, and Terraform refreshes the state without the error.

---

## terraform import fails with Cannot import non-existent remote object

`terraform import` stops with this error:

```text
Error: Cannot import non-existent remote object
```

No object with the ID that you passed exists in Azion API v4. An API v3 resource can have a different ID in API v4.

- **Look up the API v4 ID**: find the ID of the resource in [Azion Console](https://console.azion.com/).
- **Import with the API v4 ID**: run `terraform import <type>.<name> <id>` with the ID from Azion Console. For the import of a workload, a connector, and a function, refer to [Import the resources under their v2.0 types](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4/#import-the-resources-under-their-v20-types).

The state lists the imported resource under its v2.0 type.

---

## Related resources

- [Azion Terraform Provider quickstart](/en/documentation/devtools/terraform/getting-started.md): The token, the version pin, and the commands that a first configuration needs.
- [Migrate from provider v1.x to v2.0](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4.md): The resource changes, state removals, and imports that the migration errors point to.
- [How the Terraform Provider works](/en/documentation/devtools/terraform/how-it-works.md): How state, plan, and apply fit together, and what terraform state rm and terraform import change.
- [Terraform Provider best practices](/en/documentation/devtools/terraform/best-practices.md): Pin the provider version and pass the token through an environment variable.
