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

by Anthony Kong   Last Updated September 12, 2019 01:00 AM

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?

Tags : terraform

Related Questions

terraform apply keeps changing the same (NAT) route

Updated December 14, 2016 08:00 AM

Can i output a provider from a module in terraform?

Updated April 26, 2018 03:00 AM