DevOps | Scripts | Automation

Terraform

Terraform Import

Terraform Import – Overview

Terraform is an IAC (Infrastructure as a code) language used to create and maintain the infrastructure resources using the state file (terraform.tfstate). A few questions come to our mind when we make a new infra.

  • What about the resource that already exists in the environment? How can Terraform add that resource to the state file?
  • Can we update the existing state file and add the current resource?

Here, we will discuss both scenarios.

Before terraform version 1.5, we need to use terraform import command from CLI but in version 1.5 Hashicop has introduced the import block. Check this official article: Terraform Import.

For this article, we will use Terraform CLI for Azure Cloud, and the Import block can be covered in the future.

Terraform Import – Add resources to the state file

Let’s say you need to import the storage account to the state file then,

  • Create a main.tf file for storage account with resource block.
  • Run terraform import command through CLI.


1. Create main.tf file.

When we create a main.tf file for any resource block we need to provide all mandatory parameters but in the import case, minimum parameters are sufficient.

In the storage account case, if you check the storage account resource block requirement (storage account resource block), it needs multiple parameters, while the data block for storage account requires minimum parameters to fetch data.

Here, we will use the storage account resource block but with the minimum parameters required that we use for the data block (name and resource group). In this article latter, I will show you why we shouldn’t use the minimum parameters when using the existing state file.

The below code will be in our main.tf file.

# main.tf file

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.63.0"
    }
  }

}

provider "azurerm" {
  # Configuration options

  features { }
}



resource "azurerm_storage_account" "storageAccount" {
  name                = "blbstrg4"
  resource_group_name = "StorageAccountTest"
}


2. Run the Import CLI command

Terraform import cli command format will be as below. (Make sure you are logged in using the proper Azure subscription using the “az login” command).

terraform import resourceBlock.resourceBlockName ResourceId

Resource ID you can get from the Azure portal or through PowerShell / Azure CLI command.

So in our case, browse the directory of the main.tf file and run the below command.

terraform import azurerm_storage_account.storageAccount "/subscriptions/xxxxx-xxxx-xxxx-xxxx/resourceGroups/StorageAccountTest/providers/Microsoft.Storage/storageAccounts/blbstrg4"

Output:

In the same folder as the main.tf, check terraform.tfstate file. The resource will be imported.

Now you know how to import the existing resources into the state file. Just validate a few resource properties if they match the state file output.

Now let’s try to run terraform validate and plan command.


3. run Terraform validate and plan command

terraform init

No problem with terraform init. This looks good. Let’s run terraform validate.

terraform validate

Here is the catch, earlier we discussed import command needs the minimum parameters but the problem here is when you want other resources to be created using Terraform then you need to add them into the main.tf file and this time validate command will fail because it can’t find all the required parameters for the resource we used for terraform import.

Let’s run terraform plan (we know it will fail).

terraform plan

Same as the terraform validate error. Ok, let’s take the reference and provide all the required parameters for the storage account in order to successfully run terraform validate and plan.

Modified main.tf file

resource "azurerm_storage_account" "storageAccount" {
  name                = "blbstrg4"
  resource_group_name = "StorageAccountTest"
  location = "eastus"
  account_replication_type = "LRS"
  account_tier = "Standard"
}

Run terraform validate.

Success !!. Now run terraform plan.

Voila !! The terraform plan is successful. Now you can go ahead and add other resources to the main.tf file so wouldn’t face problems in running Terraform apply.


Add more resources to the main.tf file.

Let’s add one more resource (storage account) in the same main.tf file and it should automatically append in the state file. The new resource is shown below.

resource "azurerm_storage_account" "storageAccount1" {
  name                = "blbstrg090723"
  resource_group_name = "StorageAccountTest"
  location = "eastus2"
  account_replication_type = "LRS"
  account_tier = "Standard"
}

Now, run terraform plan.

It says only 1 resource to add and 0 to change means that the previously imported resource is considered. Now run terraform plan to add newly added resources to the current state file.

terraform apply --auto-approve

Check the terraform file, the new resource will be added and your work is done.

Terraform Import – best practices

So In order to, import resources to the terraform state,

  • First, write the main.tf file with a resource to import it.
  • Run the terraform import to generate the state file.
  • Add the actual terraform configuration into the main.tf file to create resources.
  • Run terraform init, plan, apply command.

Loading