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:
- You have a
backend
block but the current working directory doesn't currently have an initialized backend connection.
- 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.