How to resolve the state file version conflict when the state files are kept in git

terraform

My state files are kept along side the terraform files. (I know this is not ideal, but it is what it is at my workplace at the moment. There are plans to move away from this state management model)

The goal of my branch is to create a new ec2 instance (named adhoc-ec2) with various policy attachments. The change is tested so the ec2 instance exists in the development environment.

A the end my branch and its associated PR has gone through a PR review process, the state file has already updated in another branch and merged into the master branch.

The difference between the two versions of the files are so big it is impossible to reconcile manually.

The root cause is of course that the terraform file contains too many resources, but it is too late to undo this now.

Now I have to resolve all the conflicts before I can merge my branch into master.

I tried to resolve the conflict this way:

  1. Rebase my branch to master (such that my terraform file will contain all the latest changes)

  2. Overwrite the stale state file in branch with the latest version in the master

  3. run terraform refresh in an attempt to synchronise the state file and the actual resources of the AWS environment.

However apparently this plan does not work. Because when I run terraform plan immediately afterwards, the plans shows that terrafrom still wants to create the ec2 instance adhoc-ec2 (which is already created as I stated earlier in this question)

Alternatively I can destroy the ec2 instances with all the dependent roles and policies, but it is time consuming.

Is there a easier way to resolve this kind of state file conflict? Why refresh did not work in the first place?

Best Answer

Running terraform refresh will only update resources already in the state file so if your instance doesn't exist yet in the state file, Terraform still wants to create it.

What you should do instead is use terraform import to add the resources to state file based on what the AWS API's return which should then match what is in your manifests.