terraform import existing resources - not quite with 1.5.x with generated output

Terraform import can be quite interesting thing to do. So let's start with us trying to import a storage account. So we will start off using hcl import statement to import this. 


provider "azurerm" {
  features {}
}

resource "azurerm_storage_account" "imported_storage" {
  name                     = "saforimort"
  resource_group_name      = "mytest-kv-rg"
  location                 = "australiaeast"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

import {
  to = azurerm_storage_account.imported_storage
  id = "/subscriptions/your-subscription-id/resourceGroups/
mytest-kv-rg/providers/Microsoft.Storage/storageAccounts/saforimort"
}

As you can see here, we are trying to import to "azurerm_storage_account" "imported_storage" . and we just need to specify the azure resource id. 

And when we run "terraform apply". we can see the output here. 


And the state already imported. And of course we can change it to say GRS. So running "terraform plan" after code change will give us this results. 


What if the code that we're trying to import the actual Azure resource? 

Then terraform will import and change the state file for you. For example, lets change our storage account replication type to GRS. Then we import the state file. As you can see, terraform will 


What if you run terraform refresh with a drift - for example in the code we have replication type as LRS, then someone change that to GRS?

Running terraform refresh updates the drift between Azure resources to the state file. When we run terraform plan/apply, it uses the code as a source of truth.


How does this compare to cli terraform import? 

The achieve the same purpose.  terraform cli import does have its use-case but tedious to do if you have many different environment with many different state files

You can replicate the import block with this command here:- 

terraform import azurerm_storage_account.imported_storage2 /subscriptions/your-subscription--id/resourceGroups/mytest-kv-rg/providers/Microsoft.Storage/storageAccounts/saforimort

And you would have something like this. 



        





Comments

Popular posts from this blog

vllm : Failed to infer device type

android studio kotlin source is null error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20