1

I recreated a state file for an existing infrastructure using a third party tool i.e. terraformer. Now I want o move the .tfstate to another azurerm back-end and manage from there.

If I just copy the file i.e. mystate.tfstate from local to inside storage account container with the same file name/key as in the backend configurations will it work or do I need to do something else to achieve it?

I don't want to risk the state file or infrastructure by trying to do something that isn't sure to work as I expect.

halfer
  • 19,824
  • 17
  • 99
  • 186
Maven
  • 14,587
  • 42
  • 113
  • 174
  • 2
    I think you need to change the local `backend` configuration and re-run `terraform init`. Terraform should ask you if you want to migrate the current state to a new backend. – Marko E Feb 09 '22 at 11:54

2 Answers2

4

Terraform has some automatic migration behavior built in to terraform init.

Based on your description, it sounds like so far you've been using local state storage, and so the latest state snapshot is in a .tfstate file on your local system and you probably don't have a backend block in your configuration yet, since local storage is the default.

Before beginning this process, I suggest first making a copy of your state file in a safe place so that you can experiment more confidently. This process should not risk your existing state file, but it can't hurt to be careful if you've invested significant work in constructing that state file already.

Next, add a backend "azurerm" block to tell Terraform it should use that backend. Refer to the documentation to see which settings you'll need to set and what other preparation steps you may need to make first, such as establishing a new storage container.

If you've been using local state then you will presumably have a terraform.tfstate file in your current working directory, which Terraform will check for in the next step. If you've renamed that file at any point so far, you'll need to rename it back to terraform.tfstate to match the expectations of Terraform's local state storage implementation.

If you now run terraform init, Terraform should notice the following two things:

  1. You have a backend block but the current working directory doesn't currently have an initialized backend connection.
  2. You have an existing terraform.tfstate file in your working directory.

With those two things being true, Terraform will propose to migrate your state from the local backend to the azurerm backend. You can follow the steps it proposes and answer the prompts that appear, after which you should find the same state snapshot stored in your configured Azure storage container.

Once you've confirmed that the object is present in Azure storage, you can delete the terraform.tfstate file, since Terraform will no longer refer to it.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
1

I don't want to risk the state file or infrastructure by trying to do something that isnt sure to work as I expect.

Make a backup of the state file first, and then you won't be risking the state file.

As long as you aren't running an apply command, you won't be risking the infrastructure. And even if you are running an apply command, you will be able to review the plan before proceeding.

So just (always) backup your state file, and always review a plan before applying it, and there is no risk.

Mark B
  • 183,023
  • 24
  • 297
  • 295