I am facing an issue with importing modules in Terraform. Specifically, I am trying to import module.st-storage-account.azurerm_storage_account.storage["pnretstuscstlafiltersto7"]
, but in the state list, I found module.st_logic_apps.data.azurerm_storage_account.main["pnretstuscstlafiltersto7"]
. Similarly, for the logic app, I am importing module.st_logic_apps.azurerm_logic_app_standard.main["pn-re-tst-us-c-st-la-com-maintenance"]
, but found module.st_logic_apps.data.azurerm_subnet.main["pn-re-tst-us-c-st-la-com-maintenance"]
in the state list. This discrepancy is causing confusion.
Here is the import command I am using:
terraform import -var-file="./variables/test/variables.tfvars" 'module.st-storage-account.azurerm_storage_account.storage["pnretstuscstlafiltersto7"]' /subscriptions/***/resourceGroups/pn-re-tst-us-c-rg/providers/Microsoft.Storage/storageAccounts/pnretstuscstlafiltersto7
The state list shows:
module.st-storage-account.azurerm_storage_account.storage["pnretstuscstlafiltersto7"]
module.st-logic-apps.data.azurerm_storage_account.main["pnretstuscstlafiltersto7"]
module.st-logic-apps.azurerm_logic_app_standard.main["pn-re-tst-us-c-st-la-com-maintenance"]
module.st-logic-apps.data.azurerm_subnet.main["pn-re-tst-us-c-st-la-com-maintenance"]
It should import into module.st-storage-account.azurerm_storage_account.storage["pnretstuscstlafiltersto7"]
while importing the storage account. But now it's importing here module.st-logic-apps.data.azurerm_storage_account.main["pnretstuscstlafiltersto7"]
Import Issue while using terraform
Data sources are used to reference existing resources without managing them, here we don't need to specify the info related to resource. while resource blocks are used for managing resources. Make sure you're importing into actual resource blocks and not data sources.
This is where we youre facing the blocker i.e., referencing the resource to import configuration.
Here from the query it was clear that youre importing a storage account into module.st-storage-account.azurerm_storage_account.storage["pnretstuscstlafiltersto7"]
, but the state contains a reference to module.st-logic-apps.data.azurerm_storage_account.main["pnretstuscstlafiltersto7"]
, which is a data source and similar thing was happening with Logic App
.
This action might be led to State inconsistency. Which is not at all good for state file to set up a match with configuration. Since youre trying to import make sure the configuration matches with the resource info.
Once the checking the configuration was done check the list of resources in the state file by running the command.
terraform state list
From the list of resources remove the data module resources.
terraform state rm 'module.st-logic-apps.data.azurerm_storage_account.main["pnretstuscstlafiltersto7"]'
terraform state rm 'module.st-logic-apps.data.azurerm_subnet.main["pn-re-tst-us-c-st-la-com-maintenance"]'
Now re-import the resource to the correct module path
terraform import -var-file="./variables.tfvars" 'module.st-storage-account.azurerm_storage_account.storage["pnretstuscstlafiltersto7"]' /subscriptions/***/resourceGroups/pn-re-tst-us-c-rg/providers/Microsoft.Storage/storageAccounts/pnretstuscstlafiltersto7
This should be for the storage account
terraform import -var-file="./variables.tfvars" 'module.st_logic_apps.azurerm_logic_app_standard.main["pn-re-tst-us-c-st-la-com-maintenance"]' /subscriptions/***/resourceGroups/pn-re-tst-us-c-rg/providers/Microsoft.Logic/workflows/pn-re-tst-us-c-st-la-com-maintenance
This should be for the Logic App.
Now continue with the commands terraform refresh
, terraform apply
.
Refer:
Terraform import module state - Stack Overflow by Martin Atkins
How to use terraform import with passed-in variables?
you can check with these on how to refer the resource using Import command from these.
https://developer.hashicorp.com/terraform/cli/commands/import