0
votes

I have a terraform state file (terraform.tfstate) highlighted below in Red arrow, which is stored locally in the disk.

I also have a remote backend state file (stored in Azure Blob Storage) with resources that i have deployed.

I wanted to append the existing local state file with the existing remote backend in Azure.

enter image description here

I moved the local state to a directory and I did perform terraform state push terraform.tfstate

I got below error: terraform cannot import state with lineage over unrelated stage with lineage

Then i did terraform state push -force terraform.tfstate

However it completely replaced my existing state(in remote) with local state. (I did backup my remote state).

I wanted to append my local state file with remote State file. How can do this? All i wanted to have is -one single state file in remote with already provisioned resources, any other suggestion pls. help.

1
How many resources are there in each state, can't you just import the resources in the new state file? Are both deployed with the same terraform version? Does your main.tf produce resources that match the state in the local state file? Otherwise merging the state does not make sense at all since the next deployment would destroy the resources anyway.luk2302
@luk2302 Many Resources are there in local state file.Both deployed in same terraform version. Main.tf under adauthservice does create a exact match with local state filePrasanna Nandakumar

1 Answers

0
votes

The timeline of events given it isn't clear how you got into this state. Based on what you describe it suggests you might have migrated to the blob, but then came back at a later date and started using local state again. Regardless terraform doesn't know. This thinks there are two different states.

Given the number of resources you have this might be difficult. Pick which ever state file you have with more resources as the base. You have a few options

  1. (recommended) Import all the remaining resources in the smaller state file into into the larger one.
  2. Try to use use the mv command
  3. Edit the json statefile by copying it in (this is unpredictable - but if you are desperate)