Import existing resources into Terraform
Bring Azion resources you created in Console, the API, or the CLI under Terraform with import blocks, and let Terraform write their configuration.
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 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.
The examples import an Application and one of its cache settings.
Prerequisites
- Terraform 1.17.2 or later, for
importblocks and the-generate-config-outoption ofterraform plan. To install it, refer to Install Terraform. - A personal token in the
AZION_API_TOKENenvironment variable. To set it, refer to Provide your personal token. - The ID of each resource to import. The examples use an Application with the ID
1795011484and its cache setting395100. To list the IDs with the Azion CLI, refer to azion list application and azion list cache-setting.
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:
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.
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:
-
Download the provider:
Text -
Plan the import and write the configuration to
generated.tf:TextTerraform prints a warning that configuration generation is experimental, so review the generated file in the next step.
-
Open
generated.tfand review it. Each resource block carries values read from the account, such as the Max Age of the cache setting:generated.tf -
(Optional) To make the cache setting depend on the Application, reference the Application in
generated.tfinstead of its literal 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:
-
Apply the import:
Enter
yeswhen Terraform asks to perform the actions:Text -
Check that the configuration matches the account:
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 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.
To change the Max Age of the imported cache setting:
-
In
generated.tf, changemax_ageinsidemodules.cachefrom3600to7200seconds. -
Apply the change:
Terraform shows the change before it asks for confirmation. Enter
yes:Text
The cache setting has a Max Age of 7,200 seconds in the account.