Mahesh
Mahesh

Reputation: 11

Terraform state file migration from local to GCP cloud

I have a terraform state file in local which we want to migrate it to GCP cloud as a remote backend. I have created the bucket named "dns-zone" and added the existing tfstate file named terraform.tfstate into the bucket folder named "dns-folder".

The backend code looks like this:

terraform {
  backend "gcs" {
    bucket  = "dns-zone"
    prefix  = "dns-folder"
  }
}

after that I ran the command "terraform init -migrate-state".

now its creating the new tfstate file as "default.tfstate" in the bucket location instead of using the previuos state file (terraform.tfstate). And when i try to run terraform plan it shows whole infra to create which is already exists.

Please help me to resolve this issue.

Upvotes: 0

Views: 160

Answers (1)

Ngenator
Ngenator

Reputation: 11269

When working with the state, I always use and recommend using the terraform state ... commands to ensure terraform does everything necessary to keep track of it. In your case, you would use terraform state push <path/to/your/terraform.tfstate> to get your local state into your remote backend. https://developer.hashicorp.com/terraform/cli/commands/state/push

Upvotes: 0

Related Questions