---
name: azion-import-existing-resources-into-terraform
description: >-
  Bring Azion resources you created in Console, the API, or the CLI under Terraform with import blocks, and let Terraform write their configuration.
---

# Import existing resources into Terraform

You can bring resources that already exist in your Azion account under Terraform with `import` blocks. Terraform reads each resource through the [Azion Terraform Provider](/en/documentation/devtools/terraform/) and adds it to the Terraform state. With the `-generate-config-out` option of `terraform plan`, Terraform also writes the configuration of each resource. To move a configuration from provider v1.x instead, refer to [Migrate from provider v1.x to v2.0](/en/documentation/devtools/terraform/terraform-migration-v3-to-v4/).

The examples import an [Application](/en/documentation/platform/applications/) and one of its [cache settings](/en/documentation/platform/applications/cache/cache-settings/).

---

## Prerequisites

- Terraform 1.17.2 or later, for `import` blocks and the `-generate-config-out` option of `terraform plan`. To install it, refer to [Install Terraform](/en/documentation/devtools/terraform/getting-started/#install-terraform).
- A [personal token](/en/documentation/fundamentals/personal-tokens/) in the `AZION_API_TOKEN` environment variable. To set it, refer to [Provide your personal token](/en/documentation/devtools/terraform/getting-started/#provide-your-personal-token).
- The ID of each resource to import. The examples use an Application with the ID `1795011484` and its cache setting `395100`. To list the IDs with the [Azion CLI](/en/documentation/devtools/cli/), refer to [azion list application](/en/documentation/devtools/cli/resources/application/#list) and [azion list cache-setting](/en/documentation/devtools/cli/resources/cache-setting/#list).

---

## Declare the resources to import

An `import` block tells Terraform which resource in the account to add to the state. Its `to` argument holds the resource address in your configuration, and its `id` argument holds the import ID. The import ID depends on the resource type:

| Resource type                     | Import ID                             |
| --------------------------------- | ------------------------------------- |
| `azion_application_main_setting`  | `<application_id>`                    |
| `azion_application_cache_setting` | `<application_id>/<cache_setting_id>` |

In an empty directory, create a `main.tf` file with the provider and one `import` block per resource. Replace the IDs with your own:

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

provider "azion" {}

import {
  to = azion_application_main_setting.site
  id = "1795011484"
}

import {
  to = azion_application_cache_setting.static_assets
  id = "1795011484/395100"
}
```

The configuration declares two imports and no resource blocks: `terraform plan -generate-config-out` writes the resource blocks.

For the import ID of every other resource type, refer to the [provider documentation](https://registry.terraform.io/providers/aziontech/azion/2.10.0/docs).

---

## Generate the configuration

`terraform plan` with `-generate-config-out` reads each resource from the account and writes its resource block in a new file.

To generate the configuration:

1. Download the provider:

   ```bash
   terraform init
   ```

   ```text
   - Installing aziontech/azion v2.10.0...
   - Installed aziontech/azion v2.10.0 (self-signed, key ID 60105B099A0FF3B3)
   ...
   Terraform has been successfully initialized!
   ```

2. Plan the import and write the configuration to `generated.tf`:

   ```bash
   terraform plan -generate-config-out=generated.tf
   ```

   ```text
     # azion_application_cache_setting.static_assets will be imported
     # (config will be generated)
   ...
     # azion_application_main_setting.site will be imported
     # (config will be generated)
   ...
   Plan: 2 to import, 0 to add, 0 to change, 0 to destroy.

   Warning: Config generation is experimental

   Generating configuration during import is currently experimental, and the
   generated configuration format may change in future versions.
   ...
   Terraform has generated configuration and written it to generated.tf.
   ```

   Terraform prints a warning that configuration generation is experimental, so review the generated file in the next step.

3. Open `generated.tf` and review it. Each resource block carries values read from the account, such as the Max Age of the cache setting:

   ```hcl title="generated.tf"
   resource "azion_application_main_setting" "site" {
     application = {
       active = true
       debug  = false
       modules = {
         ...
       }
     }
   }

   resource "azion_application_cache_setting" "static_assets" {
     application_id = 1795011484
     cache_setting = {
       browser_cache = {
         behavior = "override"
         max_age  = 600
       }
       modules = {
         ...
         cache = {
           behavior = "override"
           ...
           max_age = 3600
           ...
         }
       }
       name = "static-assets"
     }
   }
   ```

4. (Optional) To make the cache setting depend on the Application, reference the Application in `generated.tf` instead of its literal ID:

   ```hcl
   application_id = azion_application_main_setting.site.application.application_id
   ```

The `generated.tf` file holds one resource block per `import` block. With the reference from step 4, `terraform plan` still shows `2 to import, 0 to add, 0 to change, 0 to destroy`.

---

## Import the resources

`terraform apply` adds the resources to the Terraform state. When the plan shows `0 to add, 0 to change, 0 to destroy`, the apply changes nothing in the account. When it shows a change, the apply makes that change in the account.

To import the resources:

1. Apply the import:

   ```bash
   terraform apply
   ```

   Enter `yes` when Terraform asks to perform the actions:

   ```text
   azion_application_main_setting.site: Importing... [id=1795011484]
   azion_application_main_setting.site: Import complete [id=1795011484]
   azion_application_cache_setting.static_assets: Importing... [id=1795011484/395100]
   azion_application_cache_setting.static_assets: Import complete [id=1795011484/395100]

   Apply complete! Resources: 2 imported, 0 added, 0 changed, 0 destroyed.
   ```

2. Check that the configuration matches the account:

   ```bash
   terraform plan
   ```

   ```text
   No changes. Your infrastructure matches the configuration.
   ```

Terraform manages the two resources, and its configuration matches their values in the account.

---

## Change an imported resource

An imported resource changes like any other resource that Terraform manages: edit its block and apply. After the import, a change made in [Azion Console](https://console.azion.com/) or the API shows as drift in the next `terraform plan`. The next apply changes it back. For more information, refer to [Configuration is the desired state](/en/documentation/devtools/terraform/how-it-works/#configuration-is-the-desired-state).

To change the Max Age of the imported cache setting:

1. In `generated.tf`, change `max_age` inside `modules.cache` from `3600` to `7200` seconds.

2. Apply the change:

   ```bash
   terraform apply
   ```

   Terraform shows the change before it asks for confirmation. Enter `yes`:

   ```text
     ~ resource "azion_application_cache_setting" "static_assets" {
         ~ cache_setting  = {
             ...
             ~ modules       = {
                 ~ cache                   = {
                     ~ max_age          = 3600 -> 7200
   ...
   Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
   ```

The cache setting has a Max Age of 7,200 seconds in the account.

---

## Next steps

- [How the Terraform Provider works](/en/documentation/devtools/terraform/how-it-works.md): State, plan and apply, and how the provider maps to the Azion API v4.
- [Terraform Provider best practices](/en/documentation/devtools/terraform/best-practices.md): Split files by role, store state remotely, and pin the provider version.
